A nested loop is a loop within a loop, an inner loop within the body of an outer one.
Working:
- The first pass of the outer loop triggers the inner loop, which executes to completion.
- Then the second pass of the outer loop triggers the inner loop again.
- This repeats until the outer loop finishes.
- A break within either the inner or outer loop would interrupt this process.
The Function of Nested loop :
OutputGFG! GFG! GFG!
GFG! GFG! GFG!
GFG! GFG! GFG!
Label the loops:
This is how we can label the name to the loop:
labelname :for(initialization;condition;incr/decr){
//code to be executed
}
OutputGFG!
GFG!
GFG!
GFG!
GFG!
Rules to Label the loops:
- Label of the loop must be unique to avoid confusion.
- In the break statements use labels that are in scope. (Below is an implementation)