Get out from nested for loops

Hi,

1
2
3
4
5
6
7
8
9
10
11
for(i=0;i<10;i++)
{
   for(j=0;j<10;j++)
   {
       if()
       {
       Now I want to exit from j loop and continue from i loop.
       }
       And now if it is wrong I want to continue from j loop.
   }
}


Actually I tried break in if statement but it did not help.

Last edited on
Have you read about keyword continue? if you want to break from inner loop only and move on to i, use continue statment and if you want t and if its wrong and you want to continue from j loop only, do nothing and let the control flow.
1
2
3
4
5
6
7
8
9
10
11
for(i=0;i<10;i++)
{
   for(j=0;j<10;j++)
   {
       if()
       {
       continue; //it will skip rest of the cycles j loop and will take control to i loop.
       }
       // do nothinh here and let the control flow normally since you want to continue from j loop only
   }
}


Makes sense?
in your example only inner loop (j loop) is affected by continue.

I do not want to enter the j loop second time. After first time just continue from next i.

So, continue increases to next j but I want to exit.
You tried break?
airerdem, you should post your usage of break, it should of worked. Here is an example:

1
2
3
4
5
6
7
8
9
10
	for ( int i = 0; i < 10; i++ )
	{
		cout << "In i loop " << i << endl;
		for ( int j = 0; j < 10; j++ )
		{
			cout << "In j loop " << j << endl;
			if ( j == 6 )   // or whatever condition, if the condition is false, the j loop continues
				break; // break out of j loop
		}
	}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for(i)
{
	for(j)
		{
			if ()
			{
					for()
					{
					something
					}
				break;
			}
		}
}


actually it worked one time in j loop and stopped. (Didn't pass second i value)
sorry, your question is not clear.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for(i=0;i<10;i++)
{
	for(j=0;j<10;j++)
		{
			if (a>b)
			{
                        cout << "a>b";
					for(k=0;k<10;k++)
					{
					cout <<k;
					}
		        break;
			}
		}
}


I mean if a>b then I want to stop j cycle (after cout k values) and increase i. If it is not I want to increase j until a>b. But in this code it just enters the both loop one time and program stops !
Last edited on
1. Where are the values of a and b changing. I dont see it anywhere. So loop will function the same way every time.
2. If you want to break after cout<<k; and stop j cycle, use break twice, once in k loop and another one in j loop after k loop, like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
for(i=0;i<10;i++)
{
	for(j=0;j<10;j++)
		{
			if (a>b)
			{
                        cout << "a>b";
					for(k=0;k<10;k++)
					{
					cout <<k;
                                        if (a>b)
                                        break;
					}
		        if (a>b)
                        break;
			}
		}
}


sidenote: I am quite curious to know what exactly are you trying to achieve with this code, which, imo, is doing nothing.
1. a and b are just example I want to know the logic.

2. in this time only first value of k will be printed, but I want to stop after all k values are printed.

and I appreciate your help ;)
try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for(i=0;i<10;i++)
{
	for(j=0;j<10;j++)
		{
			if (a>b)
			{
                        cout << "a>b";
					for(k=0;k<10;k++)
					{
					cout <<k;
                                        }
		        if (a>b)
                        break;
			}
		}
}

Let me know if it works and also if it does not work. :P
I would expect break to work. Perhaps execution is not reaching the statement.

Alternatively, dare I say that nested breaking might be a fair case to consider using goto. Assuming that break doesn't fit your needs and adding flags at each scope is not practical.
ok the problem is solved as anonymouscoder6 explained.

thank you
Congrats Airedem. :)
Topic archived. No new replies allowed.