If I have a while loop (a) inside another while loop (b) and I use the break statement while in the 'b' loop, does it also break out of the 'a' loop or do I stay in the 'a' loop?
CodePudding user response:
break;
only breaks from the innermost loop or switch
body it appears in.
If you intend to break from nested loops, you might consider moving the nested loops to a separate function and using return
to exit the function from any point inside its body.
Example:
...
int matrix[ROWS][COLS];
int value;
...
int found = 0;
for (int row = 0; row < ROWS; row ) {
for (int col = 0; col < COLS; col ) {
if (matrix[row][col] == value) {
found = 1;
break;
}
}
if (found)
break;
}
...
Can be simplified as:
int hasvalue(int matrix[ROWS][COLS], int value) {
for (int row = 0; row < ROWS; row ) {
for (int col = 0; col < COLS; col ) {
if (matrix[row][col] == value)
return 1;
}
}
return 0;
}
...
int matrix[ROWS][COLS];
int value;
...
found = hasvalue(matrix, value);
...
CodePudding user response:
Does the break statement break out of multiple loops?
No:
A break statement terminates execution of the smallest enclosing switch or iteration statement.
CodePudding user response:
Documentation on the break
statement:
Causes the enclosing for, while or do-while loop or switch statement to terminate.
The enclosing statement, singular.
CodePudding user response:
It breaks out of the inner most loop that contains it
https://beginnersbook.com/2014/01/c-break-statement/
for(int i = 0; i < 10; i ){
int x = foop();
nargel();
for (int j = 0; j < 20; j ){
int k = fringle(i,j);
if(are_we_done_yet(k))
break; <<<<<<<<<<<<<<====== this goes
}
blink(); <<<<<<<<<<<<===== here
}
some languages allow you to say which loop to break out of by giving the for
a label, but not c. Java for example https://docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
CodePudding user response:
The keyword break
works only on the current loop. You can't break the outmost loop from any enclosed loop with a single break
, you'll need to set a flag in order to break at start of each loop when it becomes non-null.
It becomes way more tricky if you also use continue
and multiple break
/continue
in the same loop level, but it can be done.
Example:
int must_break = 0 ;
...
...
for (int l1=0;l1<LIMIT1;l1 ) {
...
...
for (int l2=0;l2<LIMIT2;l2 {
...
...
for (int l3=0;l3<LIMIT3;l3 {
...
...
if (INTERNAL_BREAK_CONDITION) {
// This flag will cause all outer loops to break instantly in cascade.
must_break = 1 ;
break ; // Break L3
}
}
// Must be placed just after the possibly breaking loop.
if (must_break) // Break L2
break ;
}
// Must be placed just after the possibly breaking loop.
if (must_break) // Break L1
break ;
}
// Here, you can test: if "must_break" is non-null, then a break occurred.
if (must_break)
printf("Break detected!\n");
Or you can buy yourself a warm place in Hell and use a goto
to break all loops... But obviously, it is very badly considered in most programming languages but Assembler. In C, outside low-level drivers / kernel modules, it's usually a "no-go" whatever happens. But it works... It's not maintainable, but it works.