Loops in C
Loop control statements are used to execute a statement or a group of statements multiple times. Suppose you want to print a number 10 times. Instead of using 10 printf statements, we can simply loop over a single printf statement 10 times. Let's see the looping constructs supported by C.' for ' Loop
Consider the following code snippet :
i = 0; is the initialization statement.
i <= 10; is the condition statement.
i++; is the increment statement.
The initialization ( i = 0; ) statement is executed once.
Then, the condition ( i < 10; ) statement is checked. If it evaluates to True, body of the loop is executed. If it evaluates to False, loop is terminated and the statement after the loop body is executed.
After all the statements in loop body are executed, control jumps to the increment statement and the condition statement is evaluated again.
Every for loop follows this syntax :
for loop is known as entry controlled loop since the condition check is done at beginning of the loop.
Following program illustrates the use of for loop :
----------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------
Output :
Multiplication Table for 5 :-
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
----------------------------------------------------------------------------------------------------------------------------
' while ' loop
Syntax of the while loop is :
Initialization statement is declared just before the while statement.
Increment / Decrement statement is present inside the body of while loop.
while loop is also an entry controlled loop. It is possible that the body of the while loop is not executed even once.
Following program illustrates the use of while loop :
----------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------
Output :
Multiplication Table for 7 :-
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35
7 * 6 = 42
7 * 7 = 49
7 * 8 = 56
7 * 9 = 63
7 * 10 = 70
----------------------------------------------------------------------------------------------------------------------------
' do ... while ' loop
Syntax of the do ... while while loop is :
' do ... while ' loop is know as exit controlled loop since the condition check is done at the end of the loop.
Following program illustrates the use of do ... while loop :
----------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------
Output :
Multiplication Table for 3 :-
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30
----------------------------------------------------------------------------------------------------------------------------
' goto ' statement
' goto ' statement is used to break the current execution flow of a program and jump to a specified label in a program. This can be used as another technique to implement loops. However it is recommended to use for, while, do ... while loops only. Following program illustrates the use of ' goto ' statement :
----------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------
Output :
Multiplication Table for 9 :-
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
----------------------------------------------------------------------------------------------------------------------------
Nested Loops
We can use a loop inside the body of another loop. This is known as nesting of loops.
Following program illustrates the use of nested loops:
----------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------
' break ' and ' continue ' statement
' break ' statement is used to terminate the loop immediately without going for further conditional check. In case of nested loop, the innermost loop is terminated and the execution starts from the next line of the program after the inner loop. We have already seen that break is used to break out of switch case construct.
' continue ' statement doesn't terminate the loop but goes to the beginning of the loop for next iteration skipping rest of the loop body.
Following program illustrates the use of break statement :
----------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------
Output :
0 1 2 3 4 5 6
----------------------------------------------------------------------------------------------------------------------------
The for loop is supposed to have 10 iterations printing 0 1 2 3 4 5 6 7 8 9 but the break statement causes termination of the loop once i becomes 7. So the output will be 0 1 2 3 4 5 6 .
Following program illustrates the use of continue statement :
----------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------
Output :
1 3 5 7 9
----------------------------------------------------------------------------------------------------------------------------
Output of the above program is 1 3 5 7 9 . The print statement is skipped using the continue statement when we see an even number.
No comments:
Post a Comment