A loop is a construct used to execute a set of statements repeatedly. In C language, we have 3 kinds of looping constructions.
Though syntactically different, the purpose of these three is same.
Different Types of Loops In C Programming
Most common used Loops are as follows:
- While Loop
- Do while Loop
- For Loop
Rest of the Loops as follows:
- Infinite Loops
- Nested Loops
- Semicolon as loop body
- Local Variables in a Loop
- Using Break
- Using Continue
While Loop – Tutorial With Examples
This loop is used to execute a set of statements repeatedly as long as a condition is true.
Syntax:
1 2 3 4 5 6 7 8 9 |
while(condition) { // some code // some code } |
When control comes to this loop, the condition in the header is evaluated to check whether it results in true(1) or false(0).
- If the condition is true, the control enters the loop body and executes the statements in it.
- After executing the statements, the control comes back to the condition part and checks the condition.
- If the condition results in true, the control enters the body again to execute the statements in it.
- Then the control goes to condition part again.
This process is continued as long as the condition results in true. When the condition results in false, the control goes to the statement immediately following the loop.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
int a=1,b=3; while(a<=b) { printf(“dennis”); a++; } printf(“ritchie”); |
In the above example, by the time control comes to the header part of the loop, a is 1 and b are 3 so the condition a<=b results in true. So the control comes into loop body and prints “
- So the control comes into loop body and prints “Dennis”. Then the value of a is incremented by 1 so it becomes 2.
- Then the control comes-up to the condition part automatically and checks, as the condition results in true again, the body executes and prints “Dennis” again.
- Then a becomes 3.
- Then the control comes to condition part again which results in true.
- So the loop body is executed third time to print “Dennis” and to increment a by 1.
- This time a is 4 so the condition results in false (4<=3). As the condition is false, the control comes to the statement after the loop and prints “Ritchie”.
When the loop body is executed for N number of times the condition is checked for N+1 number of times. For the first N times, the condition results in true and the (N+1)th time the condition results in false.
Do While In C – Tutorial With Examples
Do while is almost same as while loop but the condition is checked at end of each iteration (condition is checked at beginning of each iteration in while loop).
In do-while the loop body is executed once even though the condition is false for the first time (when the condition is false for the first time the loop body is not executed at all in while).
Syntax:
1 2 3 4 5 6 7 8 9 |
do { // some code // some code } while(condition); |
When control comes to this loop, without any condition checking, it enters into loop body and executes it.
- Then it comes to condition part and evaluates it.
- If it results in true, the control goes back to loop body and executes it.
- Then it comes to condition part.
- This process of ‘executing body’ and ‘condition checking’ is repeated as long as the condition results in true.
- When the condition results in false the control come out of the loop and execute the statement immediately following the loop.
Example:
1 2 3 4 5 6 7 8 9 10 11 |
int a=1; do { printf(“%d “,a); a++; } while(a<=3); |
In the above example, the value of a is 1 by the time the control comes to the loop.
- Without any checking, the control enters the loop body and prints 1.
- Then it is incremented by 1. From there it goes to condition part and evaluates it.
- As it results in true (1<=3), the control goes back to loop body and prints 2 and then makes a value as 3.
- The next time condition is checked, it again results in true (2<=3).
- So control goes to the body to print 3 and to update a to 4. This time the condition will result in false (4<=3) so the control comes out of the loop.
We can have any number of statements in a do-while loop and all those statements should be enclosed within a set of curly braces.
Example:
1 2 3 4 5 6 7 8 9 10 11 |
do { statementA; statementB; } while(condition); |
The curly braces are optional when we have only one statement as part of the body. So the following examples will mean same.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
do statementA; while(condition); Eg2: do { statementA; } while(condition); |
Generally, a construct/statement ends with a semicolon or a closing curly brace but not with a closing parenthesis.
This is why a do-while construct is ended with a semicolon, and it is required.
For Loop In C – Tutorials With Examples
“For” is themosty used loop out of the three loops probably. Whatever we can do with a while loop we can do it with a for loop also and of course with a do-while also.
Syntax:
1 2 3 4 5 6 7 8 9 |
for(initialization; condition; incr/decr) { // some code // some code } |
When control comes to the for loop, it executes the initialization part first. This part is executed only once in the life time of the loop.
- The one-time activities associated with the loop (that too at the beginning) are done here.
- Then control moves to condition part. In case the condition results in false the control come out of the loop and execute the statement immediately following the loop.
When the condition is true, the loop body is executed. This is where the actual logic of the loop is executed. The control comes to incr/decr part and the loop control variables are incremented/decremented.
After executing the incr/decr part, the control comes to condition part. If it results in true, the body is executed again, then goes to incr/decr and then to condition part. This process is repeated as long as condition results in true.
Example:
1 2 3 4 5 6 7 |
for(int a=1; a<=3; a++) { printf(“have a physical activity regularly”); } |
In the above example, in the initialization part, the variable a is initialized to 1. This activity takes place only once. Then it moves to condition a<=3 which results in true.
- So it comes to loop body to print “have a physical activity regularly” and then it moves to increment a value by 1 so that a becomes 2.
- When it comes to condition part, it results in true, so prints the message again and comes back to increment to make a value to 3.
This time also the condition is true, so prints the message in the body and then comes to make a value to 4.
- Then the condition results in false (as 4<=3 is false) and comes out to execute the statement after the loop.
- In a for loop, initialization is executed only once irrespective of a number of times the loop is executed.
- The condition is checked N+1 times where N is the number of times the loop body is executed.
- Incr/decr part is executed N times (same as the number of times the loop body is executed).
We can have any number of statements in a for loop body. All of them should be enclosed within curly braces. If we have only one statement, then the curly braces are optional.
Example:
1 2 3 4 5 6 7 8 9 |
for(b=2.3; b<=10; b+=1.2) { printf(“hi”); printf(“mani”); } |
Example – 2:
1 2 3 4 5 6 7 |
for(c=’A’; c<=’Z’; c++) { printf(“ %c“,c); } |
The initialization part can have any number of statements, but all of them should be separated by a comma.
When initialization ends, then that part should be ended with a semi-colon. In the following example, three statements (a=10), (b=20) and (c=30) are placed in the initialization part separated by commas.
Example:
1 2 3 4 5 6 7 |
for(a=10, b=20, c=30; a+b <= c ; a++) { StatementA; } |
The incr/decr part can also have multiple statements separated by commas.
But the condition part should not be separated by commas.
All the supposed conditions should be combined using AND or OR operators to make it a single condition (if we put multiple conditions separated by commas then truth value of the last condition only is considered as truth value of the entire condition part).
- The condition should always result in a true(1) or false(1) value. If other values are given their logical value is considered as the truth value.
- Generally, the header part of the for loop decides (with initialization, condition, and incrementation) how many times the loop will be executed and the actual logic is placed in the body.
In case the header part itself has the actual logic implicitly then the body may not be required.
In that case, we can have a dummy body. We have two examples below, the first one has a set of curly braces without any code inside. In the second example, just a semi-colon is the body of the loop.
Example: 1
1 2 3 4 5 |
for(a=9; a<=b; a++) { } |
Even though it is named to be initialization, condition and incrementation parts, we can write any statement in that part.
The condition part should result in a logical value. At initialization, we can create variables but not at incrementation part. The following code is valid.
Example:
1 2 3 4 5 6 7 8 9 |
for(printf(“passion”); x>=y; printf(“result”) ) { x++; printf(“hardwork”); } |
In the example, a printing statement is placed in initialization part, another printing statement is placed in incrementation part. The actual incrementation is in the loop body. The actual initialization might have been done before the loop starts. All these are valid.
The actual incrementation is in the loop body. The actual initialization might have been done before the loop starts. All these are valid.
Sometimes the initialization takes place by the time control comes to the for loop, in that case, we can write a dummy statement (just a semicolon) at initialization part.
Similarly, we can skip the incrementation part. If we use a dummy condition (just a ; ) then it is treated as true. So all the following usages are valid.
Example:
1 2 3 4 5 |
int a=1; for( ; a<=3; a++) statementA; |
Example: 2
1 2 3 4 5 6 7 8 9 |
for(int a=1; a<=3; ) { statementA; a++; } |
Example – 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
int a=1; for( ; ; ) { if(a>3) break; statementA; a++; } |
If we write a for loop like for(;;) and there is no break or return kind of statements then that loop becomes an infinite loop. The following are infinities.
- Eg1: for( ; ; )
- Eg2: for(init; true; incr)
Infinite Loops – Tutorial With Examples
Sometimes we write a looping construct as if the condition results in true always. Such loops are known as infinite loops or indefinite loops.
Example:
1 2 3 4 5 6 7 8 9 |
while(1) { StatementA; StatementB; } |
Example – 2
1 2 3 4 5 6 7 8 9 |
while(2<4) { StatementA; StatementB; } |
This kind of loops is used when we don’t know exactly when to come out of the loop.
At the middle of the loop body, we may need to come out depending on a resultant value after an expression is evaluated or a user entered value met a condition. When such a situation arises we can come out using a statement like ‘break’ or ‘return’.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
while(condition) { // some code // some code if(some situation) break; // some code // some code } |
Condition Should Result In Logical Value
The condition in the header should always result in a logical value (true or false). No other value is allowed (similar to the condition in if header). Any value other than 0 is treated as true logically.
Example:
1 2 3 4 5 6 7 8 9 |
while(3<7) is true while(9) is true while(4+8) is true while(1) is true while(0) is false |
Nested Loops:
We can have a looping construct inside another looping construct. This kind of arrangement is known as nested looping.
Example:
1 2 3 4 5 6 7 8 9 10 11 |
while(condition1) { while(condition2) { } } |
There is no limit on the depth of nesting.
We can have any other loop inside a while loop. In fact, any loop can have any other loop as part of its nesting. Not only a loop, any other construct (if, switch, etc) can be placed in a loop.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
while(a<b) { for(c=a;c<d;c++) { while(d<e) { switch(d) { case 3: statement1; break; case 6: statement2; break; default: statement3; break; } d++; if(d>=f) statement4; } } } |
Infinite Loops – Tutorial With Examples
We can create an infinite loop by placing a condition that results in true always.
Example:
1 2 3 4 5 6 7 8 9 10 11 |
do{ //some code }while(1); Eg2: do{ //some code }while(2<5); |
Semicolon As Loop Body
If we put a semi-colon at the end of a while expression, that semicolon becomes the body of the loop. In that case, anything after the semicolon becomes out of the loop.
Example:
1 2 3 4 5 |
int a=10; while(a++ < 13) ; printf(“hello”); // this statement is outside the while loop |
- eg2: do while(m==n); is not valid
- eg3: do ; while(m==n); is valid
- eg4: do printf (“don’t sit and work continuously”); while(m==n); is valid
- eg5: do { printf (“don’t sit and work continuously”); } while(m==n); is valid
- eg6: do { } while(m==n); is valid
Local Variables In A Loop
We can create variables inside a loop, but they can be used only within that loop. They are automatically destroyed when the loop ends. So they cannot be used outside the loop.
Condition is required
do { } while(); is not valid
do { } while(a<=b); is valid
the “do” should have “while”
Eg: do { } is not valid
do { } while(condition); is valid
Using “break”
To come out of a for loop from the middle of the body, we can use break. The break will take the control out of the loop without regard to the condition in the header.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
for(a=16; a<=i; a+=3) { // some code if(condition) break; // some code } |
Using “continue”
If we want to skip remaining statements of a loop in the current iteration, then we can use ‘continue’ statement in the middle of a loop body. Then the control automatically goes to the next iteration. The next time (in the next iteration) all the statements of the loop body may get executed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
while(condition) { // some code // some code if(somesituation) continue; // some code // some code } |
If we want to go the next iteration of a loop from the middle of the body (by skipping the remaining statements of current iteration) then we can use continue.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
for(a=17; a<=j; a*=2) { // some code- if(condition) continue; // some code- } |