0

Continue statement in c

https://code.sololearn.com/cyW1Bp0lTkoz/#c Can anyone explain me how the continue statement works here?? With detailed explanation

1st Oct 2018, 12:55 PM
Dhiviya Thirumavalavan
Dhiviya Thirumavalavan - avatar
8 Answers
+ 9
Continue statement skips one iteration of the loop. Here's a little code, run it and you will understand what continue does #include <iostream> using namespace std; int main() { for(int i = 0; i <= 100; i++) { if(!(i % 2 == 0)) continue; cout << i << " "; } return 0; }
1st Oct 2018, 8:25 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 6
https://www.google.co.in/amp/s/www.geeksforgeeks.org/continue-statement-cpp/amp/ As it states "instead of terminating the loop, it forces to execute the next iteration of the loop" , the continue statement just calls for next loop. So if your loops Case 1: a= 1 , b = 1 , c = 1 and the condition of if ( a=b || b=c || c= a) is satisfied so it calls for next loop. Now this goes on till.. Case 2: a= 2 , b =2 , c= 2 and once again the condition is satisfied and the continue statement calls for next loop. Eventually breaking out of loop without printing anything. You can just change c<= 3 and see the changes for printf("%d\t %d\t %d \n",a,b,c); //in else loop
1st Oct 2018, 1:25 PM
Frost
Frost - avatar
+ 6
Dhiviya Thirumavalavan , my bad i did it by mistake (= in place of ==) but the explanation remains same. when first loop runs a=b=c = 1 so first loop skips.. When second loop runs (inner most) a = 1 b = 1 c= 2 So ur if ( a== b || b==c || c==a) is true and hence continue is done (i.e skips the loop) When new loop runs, a=1 b=2 c = 1... So u see , Everytime your condition of a==b || b==c || c==a is true resulting the continue statement be executed. So the loop skips everytime and nothing is ever printed. If you want to see some print, just change c<= 2 to c<=3 , you will get a better idea. See a bit modified version https://code.sololearn.com/cXD6T4oTUyqO/?ref=app
3rd Oct 2018, 6:37 AM
Frost
Frost - avatar
+ 3
Dhiviya Thirumavalavan do you understand the code that i posted in answer.. that gives the actual values of a,b,c when the loop runs like 1,1,1 then 1,1,2 then 1,2,1 and so on.... No your condition : a==b or b==c or c==a is true for 1,1,1 Is true for 1,1,2 Is true for 1,2,1 And so on As it is true, it will execute continue; statement which means skipping that loop and as a result nothing is printed. Please refer the web link i provided in 1st answer.
3rd Oct 2018, 7:34 AM
Frost
Frost - avatar
+ 3
Dhiviya Thirumavalavan Change for(a=1;a<=2;a++) to for(a=1;a<=3;a++) for all three variables a, b and c. And note that you don't print the variable c in line 27
3rd Oct 2018, 7:46 AM
Anna
Anna - avatar
+ 1
In addition to Frost's answer, the way you use the continue statement in your code doesn't really make sense. You could remove it or comment it out and the code would do exactly the same. You use it like this: if(condition) { continue; } else { // do something; } The else clause will only be executed if the condition is false, so you could shorten the whole thing to: if(!condition) { // do something; } If you really want to use the continue statement, there is no need for else. You could simply do this: if(condition) continue; // do something; Here, if the condition is true, it will continue to the next iteration. "do something" will only be executed if the condition is false.
1st Oct 2018, 1:47 PM
Anna
Anna - avatar
0
Frost pls check my code once.. the condition is a==b|| b==c || c==a not a=b || b=c || c=a.. pls can you see it once and explain me
3rd Oct 2018, 4:25 AM
Dhiviya Thirumavalavan
Dhiviya Thirumavalavan - avatar
0
Frost My code is about to generate combination of numbers while entering 1 2 3 ( Sorry for saying late) As a fact I can"t understand what you are saying.
3rd Oct 2018, 7:27 AM
Dhiviya Thirumavalavan
Dhiviya Thirumavalavan - avatar