Looping Statements - 1
Looping Statements - 1
Control Statements:Looping–Repetition
- Iteration and repetitive execution (for, while, do-while), nested loops.
Loop-loop is a control structure that repeats a group of any kind of statements in a program.
Loop body- The statements that are repeated in the loop.
Looping Constructs in C:
A set of statements may have to be executed number of times or till the condition is satisfied are
called Looping Constructs.
The statements that help us to execute a set of statements repeatedly are called Looping Constructs.
It is implemented using the following statements.
✓ for
✓ while
✓ do-while
The loop will have to perform 3 tasks
✓ Initialization : Loop Initialization.
✓ Condition checking : Loop Condition.
✓ Incrementation / decrementation : Loop Updating.
The difference between 3 looping control is the order in which they are represented.
Three questions to determine whether loops will be required in the general algorithm:
1. Are there any steps repeated while solving the problem? If so, which ones?
2. Do we know in advance how many times these steps are repeated?
3. Do we know the condition until when we keep repeating these steps?
Statement1;
Statement2;
initialization;
while(Condition check)
{
Statement3;
Statement4;
incrementation;
}
Statement5;
Explanation:
In case of while loop, the Initialization, Condition check and Incrementation is represented in the separate
statements.
First Initialization of the loop is performed.
Next condition is checked. If condition is true then the control enters the body of the loop and
statements are executed.
Next Incrementation of the loop is performed, which is represented in loop body after which again
the control goes back to the condition check, if condition is true, the body is executed again.
Syntax: Flowchart:
Statement1;
Statement2;
initialization;
do
{
Statement3;
Statement4;
incrementation;
}
while(condition check);
Statement5;
Explanation:
In case of for do-while, the Initialization, Condition check and Incrementation is done in the separate
statement.
First Initialization of the loop is performed.
Next condition is not checked. Rather body of the loop is entered and statements are executed.
“What are the Entry controlled and Event/ Exit controlled loops in C?”
1. Write a program to reverse a given 2. Write a program to find Gcd and Lcm
number #include<stdio.h>
#include<stdio.h> void main()
void main() {
{ int n=15,m=25,gcd;
int num=1234,rnum=0,rem; while (n!=m)
while (num!=0) {
{ if (n>m)
rem = num%10; n = n-m;
rnum = rnum*10+rem; else
num = num/10; m = m –n;
} }
printf(“reversed num = %d”,rnum); gcd = n;
} printf(“gcd = %d”,gcd);
}
3. Design a program that calculates the average exam grade for a class of students using Sentinel
controlled while loop.
#include <stdio.h>
int main ( )
{
int counter, grade, total, average ;
total = 0 ;
counter = 1 ;
printf(“Enter a grade: “) ;
scanf(“%d”, &grade) ;
while (grade != -1)
{
total = total + grade ;
counter = counter + 1 ;
printf(“Enter another grade: “) ;
scanf(“%d”, &grade) ;
}
average = total / counter ;
printf (“Class average is: %d\n”, average) ;
return 0 ;
}
#include <stdio.h>
void main ( )
{
int empno ;
float bookamt, folderamt, comm;
char choice;
do
{
printf(“enter employee number:”);
scanf(“%d”, &empno );
printf(“ \nEnter total sales amount of books and folders in Rs.\n “);
scanf(“%f %f”,&bookamt,&folderamt);
comm = (bookamt*0.1 + folderamt * 0.15);
printf(“\n Commission earned = Rs.%.2f”,comm);
printf(“\n Do you want to continue Y/N?”);
ch = getchar();
}
while (choice == ‘y’ || choice == ‘Y’);
}
Output:
Note: