Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Explanation of circular structure in c language 5
Explanation of circular structure in c language 5
In a, for int I =100; I = I % 100+ 1; The update of the loop variable I is I+1 to 100. The result of any shaping of n is between [0, 100], even if+1, I will never be greater than 100, so it is an infinite loop.

B. A typical infinite loop without loop variable control has an empty statement in the loop body and only one empty statement ";" . This is an endless cycle of doing nothing.

C. I'm afraid the bug in this question is in option C. From the questioner's point of view, the initial value of k is 1000. Do while is characterized by {++ k;; }, and then check the cycle conditions (k>= 1000). After the loop runs ++k for the first time, k is already greater than 1000, which meets the conditions for loop continuation. Therefore, the author believes that in the subsequent cycle, K will continue to increase and K will never be less than 1000. So the questioner thinks this is also an infinite loop.

D.s is initialized to 36, and -s is executed once per loop, and the content of s will decrease by 1.while(s), that is, when s is not zero, the loop body is executed. S automatically decreases in each cycle, starting from 36, and after 36 times of execution, S is 0, which no longer meets the cycle conditions. So the loop can end, not an infinite loop.

note:

1 . while(exp){ stat; }

A) check the value of exp.

B) When the expression exp is a non-zero value, execute the stat statement in the loop body and return a); When the value of the expression exp is 0, the loop ends.

2.do { stat} while(exp);

A) execute the loop statement stat, and then check the value of exp;

B) if the value of exp is not zero, returning to a) to continue the loop; If exp is zero, the loop ends.

3 . for(exp 1; exp2exp3){ stat; }

A) before executing the loop for the first time, execute exp1;

B) Check the value of exp2, and if exp2 is not zero, execute c); If exp2 is zero, the loop ends;

C) Execute the stat statement in the loop body, then execute exp3, and then return to b)

Questioner p.s. made an error in option c in the question. In fact, the signed plastic int will become a negative minimum after increasing to the positive maximum that int can represent. This behavior is called int overflow. Then k will suddenly become negative in the process of self-increasing, less than 1000, and the cycle will end. But the reason for the termination is what I said, not the logically expected way of termination. For details, if you are interested, go through the books or ask the teacher, or ask me ...