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

Loop in C

Dr. Rupam Bhattacharya


Introduction
• We know that, to solve many problem, repetition of same
logic/calculation is very important.

• Many mathematical calculations are there where repetition of


calculation is required.

– Calculation of factorial, to the power etc. are the example.

• Loop is required to implement repetition.


Introduction

• Here, we will learn how to write C program using loop.

• Learning and understanding of loop in program is vital as we


need to use loop to solve several problem.
Introduction
• In C programming language, there are three kinds of loop.

– for loop
– while loop
– do while loop

– Here, for and while are entry controlled loop where as do


while is exit controlled loop.
for loop
• The for loop:
Syntax:
for(initialization ; condition ; increment/decrement)
{
codes under for loop
}
– Initialization: This is the starting value. It depends on application.

– Condition: The loop will repeatedly check the condition. If condition


found true, for loop will execute again.

– Increment/decrement: Initial value need to be incremented or


decremented to make the loop condition false at some point,
otherwise loop will be infinite.
For loop
• The for loop:
Example:
for( i = 0 ; i < 5 ; i = i + 1)
{
printf(“\n Hello World!!!”);
}
– Initialization of i = 0 will execute once. That is the initial value.

– After initialization, condition will be tested, if it is true, loop body


will execute.

– After execution of loop, control will goes to increment i = i + 1 and


will check the condition again. If it is true, same thing will repeat.
for loop
• The for loop:
Example:
for( i = 0 ; i < 5 ; i = i + 1)
{
printf(“\n Hello World!!!”);
}

– This loop will run five times.

– It will print Hello World five times.

– Do not put semi-colon just after loop.


for loop
• Inside for loop, two semi-colons are mandatory.

• But, all statements are not mandatory.

– Consider this code:


for( i = 0 ; i < 5 ; )
{
printf(“\n Hello World!!!”);
i = i + 1;
}
This code will produce same output as of previous code
for loop
• Problem statements-1:

Accept a positive number from user. Add from 1 to that


number. Display the result.

Example: Suppose n = 6, so result will be:

1 + 2 + 3 + 4 + 5 + 6 = 21
for loop
• Code:-

• Program starts with pre-processor and main() .

• Declare 3 variables: i to run the loop, n to take input and sum


to store result.
for loop
• Code:-

• Printf()will put the string on screen.

• Scanf() will read from the keyboard and store on n.


for loop
• Code:-

• Loop is running for n times.

• Sum of 1 to n done using loop.

• Displaying the output.


for loop
• Problem statements-2:

Accept a positive number from user. Then calculate factorial of


that number.

Example: Suppose n = 5, so result will be:

5 x 4 x 3 x 2 x 1 = 120
for loop
• Code:-

• Program starts with pre-processor and main() .

• Declare 3 variables: i to run the loop, n to take input and fact


to store result.
for loop
• Code:-

• Loop is running from n to 1.

• Calculating factorial by repetitive multiplication.


for loop
• A variation:-

• i initialized before loop.


• Condition is inside loop.
• Decrement (i--) is inside loop body.
• Final output will be same.
while loop
• The while loop:
Syntax:
initialization
while(condition)
{
codes under for loop
increment/decrement loop variable
}
– Initialization: It will be done before loop. So, it will execute once.

– Condition: Condition in while loop is must. The loop will repeatedly check the
condition. If condition found true, for loop will execute again.

– Increment/decrement: It will be inside body of the loop. Initial value need to


be incremented or decremented to make the loop condition false at some
point, otherwise loop will be infinite.
while loop
• The while loop:
Example:
int i = 0;
while(i < 5)
{
printf(“\n Hello World!!!”);
i++;
}
– Initialization of i = 0 will execute once. That is the initial value.

– Condition will be tested, if it is true, loop body will execute.

– After printing Hello World, i will be incremented and will check the
condition again. If it is true, same thing will repeat.
while loop
• Problem statements-3:

Accept a positive number from user. Then display their sum of


digits.

Example: Suppose n = 516, so result will be:

5 + 1 + 6 = 12
while loop
• Code:-

• Declared 3 variables .

• n to store input, digit will store individual digit and finally,


sum will store the result.
while loop
• Code:-

• First line inside loop is cutting digits from right .


• Next, sum of digits is stored.
• n reinitialized with remaining digits.
while loop
• Example:
• n=512
• n> 0 true
• So, digit= n%10=2
• Sum=sum+digit=0+2=2
• n=n/10=51
while loop
• Exampla:
• n=51
• n> 0 true
• So, digit= n%10=1
• Sum=sum+digit=2+1=3
• n=n/10=5
while loop
• Exampla:
• n=5
• n> 0 true
• So, digit= n%10=5
• Sum=sum+digit=3+5=8
• n=n/10=0

• Hence answer = 8
while loop
• What will be the result:-
int i = 0;
while(i<5)
{
printf(“Hello world”);
}

• It is an infinite loop.
• Loop variable i is remain 0.
• So, the loop condition is always true.
while loop
• What will be the result:-
int i = 0;
while( )
{
printf(“Hello world”);
i++;
}

• It is a syntactical error.

• There must have a condition inside while( ).


while loop
• What will be the result:-
int i = -1
while(i)
{
printf(“Hello world”);
i++;
}

• Hello World will be printed once.


• First time, -1 will be considered as true (non zero).
• Next time, i will be zero, that implies false.
while loop
• What will be the result:-
int i = -1, j = 0;
while(i && j)
{
printf(“Hello world”);
i++;
}

• Hello World will not be printed once.


• (-1 && 0) implies (TRUE && FALSE).
• Final value is FALSE.
do-while loop
• The do-while loop:
Syntax:
initialization;
do
{
codes under for loop
increment/decrement loop variable
} while(condition);
– Initialization: It will be done before loop. So, it will execute once.
– do : No condition checking. Execute the body of the loop
– Condition: After that, condition will be checked. If the condition is
true, it will execute the loop, will stop otherwise.
do-while loop
• do-while is exit controlled loop.

• First, It will execute loop body, then check the condition.

• Even if the loop condition is false, do-while loop will execute


at least once.

• Output of do-while loop may not be always same compared


to for loop or while loop.

You might also like