[go: up one dir, main page]

0% found this document useful (0 votes)
129 views38 pages

By P.Vinitha M.SC., M.Phil

This document discusses looping statements in C programming. It defines looping statements as statements that repeatedly execute a block of code until a given condition is met. The three types of looping statements in C are while loops, do-while loops, and for loops. Examples are provided for each looping statement type to illustrate their syntax and flow of execution. The key differences between the different loop types are also summarized.

Uploaded by

Vinitha Arun
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views38 pages

By P.Vinitha M.SC., M.Phil

This document discusses looping statements in C programming. It defines looping statements as statements that repeatedly execute a block of code until a given condition is met. The three types of looping statements in C are while loops, do-while loops, and for loops. Examples are provided for each looping statement type to illustrate their syntax and flow of execution. The key differences between the different loop types are also summarized.

Uploaded by

Vinitha Arun
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 38

By

P.Vinitha M.Sc., M.Phil


OBJECTIVES

 To study about the looping statements in the C


Programming Language.
 To study their syntax and flow of execution with
sample programs
SUB TOPICS

 Recap of control statements


 What is looping statement?
 Types of looping statement
 While loop
 Do..while loop
 For loop
CONTROL STATEMENTS
 A program contains set of instructions.
 The instructions in the program are normally executed in
the order in which they appear.
 The control structures are used to change the order of
execution based on certain conditions or repeat set of
statements until a given condition is satisfied.
 An expression followed by semicolon is called a simple
statement.
 A set of statements enclosed within { and } is called
compound statement or statement block.
TYPES OF CONTROL STATEMENTS

 There are three types of control structures


available in C
 Decision making statements : if, if..else, switch etc.
 Looping statements : while, do..while and for
 Unconditional execution statements : break and
continue
NEED FOR LOOPING STATEMENTS:
 Consider the program for printing the word Good
Morning 5 times.
 The program without looping statement is

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("\nGood Morning");
printf("\nGood Morning");
printf("\nGood Morning");
printf("\nGood Morning");
printf("\nGood Morning");
getch();
}
 The same program can be written using the for loop
statement as
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i;
for(i=0;i<5;i++)
{
printf("\nGood Morning");
}
getch();
}

The output of the above two programs are the same but the
length of program is reduced much.
 Looping statements are used to execute group of statements
repeatedly until a given condition is satisfied.
 The looping statements consists of two part
 The body of the loop – The statements to be repeatedly
execute.
 The control statement – Condition to execute body of the
loop.
 The looping statements available in C are

 while loop

 do.. while loop

 for loop
 The while loop is used when a statement is to be
executed repeatedly until a given condition is satisfied.
 Syntax:

while(condition)
{
Body of the loop
}
Next statement;

 while is the keyword. The condition should be a valid


relational or logical expression.
Entry

False
Test
Condition

True
Body of the Loop

Next Statement
1. Check the condition.
2. If the condition is true
Execute the body of the loop. Goto step1
else
Goto next statement
 In case of while statement the condition is
checked before entering the body of the loop. So
this is called entry-controlled loop.
 The body of the loop may not be executed at
least once if the condition becomes false at the
first attempt.
 The loop variable which is the variable used in the test

condition should be initialized before the while

statement.

 Within the body of the loop the loop variable should be

increment/decremented towards the terminating

condition.
/* Example for while statement */
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
while(i<=5)
{
printf("\nGood Morning");
i++;
}
getch();
}
/* Program to find the sum of digits of a number */
#include<stdio.h>
void main()
{
int n,m,rem,sum=0;
printf("Enter the number:");
scanf("%d",&n);
m=n;
while(m!=0)
{
rem=m%10;
sum=sum+rem;
m=m/10;
}
printf("The sum of the digits of the number %d is %d",n,sum);
}
DO..WHILE
STATEMENT
 Do..while loop is used when it is necessary to execute the
body of the loop at least once before testing the condition.
Syntax:

do
{
Body of the loop;
} while(condition);

Next statement;
 In do..while loop the test condition is checked at the end of the
loop. So it is called exit-controlled loop.
 The while condition in the do..while should be followed by
semicolon.
Entry

Body of the loop

True
Test Condition

False

Next Statement
1. Execute the body of the loop.
2. check the test condition
if true
goto step 1
else
goto next statement.
/* Example for do..while statement */
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
clrscr();
do
{
printf("Hello\n");
i++;
}while(i<=5);
getch();
}
/* To count the number of alphabets in a sequence of characters
*/
#include<stdio.h>
void main()
{
int count=0;
char ch;
printf("Enter sequence of characters. Type $ to end:");
do
{
ch=getchar();
if((ch>='A'&& ch<='Z')||(ch>='a'&&ch<='z'))
count++;
}while(ch!='$');
printf("Number of characters entered is %d",count);
}
 For loop is an entry controlled loop. The for loop is usually
used when the loop is repeated for a known number of times.
Syntax:

for (initialization; test condition


;Increment/decrement)
{
Body of the loop;
}
Next statement;
 A for statement has the following syntax:

The initialization The statement is


is executed once executed until the
before the loop begins condition becomes false

for ( initialization ; condition ; increment )


{
statements;
}
The increment portion is executed at
the end of each iteration
initialization

condition
evaluated
false
true

statement

increment

NEXT Statement
1. The initialization expression is evaluated first to initialize
the loop variable.
2. The test expression is evaluated to check the condition for
execution
If true the body of the loop is executed.
Then increment/decrement expression is evaluated and goto
step 2.
3. If the test condition is false the loop is terminated and the
next statement is executed.
/* Example for statement */
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=5;i++)
printf("Inside for loop\n");
getch();
}
/* Factorial using for loop */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,f=1,i;
clrscr();
printf("Enter n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
f=f*i;
printf("Factorial of %d is %d",n,f);
getch();
}
RECAP
 In this session we have seen the three type of looping
statements
 While using the looping statements we should select the
right choice based on the conditions.
RECAP

 The while statement is normally used when the number


of times the loop to be executed is not known and is
based on the condition.

while(condition)
{
Body of the loop
}
Next statement;
RECAP

 The do.. While loop is used when we want to execute the


body of the atleast once before checking the condition.

do
{
Body of the loop;
} while(condition);

Next statement;
RECAP

 The for loop is used when we are sure about the number
of times the loop is to be executed.

The initialization The statement is


is executed once executed until the
before the loop begins condition becomes false

for ( initialization ; condition ; increment )


{
statements;
}
The increment portion is executed at
the end of each iteration
RECAP

 Take care if you have the following correctly


specified in the looping statements
1. Loop variable is initialized properly before the lop
begins.

2. The termination condition is provided.

3. The loop variable is incremented / decremented


towards the terminating value.

You might also like