I think that the SLR is a loop to pay attention to two points, the first one is the condition of the loop continuation, the second is the problem of overflow.
What is the problem with the cyclic condition? Loops are possible once and no execution, such as N>input, can be a judge of input and N, if the n<=input when the cycle, so that the problem can be avoided.
for (n = 1; n <= input; n++)
{
}
The second is that such operations like self-increment are likely to generate overflow.
Let a positive number always add 1, and the last positive number may turn negative.
Test the following code
int main () {int n = 100000;while (1) {n+=5000;if (n < 0) {printf ("%d", n); break;}} return 1;}
A negative number that has been reduced by 1 is likely to become a positive number.
Test the following code
int main () {int n = -100000;while (1) {n-=5000;if (n > 0) {printf ("%d", n); break;}} return 1;}
A positive number is always multiplied by a positive number. The final result may be negative (multiplication is implemented with addition).
Test the following code
int main () {int n = 100000;while (1) {n*=5000;if (n <0) {printf ("%d", n); break;}} return 1;}
Dividing a positive number into a positive number may result in a negative number (division can be achieved by multiplication, and multiplication is an addition)
int main () {int n = 100000;while (1) {n/=0.001;if (n <0) {printf ("%d", n); break;}} return 1;}
What is the relationship between overflow and circulation?
Or look at the code below.
for (n = 1; n <= input; n++)
{
}
Assuming that the value of input is exactly the largest integer int can represent, when the value of n is also the largest positive number, the loop can continue, after the completion of the loop body internal code, n++, this time n overflow, n becomes negative, then look at the cycle condition, a negative is always less than positive, then, N is added from a negative number until it becomes a positive number, and a positive number less than n continues to circulate, Gagaga until N produces an overflow, so that our program is not the desired result at all.
How to solve this problem? I think it is possible to detect the value of n in the loop, if n is less than 0 indicates overflow, exit the loop directly, if input this value is user input, then prompts the user to re-enter the valid data.
In the actual learning may not often encounter overflow situation, but in the actual production process may inadvertently write out the overflow code, because the logic of the program is no problem, so it is difficult to check out.
Note Points for loops