0% found this document useful (0 votes)
6 views6 pages

Looping Statements - 1

Uploaded by

Deepika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

Looping Statements - 1

Uploaded by

Deepika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Problem Solving and Programming with C GITAM

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?

1 Department of Computer Science and Engineering


Problem Solving and Programming with C GITAM

Types of looping constructs


Kind C Implementation Structure
Pre tested loops while
Post tested loops do-while
Count controlled loop while , for , do- while
Event controlled loop: Sentinel-controlled loop while , for
Event controlled loop: EndOfFile – controlled loop while , for , do-while
Event controlled loop: Input validation loop do-while
General conditional loop while, for , do-while

The while loop


Syntax: Flowchart:

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.

2 Department of Computer Science and Engineering


Problem Solving and Programming with C GITAM
 This process repeats as long as the condition evaluates to be true. Once the condition evaluates to be
false, the control flows to the statement outside the body of the loop.
Consider the following program:
Statement1; #include<stdio.h> OUTPUT:
void main()
Statement2; INDIA
{
initialization; int i; INDIA
i=1;
while(Condition check) INDIA
while(i<=5)
{ INDIA
{
Statement3; INDIA
printf(“INDIA\n”);
Statement4; i++; is our country
incrementation; }
printf(“is our country\n”);
} getch();
Statement5; }

The do while loop

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.

3 Department of Computer Science and Engineering


Problem Solving and Programming with C GITAM
 Next Incrementation is performed after which condition is checked, if condition is true, the control
re-enters the body and executes the statements again.
 This process repeats as long as the condition evaluates to be true. Once the condition evaluates to be
false, the control flows to the statement outside the body of the loop.
Consider the following program:
Statement1; #include<stdio.h> OUTPUT:
void main()
Statement2; INDIA
{
initialization; int i; INDIA
do
do INDIA
{
{ INDIA
printf(“INDIA\n”);
Statement3; i++; INDIA
Statement4; }
is our country
incrementation; while(i<=5);
} printf(“is our country\n”);
getch();
while(Condition check); }
Statement5;

“What are the Entry controlled and Event/ Exit controlled loops in C?”

Sl.No. Entry controlled loops Exit controlled loops


Entry Controlled will check the Exit Controlled will check the
Condition at First and doesn't Condition at Last and at least once the
1
execute if it is False statement will execute though it is
False
Entry control is otherwise called as Exit control is also called as DO
WHILE loop, because the while WHILE loop, because the do while
2 loop checks the condition at first, loop checks the condition at last
and then only execute the following
instructions.
In Entry controlled loop the test Exit controlled loop the body of loop
condition is checked first and if that will be executed first and at the end the
3 condition is true than the block test condition is checked, if condition
of statement in the loop body will is satisfied than body of loop will be
be executed executed again.
Entry control is otherwise called as Exit control is also called as DO
WHILE loop, because the while WHILE loop, because the do while
4 loop checks the condition at first, loop checks the condition at last
and then only execute the following
instructions

4 Department of Computer Science and Engineering


Problem Solving and Programming with C GITAM

Programs based on while loop -

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 ;
}

5 Department of Computer Science and Engineering


Problem Solving and Programming with C GITAM
4. Input validation: do – while loop

#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:

Enter employee number : 1500


Enter total sales amount of books and folders in Rs. 2100 1700
Commission earned = Rs. 465
Do you want to continue Y/N? N

Note:

Loop Type Description


while loop Repeats a statement or group of statements while a given condition
is true. It tests the condition before executing the loop body.
for loop Executes a sequence of statements multiple times and abbreviates
the code that manages the loop variable.
do while loop It is more like a while statement, except that it tests the condition at
the end of the loop body.
Nested Loops You can use one or more loops inside any other while, for, or
do..while loop.

6 Department of Computer Science and Engineering

You might also like