The nested repetition control structures Continue Statements What














- Slides: 14
The nested repetition control structures & Continue Statements
What is nested loop? ? • A loop inside the body of another loop is called nested loop. • The loop that contains another loop in its body is called outer loop. • The loop used inside the body of outer loop is called inner loop.
Syntax Outer loop { } statements; Inner loop { body of inner loop } statements;
Flowchart of nested loop condition for outer loop False True Condition for inner loop True Body of inner loop False
Example void main() { int u, i; u=1; while (u<=3) { printf(“%d”); for(i=1; i<=2; i++) { printf(“I love pakistan”); u= u+1; }
Infinite loop • sometimes called an endless loop • Infinite loop is a piece of coding that lacks a functional exit so that it repeats indefinitely. • Usually, an infinite loop results from a programming error • for example, where the conditions for exit are incorrectly written. Intentional uses for infinite loops include programs that are supposed to run continuously.
The continue statement • The continue statement can be used in the body of any loop structure. • statement is executed, it skips the remaining of loop.
Syntax & Example • Syntax: continue; • Example: int x=1; do { printf(“Welcome”); ++x; continue; printf(“Karachi”); }while (x<=5);
void main() { int u, i; for(u=5; u>=1; u--) { for(i=1; i<=u; i++) { printf(“*”); } printf(“n”); } getch(); }
Program task 1… • Write a program that displays the following output using the nested for loop. ***** *** ** *
Program task 2… • Write a program that displays the following output using the nested for loop. 1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25
void main() { int u, i; u=1; while(u>=5) { i=1; while(i<=u) printf(“%dt”, u*i++); u++; } getch(); }
Program task 3… • Write a program that displays the following output using the nested for loop. 1 1 4 9 16 25
void main() { int u, i; u=1; while(u<=5) { i=1; while(i<=u) { printf(“%dt”, i*i); i++; } printf(“n”); u++; } getch(); }