Lecture 4a:
Loops in C
By Mr. Peter Kaaya
Loop statement
• Loop:-it is a block of statement that performs set
of instructions.
• In loops, Repeating particular portion of the
program either a specified number of time or
until a particular no of condition is being
satisfied.
• There are three types of loops in c
1. While loop
2. do while loop
3. for loop
4. While loop
syntax
while(condition)
{
Statement 1;
Statement 2;
}
Or while(test condition)
Statement;
• The test condition may be any expression
when we want to do something a fixed no of
times but not known about the number of
iteration, in a program then while loop is
used.
• Here first condition is checked if, it is true
body of the loop is executed else, If condition
is false control will be come out of loop.
Example:-
/* wap to print 5 times welcome to C” */
#include<stdio.h>
void main(){
int p=1;
While(p<=5){
printf(“Welcome to C\n”);
P=p+1;
}
}
Output:
Welcome to C
Welcome to C
Welcome to C
Welcome to C
Welcome to C
• So as long as condition remains true
statements within the body of while
loop will get executed repeatedly.
do while loop
• This (do while loop) statement is also used for
looping.
• The body of this loop may contain single statement
or block of statement.
• The syntax for writing this statement is:
Do
{
Statement;
}
while(condition);
Example:-
#include<stdio.h>
void main(){
int X=4;
do {
printf(“%d”, X);
X=X+1;
}
while(X<=10);
printf(“ ”);
}
Output:
• 4 5 6 7 8 9 10
• Here firstly statement inside body is executed then
condition is checked.
• If the condition is true again body of loop is
executed and this process continue until the
condition becomes false.
• Unlike while loop semicolon is placed at the end of
while.
• There is minor difference between while and do while
loop,
• while loop test the condition before executing any of
the statement of loop.
• Whereas do while loop test condition after having
executed the statement at least one within the loop.
• If initial condition is false while loop would not
executed it’s statement
• do while loop executes it’s statement at least once
even If condition fails for first time.
• It means do while loop always executes at least once
for loop
• In a program, for loop is generally used when
number of iteration are known in advance.
• The body of the loop can be single statement
or multiple statements.
• Syntax:-
for(exp1;exp2;exp3) {
Statement;
}
OR
for(initialized counter; test counter; update
counter) {
Statement;
}
• Here exp1 is an initialization expression, exp2 is test
expression or condition and exp3 is an update
expression.
• Expression 1 is executed only once when loop
started and used to initialize the loop variables.
• Condition expression generally uses relational
and logical operators.
• And update part executes only when after
body of the loop is executed.
Example:-
void main() {
int i;
for(i=1;i<10;i++)
{
Printf(“ %d ”, i);
}
}
• Output:-1 2 3 4 5 6 7 8 9
Nesting of loop
• When a loop written inside the body of
another loop then, it is known as nesting of
loop.
• Any type of loop can be nested in any type
such as while, do while, for.
• For example nesting of for loop can be
represented as :
void main(){
int i,j ;
for(i=0; i<2; i++)
for(j=0;j<5; j++)
printf(“i= %d and j=%d”, i, j);
}
• Output:
– i=0
– j=0 1 2 3 4
– i=1
– j=0 1 2 3 4
End of the lecture