Looping Statements in C
Looping Statements in C
Loops in programming come into use when we need to repeatedly execute a block
of statements. For example: Suppose we want to print “Hello” 10 times.
This can be done in two ways as shown below:
{
printf( "Hello\n"); // prints hello for 10 times
}
Types of loops:
There are mainly two types of loops:
1. Entry Controlled loops: In this type of loops the test condition is tested before
entering the loop body. For Loop and While Loop are entry controlled loops.
2. Exit Controlled Loops: In this type of loops the test condition is tested at the
end of loop body. do – while loop is exit controlled loop.
There are 3 types of Loop in C language, namely:
1. while loop
2. for loop
3. do while loop
While loop:
while loop can be addressed as an entry control loop. It is completed in 3 steps.
Variable initialization.
Condition.
Variable increment or decrement.
Syntax :
variable initialization;
while(condition)
{
statements;
variable increment or decrement;
}
void main( )
{
int x;
x = 1;
while(x <= 10)
{
printf("%d\t", x);
/* below statement means, do x = x+1, increment x by 1*/
x++;
}
}
Output:
1 2 3 4 5 6 7 8 9 10
For loop:
statement-block;
Output:
1 2 3 4 5 6 7 8 9 10
Do while loop
In some situations it is necessary to execute body of the loop before testing the
condition.
Such situations can be handled with the help of do while loop. do statement
evaluates the body of the loop first and at the end, the condition is checked
using while statement. It means that the body of the loop will be executed at least
once, even though the starting condition inside while is initialized to be false.
General syntax is,
do
{
//body.
}
while(condition);
Example: Program to print first 10 multiples of 5.
#include<stdio.h>
void main()
{
int a, i;
a = 5;
i = 1;
do
{
printf("%d\t", a*i);
i++;
}
while(i <= 10);
}
Output:
5 10 15 20 25 30 35 40 45 50
Nested for loop
void main( )
{
int i, j;
/* first for loop */
for(i = 1; i < 5; i++)
{
printf("\n");
/* second for loop inside the first */
for(j = i; j > 0; j--)
{
printf("%d", j);
}
}
}
Output:
1
21
321
4321
54321
Jumping Out of Loops(Jumping statements)
Sometimes, while executing a loop, it becomes necessary to skip a part of the loop
or to leave the loop as soon as certain condition becomes true.
This is known as jumping out of loop.
1) break statement
When break statement is encountered inside a loop, the loop is immediately exited
and the program continues with the statement immediately following the loop.
2) continue statement
It causes the control to go directly to the test-condition and then continue the loop
process. On encountering continue, cursor leave the current cycle of loop, and starts
with the next cycle.
Write about an Infinite Loop?
An infinite loop (sometimes called an endless loop ) is a piece of coding that lacks a
functional exit so that it repeats indefinitely. An infinite loop occurs when a
condition always evaluates to true. Usually, this is an error.
Example:
#include <stdio.h>
int main ()
{
int i;
for ( ; ; ) // This is an infinite for loop as the condition
{
printf("This loop will run forever.\n");
}
return 0;
}
Output: