Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
When I'm using (goto) to exit from internal loop to go to parent loop to complete the loop, which keyword , or which words can i use ?

for example :

for(i=0 , i <= 100 , i++)
{
for(j=0 , j < 20 , j++)
{
if (j = 5)
goto .........?
}
}
Posted
Updated 12-Mar-12 8:08am
v2
Comments
shreekar 12-Mar-12 14:39pm    
Please post the updated code with the break statement used. Use some debug.print or such to show the execution flow and where it is going wrong.
P.S.: I am not sure if j=5 is correct for equality checking in C#
Martin Arapovic 12-Mar-12 14:53pm    
...and in for loops instead "," ";" needs to be used... Syntax is incorrect...
[no name] 12-Mar-12 20:18pm    
if (j == 5)

I would just use the command

break;


where your goto statement is.

--Edit:

Are you talking about this?

bool breakUsed = false;

for(i=0;i<=100;i++)
{
   if(breakUsed == false)
   {
      for(j=0;j<20;j++)
      {
         if(j==5)
         {
            breakUsed = true;
            break;
         }
      }
   }
}
 
Share this answer
 
v2
Comments
aodeh 12-Mar-12 14:19pm    
inside the internal loop , and i need to exit just from internal loop and goto parent loop to complete it.

when such a condition happen in internal loop , i need to goto parent loop to complete it , not to complete the sequence of execution ..
Tim Groven 12-Mar-12 15:14pm    
I udpated my answer. Is that what you mean?
aodeh 12-Mar-12 16:05pm    
yes exactly , really thank you man
You're looking for the break[^] statement.

You don't need goto at all.
 
Share this answer
 
Comments
aodeh 12-Mar-12 14:33pm    
break statement , exit from internal and parent loop then complete the sequence of execution.
aodeh 12-Mar-12 14:34pm    
i need to go back to parent loop to complete the loop , when i used break or continue the result is : exit from parent and internal loop then complete the sequence of execution .
Dave Kreskowiak 12-Mar-12 14:56pm    
Try again. Break will only get out of the INNER loop. It will not exit the outer loop. The outer loop will keep running, and thereby start the inner loop again. The way you're code is structured, you cannot nest a looop inside a loop and then break the inner loop and have the outer loop code skip the inner loop for the duration of the outerloop.

You'll have to put a flag in the outer loop code that check to see if it should run the inner loop at all if that's what you want. Set that flag right before the break statement in the inner loop code.
shreekar 12-Mar-12 15:01pm    
I think he has shown just an example with pseudo code. I have added a comment to the original question requesting the actual code with the break statement implemented.
Sergey Alexandrovich Kryukov 12-Mar-12 15:18pm    
Sure, a 5. I would ask OP what's the goal of the whole fragment of the code. Maybe, the whole idea is wrong. It's always a good idea to start with requirements.
--SA
Hi,
Use break; for exit loop and continue; to iterate to the next element. Check this[^] link.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900