0% found this document useful (0 votes)
4 views7 pages

5 Looping

The document discusses control statements in C programming, focusing on looping constructs such as for, while, and do-while loops. It explains the structure, syntax, and execution flow of these loops, including nested and infinite loops. Additionally, it differentiates between entry-controlled and exit-controlled loops, providing examples and scenarios for their usage.

Uploaded by

anshureddy0612
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)
4 views7 pages

5 Looping

The document discusses control statements in C programming, focusing on looping constructs such as for, while, and do-while loops. It explains the structure, syntax, and execution flow of these loops, including nested and infinite loops. Additionally, it differentiates between entry-controlled and exit-controlled loops, providing examples and scenarios for their usage.

Uploaded by

anshureddy0612
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/ 7

Looping 2019

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 Mr. Sukruth Gowda M A, Assistant Professor


Looping 2019

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 for loop


Syntax: Flowchart:
Statement1;
Statement2;
for(initialization; condition; incrementation)
{
Statement3;
Statement4;
}
Statement5;

Explanation:
In case of for loop, the Initialization, Condition check and Incrementation is represented in the same line.
 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 after which again the control goes back to the
condition check, if condition is true, the body is executed 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.

2 Mr. Sukruth Gowda M A, Assistant Professor


Looping 2019
Consider the following program:
#include<stdio.h> OUTPUT:
INDIA
void main()
for(Initialization; Condition; Incrementation) { INDIA
int i;
{ INDIA
for(i=1;i<=5;i++)
Statement3; INDIA
{
INDIA
printf(“INDIA\n”);
Statement4; } is our country
} printf(“is our country\n”);
getch();
Statement5; }

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.

3 Mr. Sukruth Gowda M A, Assistant Professor


Looping 2019
 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.

4 Mr. Sukruth Gowda M A, Assistant Professor


Looping 2019
 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 of test condition is checked, if condition
statement in the loop body will be is satisfied than body of loop will 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

5 Mr. Sukruth Gowda M A, Assistant Professor


Looping 2019
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.

Nested Loops
 Using one loop inside another loop is called Nested Looping. You can use one or more loops inside
any other while, for, or do-while loop.
Syntax:
The syntax for a nested for loop statement in C is as follows:
for ( init; condition; increment )
{
for ( init; condition; increment )
{
statement(s);
}
statement(s);
}

}
}
}

Infinite loops
 A loop becomes an infinite loop if a condition never becomes false.
 The for loop is traditionally used for this purpose. Since none of the three expressions that form the
‘for’ loop are required, you can make an endless loop by leaving the conditional expression empty.
Syntax:
for( ; ; )
{
Statement to execute for ever;
}
Example:

6 Mr. Sukruth Gowda M A, Assistant Professor


Looping 2019
#include <stdio.h>
void main ()
{
for( ; ; )
{
printf("This loop will run forever.\n");
}
getch();
}

 When the conditional expression is absent, it is assumed to be true. You may have an initialization
and increment expression, but C programmers more commonly use the for(;;) construct to signify an
infinite loop.
 It can also be implemented with while and do-while loops also.

Note: You can terminate an infinite loop by pressing Ctrl + C keys.

7 Mr. Sukruth Gowda M A, Assistant Professor

You might also like