0
Continue statement in c
https://code.sololearn.com/cyW1Bp0lTkoz/#c Can anyone explain me how the continue statement works here?? With detailed explanation
8 Respuestas
+ 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;
}
+ 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
+ 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
+ 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.
+ 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
+ 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.
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
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.