Professional Documents
Culture Documents
C supports nesting of loops in C. Nesting of loops is the feature in C that allows the looping of
statements inside another loop.
Any number of loops can be defined inside another loop, i.e., there is no restriction for defining
any number of loops. The nesting level can be defined at n times. You can define any type of
loop inside another loop; for example, you can define 'while' loop inside a 'for' loop.
Outer_loop
{
Inner_loop
{
// inner loop statements.
}
// outer loop statements.
}
Outer_loop and Inner_loop are the valid loops that can be a 'for' loop, 'while' loop or 'do-while'
loop.
The nested for loop means any type of loop which is defined inside the 'for' loop.
#include <stdio.h>
int main()
{
int n;// variable declaration
printf("Enter the value of n :");
// Displaying the n tables.
for(int i=1;i<=n;i++) // outer loop
{
for(int j=1;j<=10;j++) // inner loop
{
printf("%d\t",(i*j)); // printing the value.
}
printf("\n");
}
o First, the 'i' variable is initialized to 1 and then program control passes to the i<=n.
o The program control checks whether the condition 'i<=n' is true or not.
o If the condition is true, then the program control passes to the inner loop.
o The inner loop will get executed until the condition is true.
o After the execution of the inner loop, the control moves back to the update of the outer
loop, i.e., i++.
o After incrementing the value of the loop counter, the condition is checked again, i.e.,
i<=n.
o If the condition is true, then the inner loop will be executed again.
o This process will continue until the condition of the outer loop is true.
Output:
C break and continue
break statement in C
In all the C loops we have a way to break out of a loop at any point in time, immediately, regardless
In simple words, The break statement is a loop control statement which is used to terminate
the loop immediately.
Basically break statements are used in the situations when we are not sure about the actual
number of iterations for the loop or we want to terminate the loop based on some condition.
How break statement works?
1234
I In the above example, a for loop is used to print the value of i in each iteration. In which
a if statement is used with break statement in it.
T he condition of if statement is i == 5 i.e. when i is equal to 5 and break statement is executed to
terminate the loop. Hence, the output doesn't include values greater than or equal to 5.
continue statement in C
The continue exactly as the name suggests. Since we use this in loops, it will skip over the remaining
bbody of the current loop, and continue to the next iteration.
We can use this inside any loops like for, while, or do-while loops.
1. The continue statement is used to skip the remaining portion of a for/while loop.
2. we continue onto the next iteration, and move to the loop condition check.
3. This is quite useful for programmers to be flexible with their approach in control loops.
How continue statement works?
1 2 3 5 6 7 8 9 10
IIIn the above program, we have used the the for loop to print the value of i in each iteration. Here,
notice the code,
if (i == 4)
{
continue;
}
When i is equal to 4, the continue statement skips the current iteration and starts the next
iteration
Then, i become 5, and the condition is evaluated again.
Hence, 5 and 6 are printed in the next two iterations.
Loop prints the value of i until it became 11 and condition becomes false. Then, the loop
terminates.