Professional Documents
Culture Documents
Lecture 9
Lecture 9
Topics Covered
• Introduction
• The while loop
• The do-while statement
• The for statement
SKASC 1
18CSS26 & ARTIFICIAL INTELLIGENCE
INTRODUCTION
• Loop statements are used to perform repetitive processes
without the use of goto statements.
• Loop control statements change execution from its normal
sequence.
• A loop statement allows us to execute a statement or group of
statements multiple times
• A program loop consists of two segments:
• Body of the loop
• Control statement
SKASC 2
18CSS26 & ARTIFICIAL INTELLIGENCE
INTRODUCTION
• The control statement tests certain conditions and then
directs the repeated execution of the statements
contained in the body of the loop.
• Depending on the position of the control statements in the
loop, a control structure may classify in to two types.
– Entry-controlled loop
– Exit-controlled loop
SKASC 3
18CSS26 & ARTIFICIAL INTELLIGENCE
Entry-controlled loop
SKASC 4
18CSS26 & ARTIFICIAL INTELLIGENCE
Exit-controlled loop
SKASC 5
18CSS26 & ARTIFICIAL INTELLIGENCE
SKASC 6
18CSS26 & ARTIFICIAL INTELLIGENCE
SKASC 7
18CSS26 & ARTIFICIAL INTELLIGENCE
SKASC 8
18CSS26 & ARTIFICIAL INTELLIGENCE
• Syntax:
while(test-condition)
{
body of the loop
}
SKASC 9
18CSS26 & ARTIFICIAL INTELLIGENCE
SKASC 10
18CSS26 & ARTIFICIAL INTELLIGENCE
SKASC 11
18CSS26 & ARTIFICIAL INTELLIGENCE
SKASC 13
18CSS26 & ARTIFICIAL INTELLIGENCE
• Syntax:
do
{
body of the loop
}
while (test-condition);
SKASC 14
18CSS26 & ARTIFICIAL INTELLIGENCE
SKASC 15
18CSS26 & ARTIFICIAL INTELLIGENCE
SKASC 16
18CSS26 & ARTIFICIAL INTELLIGENCE
FOR STATEMENT
Syntax:
FOR STATEMENT
FOR STATEMENT
SKASC 19
18CSS26 & ARTIFICIAL INTELLIGENCE
Example
#include <stdio.h>
int main()
{
int i;
for (i = 1; i <=10; i++)
{
printf("%d ", i);
}
return 0;
}
SKASC 20
18CSS26 & ARTIFICIAL INTELLIGENCE
FOR STATEMENT
Example:
#include <stdio.h>
int main()
{
int sum=0,i;
for(i=1;i<=10;i++)
{
sum= sum+i;
}
printf("\n Sum of 1 to 10 numbers is %d", sum);
return 0;
} SKASC 21
Program to print natural number in
18CSS26 & ARTIFICIAL INTELLIGENCE
FOR STATEMENT
FOR STATEMENT
SKASC 24
18CSS26 & ARTIFICIAL INTELLIGENCE
FOR STATEMENT
SKASC 25
18CSS26 & ARTIFICIAL INTELLIGENCE
FOR STATEMENT
for(n=1,m=50;n<=m;n=n+1,m=m-1)
{
p=m/n;
printf(“%d %d %d\n”,n,m,p);
}
SKASC 26
18CSS26 & ARTIFICIAL INTELLIGENCE
FOR STATEMENT
FOR STATEMENT
SKASC 28
18CSS26 & ARTIFICIAL INTELLIGENCE
FOR STATEMENT
Example:
m=5
for(;m!=100;)
{
printf(“%d\n”,m);
m=m+5 ;
}
SKASC 29
18CSS26 & ARTIFICIAL INTELLIGENCE
FOR STATEMENT
Nesting of for loop:
A for loop can be nested with in another for loop.
Example:
for(i=1;i<10;++i)
{ ----------------
for(j=1;j!=5;++j)
{ ------------
}
}
SKASC 30
18CSS26 & ARTIFICIAL INTELLIGENCE
FOR STATEMENT
level.
SKASC 31
18CSS26 & ARTIFICIAL INTELLIGENCE
Exercises
1. Write a C program to print the Fibanocci series.
2. Write a C program to print the Factorial of a given
number using while loop.
3. Write a C program to find the sum of n number using
while loop.
SKASC 32
18CSS26 & ARTIFICIAL INTELLIGENCE
Summary
❖ The control statement tests certain conditions and then
directs the repeated execution of the statements contained in
the body of the loop.
❖ A control structure may classify in to two types.
❖ Entry-controlled loop
❖ Exit-controlled loop
❖ The test-condition is evaluated and if the condition is true,
then the body of the loop will be executed.
❖ On reaching the do statement, the program proceeds to
evaluate the body of the loop first. At the end of the loop, the
test-condition in the while statement
SKASC is evaluated. 33
18CSS26 & ARTIFICIAL INTELLIGENCE
SKASC 34