You are on page 1of 33

Control Structures[Repetition

structures/ or Looping statements/ or


Iterative statements]

By
Pundreekaksha Sharma
Assistant Professor
CSE Deptt

©LPU CSE101 C Programming


Outline
• Repetition structure/Control Loop Statements
– for statement
– while statement
– do-while statement

©LPU CSE101 C Programming


Repetition(Going to School)
Day =
Monday to
Saturday

©LPU CSE101 C Programming


Looping (repetition)
• What if we want to display hello 500 times?
– Should we write 500 printf statements or
equivalent ?
⮚ Obviously not.
• It means that we need some programming
facility to repeat certain works.
• Such facility is available in form of looping
statements.

©LPU CSE101 C Programming


Loop
• The main idea of a loop is to repeat an action
or a series of actions.

The concept of a loop without condition

©LPU CSE101 C Programming


• But, when to stop looping?
• In the following flowchart, the action is executed
over and over again. It never stops – This is called
an infinite loop
• Solution – put a condition to tell the loop either
continue looping or stop.

©LPU CSE101 C Programming


Loop
• A loop has two parts –
body and condition

• Body – a statement or a
block of statements that
will be repeated.

• Condition – is used to
control the iteration –
either to continue or stop
iterating.

©LPU CSE101 C Programming


Loop statements
• C provides three loop statements:

©LPU CSE101 C Programming


Loop statements

©LPU CSE101 C Programming


The “while” Statement in C
• The syntax of while statement in C:
Syntax

while (loop repetition condition){


statement;
updating control;
}

While fatigue level is not


reached

©LPU CSE101 C Programming


while statement
while(loop repetition condition)
{
Statements;
}

Loop repetition condition is the condition which


controls the loop.
•The statement is repeated as long as the loop
repetition condition is true.
•A loop is called an infinite loop if the loop
repetition condition is always true.
•while loop is known as entry controlled loop, as
condition is checked at the beginning/ or entry
point
©LPU CSE101 C Programming
while statement
Example: This while statement prints numbers 10 down to 1
#include<stdio.h>
int main()
{
int n=10;
while (n>0){
printf(“%d ”, n);
n=n-1;
}
return 0;
}
10 9 8 7 6 5 4 3 2 1

Do TEN push ups imposes a


count condition
©LPU CSE101 C Programming
Q1
How many times i value is checked in the following C code?
#include <stdio.h>
int main()
{
int i = 0;
while (i < 3)
i++;
printf("In while loop\n");
}
A. 2
B. 3
C. 4
D. 1

©LPU CSE101 C Programming


Q2
What will be the output of the following C code?
#include <stdio.h>
int main()
{
int i = 0;
while (++i)
{
printf("H");
}
return 0;
}
A. H
B. H is printed infinite times
C. Compile time error
D. Nothing will be printed

©LPU CSE101 C Programming


The for Statement in C
• The syntax of for statement in C:
Syntax
for (initialization-expression;
loop-repetition-condition;
update-expression){
statement;
}
• The initialization-expression set the initial value of the loop
control variable.
• The loop-repetition-condition test the value of the loop
control variable.
• The update-expression update the loop control variable.
• It is also known as entry controlled loop as condition is
checked first and then loop body executes

©LPU CSE101 C Programming


for statement
for (Initialization; Condition; Updating )
{
Repeated_Actions;
}

©LPU CSE101 C Programming


for statement
Example: This for statement prints numbers 10 down to 1
#include<stdio.h>
int main()
{
int n;
for (n=10; n>0; n=n-1){
printf(“%d ”, n);
}
return 0;
}

10 9 8 7 6 5 4 3 2 1
Do TEN push ups = for
count=1; count<=10; count+
+
©LPU CSE101 C Programming
©LPU CSE101 C Programming
Program to find sum of first n numbers
#include<stdio.h>
int main()
{
int n,i,sum=0;
printf("enter the value");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+i;
}
printf("the sum=%d",sum);
return 0;
}

©LPU CSE101 C Programming


