Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C Looping Statement

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

LOOPING STATEMENT

Looping statement are the statements execute one or more statement


repeatedly several number of times. In C programming language there are
three types of loops; while, for and do-while.

WhY TO use loop ?

When you need to execute a block of code several number of times then you
need to use looping concept in C language.

ADVANTAGE WITH LOOPING STATEMENT

 Reduce length of Code

 Take less memory space.

 Burden on the developer is reducing.

 Time consuming process to execute the program is reduced.

The sequence of statements to be executed is kept inside the curly braces {


} known as the Loop body. After every execution of the loop body, condition is
verified, and if it is found to be true the loop body is executed again. When the
condition check returns false, the loop body is not executed, and execution
breaks out of the loop.

TYPES OF LOOPING STATEMENT


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.(e.g int x = 0;)


 condition(e.g while(x <= 10))
 Variable increment or decrement ( x++ or x-- or x = x + 2 )

Syntax :

variable initialization;

while(condition)

statements;

variable increment or decrement;

#include<stdio.h>

main( )

int x;

x = 1;

while(x <= 10)

printf("%d\t", x);

/* below statement means, do x = x+1, increment x by 1*/

x++;

getche();

}
FOR LOOP
for loop is used to execute a set of statements repeatedly until a particular
condition is satisfied. We can say it is an open ended loop..
SYNTAX:

for(initialization; condition; increment/decrement)


{
statement-block;
}

In for loop we have exactly two semicolons, one after initialization and second
after the condition. In this loop we can have more than one initialization or
increment/decrement, separated using comma operator. But it can have only
one condition.
The for loop is executed as follows:

1. It first evaluates the initialization code.


2. Then it checks the condition expression.
3. If it is true, it executes the for-loop body.
4. Then it evaluate the increment/decrement condition and again follows
from step 2.
5. When the condition expression becomes false, it exits the loop.
NESTED FOR LOOP
We can also have nested for loops, means one for loop inside another for loop.
Basic syntax is,

for(initialization; condition; increment/decrement)

for(initialization; condition; increment/decrement)

statement ;

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.
SYNTAX:
do
{
<statement/s>;
.....
}
while(condition)
Example: Program to print first 10 multiples of 5.

#include<stdio.h>

main()

int a, i;

a = 5;

i = 1;

do

printf("%d\t", a*i);

i++;

while(i <= 10);

getche();
}

Output:
5 10 15 20 25 30 35 40 45 50

You might also like