Can't understand output from this C program [on hold]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Can't understand output from this C program [on hold]



Here is the question that I attempted in a mock test.


main(){

int a;

a=1;

while(a<=10){
printf("%d",a);
if(a>3 && a<8)
continue;
a++;
}
printf("%d",a+10);
}



Options are as follows


(A) 1 2 3 4 5 6 ...................infinite
(B) 1 2 3 4 5 5 ...................infinite
(C) 1 2 3 4 4 4 ...................infinite
(D) 1 2 3 4 4 3 ...................infinite
Answer : 1 2 3 4 4 4 ...................infinite



I did not get how the answer is (C). Can anyone explain?



Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.





What is continue going to do?
– Christian Gibbons
2 days ago


continue





a starts off as 1, is printed, then incremented. When a is 2 and 3, the same thing happens. Once a hits 4, it also gets printed, but since a>3 && a<8 is true, continue will be called, skipping the rest of the while loop, so a stops getting incremented and you end up with 4 continuously being printed.
– frslm
2 days ago




a


a


a


a>3 && a<8


continue


a





Read en.cppreference.com/w/cpp/language/continue
– achal
2 days ago





use a debugger to step through.
– Sourav Ghosh
2 days ago





It seems a question to be at codegolf.stackexchange.com
– Felipe Augusto
2 days ago




3 Answers
3



C is indeed the correct answer. You need to understand the use of continue. continue statement is used inside loops. Whenever a continue statement is encountered inside a loop, control directly jumps to the beginning of the loop for next iteration, skipping the execution of statements inside loop's body for the current iteration.


continue


continue



So here in this case as the value of a reaches 4 after it gets printed the next statement if(a>3 && a<8) stands true hence continue, which skipped the rest code. In the next iteration again it does the same and will keep on doing it till infinite...


a


if(a>3 && a<8)


true


continue



Hope that explains you. Let me know if you want me to explain it further.





printf("%d",a+10); is given outside but what's the use since the prinf() inside the loop is printing the output?Correct me if I'm wrong.
– prateek parmar
2 days ago





printf("%d",a+10); would have been useful only if control actually reaches there. But the control remains in the infinite while loop. Hence this statement never executes.
– Rizwan
2 days ago



The answer is option C



When compiler encounters a continue statement then it ignore the whole code below it for that iteration.



In this case when a becomes 4 it was never incremented,so 4 gets printed infinite times after 3.



Once a is equal to 4, the continue statement is executed, which branches back to the beginning of the loop, bypassing the a++ statement. From this point on, a++ is never executed and the value of a never changes.


a


continue


a++


a++


a

Popular posts from this blog

Stripe::AuthenticationError No API key provided. Set your API key using “Stripe.api_key = ”

CRM reporting Extension - SSRS instance is blank

Keycloak server returning user_not_found error when user is already imported with LDAP