0% found this document useful (0 votes)
8 views23 pages

Loops

Uploaded by

Abir Sarker
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views23 pages

Loops

Uploaded by

Abir Sarker
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Iteration Statements/ Loop Control Statements

How it Works

1 Loops - 26 October, 2024


A sequence of statements are executed until a specified condition is true. This sequence of statements to be executed
is kept inside the curly braces { } known as the Loop body. After every execution of loop body, condition is verified,
and if it is found to be true the loop body is executed again. When the condition check returns false, the loop body is
not executed.

The loops in C language are used to execute a block of code or a part of the program several times. In other words, it
iterates/repeat a code or group of code many times.
Or Looping means a group of statements are executed repeatedly, until some logical condition is satisfied.

2 Loops - 26 October, 2024


Why use loops in C language?
Suppose that you have to print table of 2, then you need to write 10 lines of code.By
using the loop statement, you can do it by 2 or 3 lines of code only.

3 Loops - 26 October, 2024


A looping process would include the following four steps.
1. : Initialization of a condition variable.
2. : Test the condition.
3. : Executing the body of the loop depending on the
condition.
4. : Updating the condition variable.

4 Loops - 26 October, 2024


C language provides three iterative/repetitive loops.

1 : while loop
2 : do-while loop
3 : for loop

5 Loops - 26 October, 2024


While Loop:

Syntax : variable initialization ;


while (condition)
{
statements;
variable increment or decrement ;
}

while loop can be addressed as an entry control loop. It is completed in 3 steps.

• Variable initialization.( e.g int x=0; )


• condition( e.g while( x<=10) )
• Variable increment or decrement ( x++ or x-- or x=x+2 )

6 Loops - 26 October, 2024


The while loop is an entry controlled loop statement, i.e means the condition is evaluated
first and it is true, then the body of the loop is executed. After executing the body of the loop,
the condition is once again evaluated and if it is true, the body is executed once again, the
process of repeated execution of the loop continues until the condition finally becomes false
and the control is transferred out of the loop.

7 Loops - 26 October, 2024


Example : Program to print first 10 natural numbers
#include<stdio.h>
void main( )
{
int x;
x=1;
while (x<=10)
{
printf("%d\t", x);
x++;
}
getch();
}
Output 1 2 3 4 5 6 7 8 9 10

8 Loops - 26 October, 2024


9 Loops - 26 October, 2024
do-while loop

Syntax :

variable initialization ;
do{
statements ;
variable increment or decrement ;
}while (condition);

10 Loops - 26 October, 2024


The do-while loop is an exit controlled loop statement The body of the loop are executed
first and then the condition is evaluated. If it is true, then the body of the loop is executed
once again. The process of execution of body of the loop is continued until the condition
finally becomes false and the control is transferred to the statement immediately after the
loop. The statements are always executed at least once.

11 Loops - 26 October, 2024


Flowchart

12 Loops - 26 October, 2024


Example : Program to print first ten multiple of 5

#include<stdio.h>
#include<conio.h>
void main() {
int a,i;
a=5; i=1;
do {
printf("%d\t",a*i);
i++;
}while(i <= 10);

getch();
}

Output
5 10 15 20 25 30 35 40 45 50

13 Loops - 26 October, 2024


For Loop:
• This is an entry controlled looping statement.
• In this loop structure, more than one variable can be initialized.
• One of the most important features of this loop is that the three actions can be taken
at a time like variable initialization, condition checking and increment/decrement.
The for loop can be more concise and flexible than that of while and do-while loops.

14 Loops - 26 October, 2024


• Syntax : for(initialization; condition; increment/

decrement)

{
Statements;
}

15 Loops - 26 October, 2024


Example:
#include<stdio.h>
#include<conio.h>
void main( )
{
int x;

for(x=1; x<=10; x++)


{
printf("%d\t",x);
}

getch();
}

Output
1 2 3 4 5 6 7 8 9 10

16 Loops - 26 October, 2024


17 Loops - 26 October, 2024
Infinitive for loop in C

If you don't initialize any variable, check condition and increment or decrement variable in for loop, it is known as
infinitive for loop. In other words, if you place 2 semicolons in for loop, it is known as infinitive for loop.

for(; ;)
{
printf("infinitive for loop example by javatpoint");
}

18 Loops - 26 October, 2024


Basis of Difference For Loop While Loop Do While Loop
Where to The for loop is The other two loops i.e. while and do while loops are more suitable in
Use for Loop, while Loop and do while appropriate the situations where it is not known before hand when the loop will
Loop when we know in terminate.
advance
how many times the loop will be
executed.
In case if the test condition fails at In case if the test condition fails at
the beginning, and you may not the beginning, and you may want
want to execute the body of the to execute the body of the loop
loop even once if it fails, then the atleast once even in the failed
while loop should be preferred. condition, then the do while loop
should be preferred.

How all the three loops works? A do while loop will always
executed the code in the do {} i.e.
A for loop initially initiates a counter A while loop will always evaluate body of the loop block first and
variable (initializationexpression), the test-expression initially. It the then evaluates the condition. In
then it checks the test- test-expression this case also, the counter variable
expression, and executes the body of becomes true, then the body of the is initialized outside the body of
the loop if the test expression is true. loop will be executed. The the loop.
After executing the body of the loop, update

19 Loops - 26 October, 2024


Position of the statements : In for loop, all the three In while and do while loop, they are placed in
statements are placed in different position.
• Initialization
one position
• test-expression
• update-expression

Syntax of Loops for ( while(testexpression do {


)
initializationexp.(s); body-of-theloop;
test-expression(s); {
updateexpression(s);
updateexpression(s) body-of-theloop;
}
) while (test-
updateexpression(s);
{ expression);
}
body-of-the-loop
;
}

20 Loops - 26 October, 2024


Which one is Entry Controlled Both loops i.e. for loop and while loop are entry do while loop is an exit
Loop controlled loop, means condition is checked first and controlled loop, means
and if the condition is true then the body of the loop will means that condition is
Which one is Exit executes. placed after the body of the
Controlled Loop ? loop and is evaluated before
exiting from the loop.

Conversion of one Loop to : int i = 1; int i = 1;


another Loop or
: : :
Example : Print numbers from 1
to 10 using all the three loops. for (int i=1; i<=10; i++) : :

{ while (i<=10) do

Printf(“%d”,i); } { {

Printf(“%d”,i);
Printf(“%d”,i); ++i;
++i
}
}
while (i<=10)

21 Loops - 26 October, 2024


22 Loops - 26 October, 2024
23 Loops - 26 October, 2024

You might also like