Question

I have made a sample code in c.

do
{


 switch(i)
 {

 case 1:
 {
   cout<< "1\n";
   break;

 }
 case 2:
 {
      cout<< "1\n";
      break;
  }
  case 3:
  {
    continue;
  }


 }
 }while (0);

Here in this code if value of i is 3 then i want to restart the do while loop which will result in an infinite loop. But unfortunately loop is not getting executed again. What will be the reason for that.? I have checked the assembly code in visual studio of the same code and found that there is no JMP statement in the assembly for continue statement inside switch -case .

Was it helpful?

Solution

while (0) means it will fall out the bottom of the loop. Even if you have continue the condition is re-eval'd

OTHER TIPS

When you do continue inside a do-while loop, it jumps at the evaluation at the bottom, that is always false in your case.

The other answers are explaining continue but it looks from your comments like you understand how that works.

if value of i is 3 then i want to restart the do while loop which will result in an infinite loop

The code is functioning as you describe - it is jumping to the 'while' condition on i==3. The only difference is that if you want an infinite loop you need while(1) not while(0).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top