+ 5
Why continue keyword is not giving the expected output with while loop?
I am trying to check the usage of the continue key word using while loop. It is not working correct as it working with for loop. Please help me in understanding this. https://code.sololearn.com/cXR3rK3gFRFQ/?ref=app
14 Respostas
+ 25
I'll just generalize what John Wells said.
FOR loop syntax in general is:
for ( counter declaration; exiting condition; counter increment), so you take care about everything within the parentheses.
for a WHILE loop only the condition is in the parentheses, so you have to take care of the declaration of the counter before, and for incrementing it within the body of the loop itself.
+ 12
You forgot to use i++ in the while loop.
For loop doesn't need it as i++ is there within the brackets but it is necessary in while loop so that the value of i will keep on increasing and the loop will continue untill i<10.
Use this
int i=0;
while(i<10)
{ i++;
if(i==3)
continue;
printf("%d\n",i);
}
i++ should be at the start not at the end because then when i becomes 3 i++ wouldn't be executed if it is at the end because continue forces the loop to start again. To get output from 1 to 10 use i=0 and i<10 instead of i=1 and i<=10 otherwise output would be 2 to 11.
+ 10
That code doesn't change i like the for loop does. Using continue within it, you can't write it that way, as i increment needs to be at the end, and continue won't get there. This will work:
i=0;
while(i<10)
{
i++;
if(i==3)
continue;
printf("%d\n",i);
}
+ 4
The problem with your while loop is that its missing an increment so it was printing 1's until the code overflows.
i=1;
while(i<10)
{
if(i==3) //Your code gets stuck here because i is never incremented.
continue;
printf("%d\n",i);
}
//The below code will give you the same result as the for loop.
i=1;
while(i<=10)
{
if(i==3){
i++;
continue;
}
printf("%d\n",i++);
}
see my answer below.
https://code.sololearn.com/c3hgTOZuyC1G/#c
+ 3
It is doing exactly what you told it to do. What your code is doing is saying when i == 3 do not execute anything in the loop, just move on to the next number. And it does that. It does not print 3...
So maybe you didn't really code what you wanted
+ 2
Thank you cyk, but please check the code in comments. That code is not working as like for loop.
+ 2
dude idk what you doing but I must increment so check this code
https://code.sololearn.com/ckIQ4v9JAIN0/?ref=app
+ 2
check out your while loop condition and also check the condition when the continue will execute...I think you can solve your problem easily by doing this. If not then please check the syntaxes one more time.
+ 1
by using continue statement in while loop just u can skip the successive statements so you can't use the control. upto the end of program
+ 1
See , in for loop syntex u include a i++ for the increment of i inside d brackets but in while loop syntex u need to manually increment the value of i inside the parenthesis.
+ 1
Try this code I hope you get success.
$foo = 'Hello';
for ($p = 0; $p < 8; $p++) {
switch($p) {
case 3:
if ($foo === 'Hello') {
echo $foo;
break;
} else {
continue 2;
}
default:
echo "Sleeping...<br>";
continue 2;
}
echo "World!";
break;
}
https://babasupport.org/printer/apple-printers-customer-support/619
+ 1
you have not increased the value of i anywhere within the while loop. Try it once. before the end of loop add i++;
+ 1
your code might be facing some issues try to check step by step