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.
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
}
}
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
}
}
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 !
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.
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.