Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 34

18CSS26 & ARTIFICIAL INTELLIGENCE

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

• In this loop, the control conditions are tested before


the start of the loop execution.
• If the conditions are not satisfied, then the body of
the loop will not be executed.
• Otherwise it is called as pre-test loop.

SKASC 4
18CSS26 & ARTIFICIAL INTELLIGENCE

Exit-controlled loop

• In this loop, the test is performed at end of the body


of the loop and therefore the body is executed
unconditionally for the first time.
• Another name for Exit-controlled loop is post-test loop.

SKASC 5
18CSS26 & ARTIFICIAL INTELLIGENCE

Steps in Looping Process

• A looping process includes the following four steps:


– Setting and initialization of a condition variable
– Execution of the statements in the loop
– Test for a specified value of the condition variable for
execution of the loop
– Incrementing or updating the condition variable

SKASC 6
18CSS26 & ARTIFICIAL INTELLIGENCE

Flow Chart - Loops

SKASC 7
18CSS26 & ARTIFICIAL INTELLIGENCE

Decision Making and Looping

• The decision making and looping statements in C are as


follows:
• The while statement
• The do statement
• The for statement

SKASC 8
18CSS26 & ARTIFICIAL INTELLIGENCE

THE WHILE STATEMENT

• The while is an entry-controlled loop statement.

• Syntax:

while(test-condition)
{
body of the loop
}

SKASC 9
18CSS26 & ARTIFICIAL INTELLIGENCE

THE WHILE STATEMENT

• The test-condition is evaluated and if the condition is


true, then the body of the loop will be executed.
• After execution of the body, the test-condition is once
again evaluated and if it is true, the body is executed
once again.

SKASC 10
18CSS26 & ARTIFICIAL INTELLIGENCE

THE WHILE STATEMENT

• This process of repeated execution of the body


continues until the test-condition finally becomes
false and the control is transferred out of the loop.
• The body of the loop may have one or more statements.
The braces are need only if the body contains two or
more statements.

SKASC 11
18CSS26 & ARTIFICIAL INTELLIGENCE

THE WHILE STATEMENT

Display the numbers from 1 to 10 using while loop.


#include<stdio.h>
int main() Output
1
{ int num=1; 2
while(num<=10) 3
{ 4
5
printf(“%d\n”,num);
6
num++; 7
} 8
} 9
10
SKASC 12
18CSS26 & ARTIFICIAL INTELLIGENCE

THE DO- WHILE STATEMENT

• The do-while is an exit controlled loop.


• 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 is evaluated.
• If the condition is true, the program continues to
evaluate the body of the loop once again.
• This process continues as long as the condition is true.

SKASC 13
18CSS26 & ARTIFICIAL INTELLIGENCE

THE DO- WHILE STATEMENT

• Syntax:

do
{
body of the loop
}
while (test-condition);

SKASC 14
18CSS26 & ARTIFICIAL INTELLIGENCE

THE DO- WHILE STATEMENT

SKASC 15
18CSS26 & ARTIFICIAL INTELLIGENCE

Display the numbers from 1 to 10 using


do- while loop.
#include <stdio.h>
int main()
{
int num=1;
do
{
printf("%d\n",num);
num++;
} while(num<=10);
return 0;
}

SKASC 16
18CSS26 & ARTIFICIAL INTELLIGENCE

FOR STATEMENT

• The ‘for’ loop is another entry-controlled loop.


• It is used when an action is to be repeated for a
specified number of times.

Syntax:

for (initialization; test-condition; increment)


{
Body of the loop
}
SKASC 17
18CSS26 & ARTIFICIAL INTELLIGENCE

FOR STATEMENT

• The execution of the for statement is as follows:


• The initialization of the control variable is done first,
using assignment statements such as i=1 and count=0.
• The variables i and count are known as loop-control
variables.
• The value of the control variable is tested using the
test-condition.
• The test condition is a relational expression such as
i<0
SKASC 18
18CSS26 & ARTIFICIAL INTELLIGENCE

FOR STATEMENT

• If the test condition is true, the body of the loop is


executed; otherwise the loop is terminated and the
execution continues with the statement that immediately
follows loop.
• When the body of the loop is executed the control is
transferred back to the for statement after evaluating
the last statement in the loop.

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

reverse in given range


#include <stdio.h>
int main()
{
int i, start, end;
/* Input start and end limit from user */
printf("Enter starting value: ");
scanf("%d", &start);
printf("Enter end value: ");
scanf("%d", &end);
/*
* Run loop from 'start' to 'end' and
* decrement by 1 in each iteration
*/
for(i=start; i>=end; i--)
{
printf("%d\n", i);
}
return 0;
}
SKASC 22
18CSS26 & ARTIFICIAL INTELLIGENCE

FOR STATEMENT

Example: - Program to print alphabets from a-z


#include<stdio.h>
int main()
{
char ch;
printf("Alphabets from a - z are: \n");
for(ch='a'; ch<='z'; ch++)
{
printf("%c\n", ch);
}
return 0;
}
SKASC 23
18CSS26 & ARTIFICIAL INTELLIGENCE

FOR STATEMENT

• Additional features of for loop


• The ‘for’ loop in c has several capabilities that are not
found in other loop constructs.
• For example we can initialize more than one variables
at a time in the ‘for’ statement.
p=1;
for(n=0;n<17;++n) can be written as
for(p=1,n=0;n<17;++n)

SKASC 24
18CSS26 & ARTIFICIAL INTELLIGENCE

FOR STATEMENT

• Here the initialization section has two parts p=1 and

n=1 are separated by comma.

• Like the initialization section, the increment section

may also have more than one part.

SKASC 25
18CSS26 & ARTIFICIAL INTELLIGENCE

FOR STATEMENT

• The multiple arguments in the increment section are


separated by commas.
• Example:

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

• The third feature is that the test-condition may have


any compound relation and the testing need not
be limited only to the loop control variable.
• Example: sum=0;
for(i=1;i<20&&sum<100;++i)
{
sum=sum+1;
printf(“%d %d\n”,i,sum);
}
SKASC 27
18CSS26 & ARTIFICIAL INTELLIGENCE

FOR STATEMENT

• And also permissible to use expression in the


assignment statements of initialization and increment
sections.
Example:
for(x=(m+n)/2;x>0;x=x/2)

• Another unique aspect of for loop is that one or more


sections can be omitted, if necessary.

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

• The nesting may continue up to any desired

level.

• The loop should be properly indented so as to

enable the reader to easily determine which

statements are contained with in each for statement.

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

You might also like