English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

How will be the effect of 'break' statement in nested for loops?

2006-10-22 20:03:46 · 3 answers · asked by Visakhan C 1 in Computers & Internet Programming & Design

3 answers

break statement is used to come out from the innermost loop.
say you are using three loop variables like this
for(i=1;i<=10;i++)
{
if(i%2 ==0) break;
cout< for( j=1;j<=10;j++)
{
if(j%2==0) break;
cout< for(k=1;k<=10;k++)
{
if(k%2==0) break;
cout< }

}
}
Now output of this loop is as follow
when i is odd it will enter the j loop otherwise it will exit from the i loop and execute the instruction just after the i loop.
and if it entered in j loop then it will come out from the j loop when it become odd. and same in the case of k.
so output is
1
1
1
because k is break at k=2 then j becomes 2 and it break it and then i become 2 and exit from all loop

2006-10-22 21:52:40 · answer #1 · answered by rohit 1 · 0 0

if u have break in nested loops,be sure of where u r placing the break.
ex:-
if(x==10 || 20)
{
if(x==10)
{printf("%d",x);
break;
}
break;
if(x==20)
printf("HI");
}
this will break the loop itslef after the 1st print statement.

2006-10-22 20:21:06 · answer #2 · answered by krishna 4 · 0 0

Visakhan C This link will help you

http://www.google.co.uk/search?hl=en&q=use+c%2B%2B+%27break%27+code+in+nested+for+loop&btnG=Google+Search&meta=

2006-10-22 21:36:27 · answer #3 · answered by Joe_Young 6 · 0 0

fedest.com, questions and answers