current position:Home>Operation of if function and for function

Operation of if function and for function

2022-02-03 01:30:09 CSDN Q & A

include<stdio.h>
main()
{ int a=1,b;
for(b=1;b<=10;b++)
{ if(a>=8) break;
if(a%2==1) {a+=5; continue;}
a-=3;
}
printf("%d\n",b);
}
Why does the program run with an output of 4




Refer to the answer 1:
a b
6 2
3 3
8 4

On the third cycle a>=8 Established direct break End of cycle

a-=3 Not every time




Refer to the answer 2:



Refer to the answer 3:

break Exit the whole cycle
continue Exit this cycle , Continue the next cycle
break and continue No subsequent code is executed

Execute before the first round b = 1; Judge b <= 10
if (a >= 8) dissatisfaction Don't execute
if (a % 2 == 1) Satisfy perform { a += 5; continue; } here continue 了 The following code will not be executed ( Here it means a -= 3;) Go straight to the next cycle At this time a=6

Execute... Before the second cycle starts b++ At this time b = 2 Judge b <= 10
if (a >= 8) dissatisfaction Don't execute
if (a % 2 == 1) { a += 5; continue; } dissatisfaction Don't execute
Then perform a -= 3; here a = 3

Execute... Before the third cycle starts b++ At this time b = 3 Judge b <= 10
if (a >= 8) dissatisfaction Don't execute
if (a % 2 == 1) { a += 5; continue; } Satisfy perform { a += 5; continue; } here continue 了 The following code will not be executed ( Here it means a -= 3;) Go straight to the next cycle At this time a=8

Execute before the fourth cycle b++ At this time b = 4 Judge b <= 10
if (a >= 8) Satisfy Carry out the following break; Then exit the whole cycle
Perform the last cardiopulmonary bypass printf("%d\n",b); here b = 4




Refer to the answer 4:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.primo.wiki/2022/02/202202030130068557.html

Random recommended