Loop Control Structures
By:-
Dr. Vani Kapoor Nijhawan
Assistant Professor, VIPS
Dr. Vani Kapoor Nijhawan 1
Repetition Structures: Types
Allows the programmer to specify that an action is to be repeated while some
condition remains true.
There are three repetition structures in C: -
the while loop
the for loop
do-while loop.
Dr. Vani Kapoor Nijhawan 2
For Loop
The syntax of for loop is
for(initialisation;condition;iteration)
{
set of statements
}
Eg: Program to print Hello 10 times
for(i=1;i<=10;i++)
{
printf(“Hello”);
}
Dr. Vani Kapoor Nijhawan 3
While Loop
The syntax for while loop
Initialisation; //part 1
while(condn) //part 2
{
statements;
iteration; (increment/decrement) //part3
}
Eg:
a=10; //part1
while(a !=10) //part 2
{
printf(“%d”,a);
a- -; //part 3
}
Dr. Vani Kapoor Nijhawan 4
Questions
Q1. The current year and the year of joining of any employee are
taken as input from the keyboard. If the number of years of service is
more than 3 then a bonus of Rs. 2500 is given else nothing else is
done. The pay of an employee is calculated as under:
If basic salary is less than Rs. 1500 then HRA-10% & DA-90% of
basic.
If basic salary is greater than or equal to Rs. 1500 then HRA=Rs.500 &
DA=98% of basic salary.
Q2. Write a program to calculate the simple interest. If time is >=10 years
then rate of interest is 10% otherwise rate of interest is 5%.
Q3. Check a number for Armstrong number e.g. 153 is Armstrong number
(1*1*1+5*5*5+3*3*3=153).
Q4. WAP to reverse a number.
Q5. WAP to find out the sum of digits of a number.
Dr. Vani Kapoor Nijhawan 5
Do-While Loop – The Odd Loop
The syntax of do while loop
do
{
set of statements
}while(condn);
Eg: // for(i=1;i<=10;i++) // while(n!=0)
i=1;
do
{
printf(“%d”,i); //1
i--; //0
}while(i!=0)
Dr. Vani Kapoor Nijhawan 6
Do-while Vs while loop
Do-while will be executed minimum once, but while loop can have minimum
execution as 0 times.
Do-while is an exit control loop, but while are entry control loop.
While loop is not terminated by semi colon. But do-while is terminated by
semicolon.
Variable initialisation happens before the loop in case of while loop,but may
be done inside the loop body in case of do-while loop.
Syntax:
While(c1) do {
{ }while(c1);
}
Dr. Vani Kapoor Nijhawan 7
Break Statement
Break Statement is used to take the control out of a block.
for(i=0;i<=10;i++) //i=0,1,2…5 (not executed 6 to 10)
printf(“%d”,i); // 0 1 2 3 4 5
if(i==5)
break;
}//for
Dr. Vani Kapoor Nijhawan 8
Continue Statement
Continue is used to bypass(skip) a set of statements (written below continue) for a
particular iteration.
for(i=0;i<=10;i++) //i=0 1…5 6
if(i!=5) //T
continue; //i=5
printf(“%d”,i); // 0 1 2 3 4 6 7 8 9 10
}//for
Dr. Vani Kapoor Nijhawan 9
Break Vs Continue
1. Break takes the control out of a particular block, but continue skips rest of the statements
for a particular condition.
2. Break exits the loop completely but in case of continue the loop keeps executing with next
iteration.
Break statement works in loop as well as switch statement. but continue works with loops.
Example:
for(j=1;j<=3;j++)
printf(“%d\t%d”,i,j);
if(j==2)
break ; // repeat same code but replace break with continue for example of continue
} //for 1
Dr. Vani Kapoor Nijhawan 10
Goto Statement
Goto is used for unconditional/conditional branching.
It requires a label (identifier) where a branch is to be made.
It can have two forms.
Syntax: (Forward Jump)
Goto label;
………
………
Label:
Statements;
Dr. Vani Kapoor Nijhawan 11
Goto Statement
Syntax: (backward Jump)
Label:
Statements;
………
………
goto label;
Dr. Vani Kapoor Nijhawan 12
Example: Goto
#include<math.h>
#include<stdio.h>
void main()
{
float x,y;
read:
scanf(“%f”,&x);
if(x<0)
goto read;
y= sqrt(x);
printf(“%f%f”,x,y);
}
Dr. Vani Kapoor Nijhawan 13
Questions
Q1. Write the programs to print following patterns:
a) 1
2 3
4 5 6
b) 1
1 2
1 2 3
c) 1
2 2
3 3 3
Q2. WAP to print Fibbonacci series: 0 1 1 2 3 5 8 13 21…….
Q3. WAP to calculate factorial of a number.
Dr. Vani Kapoor Nijhawan 14