Program to find sum of digits of a number
#include<stdio.h>
int main()
{
int no,sum=0,n;
printf("enter the no");
scanf("%d",&no);
while(no!=0)
{
n=no%10;
sum=sum+n;
no=no/10;

}
printf("the sum of digits=%d",sum);
}

©LPU CSE101 C Programming


Q1
#include <stdio.h>
int main()
{
int i;
for (i = 1; i != 10; i += 2)
printf("Hello");
return 0;
}
A. Hello will be displayed 5 times
B. Hello will be displayed 4 times
C. Hello will be displayed infinite no. of times
D. Hello will be displayed 6 times

©LPU CSE101 C Programming


Q2
What will be the output of following code
#include<stdio.h>
int main()
{
int i;
for(i=1;i<10;i++);
printf("%d",i);
return 0;
}
A. Numbers from 1 to 9 will be printed
B. 10
C. 9
D. Infinite loop

©LPU CSE101 C Programming


for vs while loop

©LPU CSE101 C Programming


Nested Loops
• Nested loops consist of an outer loop with one
or more inner loops.
• Eg:
Outer loop
for (i=1;i<=100;i++){
for(j=1;j<=50;j++){ Inner loop


}
}
• The above loop will run for 100*50 iterations.

©LPU CSE101 C Programming


#include<stdio.h>
int main() Program to
{
int i,j,k ; print tables
printf(“Enter a number:”);
scanf(“%d”, &k);
up to a
printf(“the tables from 1 to %d: \n”,k);
for(i=1; i<k; i++){
given
for(j=1; j<=10; j++){ number.
printf(“%d ”,i*j);
} //end inner for loop
printf(“\n”);
} //end outer for loop
return 0;
} //end main

Enter a number
4
The tables from 1 to 4
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12CSE101
©LPU 16 20 24 28 32 36 40
C Programming
#include<stdio.h>
int main() Program to
{
int i,j;
display a
printf(“Displaying right angled triangle for 5 pattern.
rows”);
for(i=1 ; i<=5 ; i++) {
for(j=1 ; j<=i ; j++)
printf(“* ”);
printf(“\n”);
}
return 0;
}

Displaying right angled triangle for 5 rows


*
**
***
****
*****
©LPU CSE101 C Programming
While vs. for statements

Comparing for and while loops


©LPU CSE101 C Programming
The do-while Statement in C
• The syntax of do-while statement in C:
Syntax
do
{
statement;
} while (condition);
• The statement executed at least one time(even if the
condition is false)
• For second time, If the condition is true, then the
statement is repeated else the loop is exited.
• Also known as exit-controlled loop, as loop body
executes first and then the condition is checked

5-28
©LPU CSE101 C Programming
do…while statement

do
{
Repeated_Actions;
} while (Condition);

©LPU CSE101 C Programming


do…while statement
Example: this do…while statement prints numbers 10 down to 1
#include<stdio.h>
int main()
{
int n=10;
do{
printf(“%d ”, n);
n=n-1;
}while (n>0);
}

10 9 8 7 6 5 4 3 2 1

©LPU CSE101 C Programming


Difference between while and do..while
while loop do..while loop
1. Condition is specified at the top 1. Condition is mentioned at the
bottom
2. Body statements are executed 2. Body statements are executed at
when the condition is satisfied least once even if the expression value
evaluates to false
3. It is an entry controlled loop 3. It is an exit controlled loop
4.Syntax: 4.Syntax:
while (condition) do
statement; {
statements;
}
while (condition);

©LPU CSE101 C Programming


Q1
What will be the output of the following C code?
#include <stdio.h>
int main()
{
do
printf("In while loop ");
while (0);
printf("After loop\n");
return 0;
}
A. In while loop
B. In while loop
After loop
C. After loop
D. Infinite loop

©LPU CSE101 C Programming


Q2
What will be the output of the following C code?
#include <stdio.h>
int main()
{
int i = 0;
do {
i++;
printf("In while loop\n");
} while (i < 3);
return 0;
}
A. In while loop
In while loop
In while loop

B. In while loop
In while loop

C. Nothing will be displayed


D. Compile time error
©LPU CSE101 C Programming

You might also like