Loop
Loop
Loop or Iteration
The repetition of a statement is called loop or iteration
When a statement or group of statement repeated again and
02/10/2025
again is called loop .
Loops cause a section of your program to be repeated a certain
number of times.
CSF,NU
The repetition continues while a condition is true.
When the condition becomes false, the loop ends and control
passes to the statements following the loop.
Types of loops
For loop
While loop 2
Do loop
For loop
• The for loop executes one or more statements for a specified
number of times.
02/10/2025
• The for loop allows us to specify three things about a loop in
a single line:
• Setting a loop counter to an initial value.
CSF,NU
• Testing the loop counter to determine whether its value has
reached the number of repetitions desired.
• Increasing or decreasing the value .
3
For Loop
CSF,NU 02/10/2025
4
For Loop Syntax
02/10/2025
for ( initialization ; testing/condition ; increment /decrement )
{
statements;
CSF,NU
}
5
CSF,NU 02/10/2025
6
Example
#include<stdio.h>
#include<conio.h>
02/10/2025
main()
{
CSF,NU
int a;
for(a=1;a<=10;a++)
{
Printf(“%d = Afghanistan \n”,a);
}
getch();
} 7
Write a program to display the numbers
from (1-100)
#include<stdio.h>
#include<conio.h>
02/10/2025
main()
{
CSF,NU
for(int a=1;a<=100;a++)
Printf(“%d”,a);
getch();
}
8
Sum of Numbers from 1-10
#include<stdio.h>
#include<conio.h>
02/10/2025
main()
{
int b;
CSF,NU
for(int a=1;a<=10;a++)
{
printf("%d",a);
b=a+b;
}
printf("the sum=%d",b);
getch();
}
9
Table
#include<stdio.h>
#include<conio.h>
02/10/2025
main()
{
int b=2;
CSF,NU
for(int a=1;a<=10;a++)
{
printf("%d*%d=%d\n",a,b,a*b);
}
getch();
}
10
Increment and Decrement Operators
The statements of increment and decrement are commonly used in
the for loop.
02/10/2025
The increment (i.e., ++) or decrement (i.e., --) operators are the
frequently used operators which take only one operand.
The increment/decrement operators increase or decrease the value of
CSF,NU
the single operand.
◦ e.g., for (int i = 0; i < 100; i++){ … }
◦ The variable i increase one after each iteration.
11
Comparison of Prefix and Postfix
Increments
The value of the expression (that uses the ++/-- operators) depends on
the position of the operator.
02/10/2025
The value of
j is not
increased
CSF,NU
The value of
j
is increased
5-
Nested Loops
• Usually used to work with two dimensional
arrays (later).
02/10/2025
• Nested loops consist of an outer loop with one
or more inner loops.
CSF,NU
• Each time the outer loop is repeated, the inner
loops are reentered.
13
Nested Loops
Nested loops consist of an outer loop with one or more inner loops.
e.g.,
for (i=1;i<=10;i++) Outer loop
02/10/2025
{
for(j=1;j<=50;j++) Inner loop
CSF,NU
{
…
}
}
The above loop will run for 10*50 iterations.
14
What is the Output?
int a=0, b=0, sum=0;
for(a=0; a<6; a+=2)
02/10/2025
for(b=0; b>4; b--)
sum=sum+1;
CSF,NU
printf(“%d”,sum);
/* Output = */
Sum is 0
15
Loop
#include<stdio.h>
02/10/2025
#include<conio.h>
main()
{
int a, b;
CSF,NU
for(a=1; a<10; a++)
{
for(int b=0;b<a;b++)
printf("*");
printf("\n");
}
}
16
Nested loop
#include<stdio.h>
#include<conio.h>
02/10/2025
main()
{
for (int a=1;a<=10;a++)
CSF,NU
{
for(int b=1;b<=10;b++)
printf("%d*%d= %d \n",a,b,a*b);
printf("\n");
}
} 17
While loop
• Its often the case in programming that you want to do
something a fixed number of times .or
02/10/2025
• It repeats a statement or block while its controlling expression
is true
• while(condition)
CSF,NU
• {
• // body of loop
• }
18
while Loop Flow Chart
02/10/2025
false
Continue
CSF,NU
condition?
true
Statement(s)
Next
Statement 19
CSF,NU 02/10/2025
20
Example
#include<stdio.h>
#include<conio.h>
02/10/2025
main()
{
int i=1;
CSF,NU
while(i<=10)
{
printf("%d \n",i);
i++;
}
getch();
} 21
What is the Output?
#include<stdio.h>
#include<conio.h>
main()
{
int a= 0;
02/10/2025
int sum = 0;
while (a< 100)
{
int b= 0;
CSF,NU
while (b< 100)
{
sum = sum + 1;
b++;
}
a++;
}
printf("Sum is %d\n", sum);
getch();
}
/* Output = */
Sum is 10000 22
Example
#include<stdio.h>
#include<conio.h>
02/10/2025
main()
{
int num;
printf("enter ur num");
CSF,NU
scanf("%d",&num);
while(num<=100)
{
printf("%d",num);
num++;
}
getch();
}
23
Do while loop
• As you just saw, if the conditional expression controlling a while
loop is initially false, then the body of the loop will not be
02/10/2025
executed at all.
• However, sometimes it is desirable to execute the body of a
while loop at least once, even if the conditional expression is
CSF,NU
false to begin.
• The do-while loop always executes its body at least once,
because its conditional expression is at the bottom of the loop.
• We can use the do while loop for unspecific reputation of
statements or an action.
24
Do loop
• Each iteration of the do-while loop first executes the body of the loop
and then evaluates the conditional expression
02/10/2025
• If this expression is true, the loop will repeat Otherwise, the loop
terminates.
CSF,NU
25
do Loop Flow Chart
Statement(s)
02/10/2025
true
Continue
CSF,NU
condition?
false
Next
Statement
26
do Loops (Cont…)
Its general form is:
02/10/2025
do {
// body of loop;
CSF,NU
} while (condition);
27
CSF,NU 02/10/2025
28
do Loops (Cont…)
#include<stdio.h>
#include<conio.h>
02/10/2025
void main()
{
CSF,NU
int n = 10;
do {
printf("num %d ", n);
n--;
} while(n > 1);
getch(); 29
}
Example
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
02/10/2025
main()
{
char name;
CSF,NU
char choice;
do{
printf("enetr ur name");
scanf(" %c",&name);
printf("you have entered %c\n",name);
printf("enter y for again or press any key to cancel\n");
scanf(" %c",&choice);
}while(choice=='y');
getch();
} 30
Other Control Statements
• There are several other control statements in C.
02/10/2025
• We’ve already seen one, break, used in switch statements, but it
can be used other places as well.
CSF,NU
• Another statement, continue, is used only in loops, and the third
control statement is, goto.
31
The break Statements
• The break statement causes an exit from a loop(while, for, do-while),
just as it does from a switch statement.
02/10/2025
• The next statement after the break is executed is the statement
following the loop.
CSF,NU
32
Example
#include<stdio.h>
#include<conio.h>
main()
02/10/2025
{
char name;
printf("enter ur name");
CSF,NU
scanf("%c",&name);
while(name=='a')
{
printf("you have entered correct name");
break;
}
getch();
} 33
Example
#include<stdio.h>
#include<conio.h>
main()
02/10/2025
{
for(int a=0;a<10;a++)
{
CSF,NU
if(a==5)
break;
printf("%d",a);
}
getch();
} 34
The Continue statement
• Sometimes, however, you want to go back to the top of the
loop when something unexpected happens.
• Executing continue has this effect.
02/10/2025
• Skips the remaining statements in the body of a while, for or
do-while structure and proceeds with the next iteration of
CSF,NU
the loop
• In while and do/while, the loop-continuation test is
evaluated immediately after the continue statement is
executed
• In the for structure, the increment expression is executed,
then the loop-continuation test is evaluated
35
The Continue statement
02/10/2025
CSF,NU
36
Example
#include<stdio.h>
#include<conio.h>
02/10/2025
main()
{
for(int a=0;a<10;a++)
CSF,NU
{
if(a==5)
continue;
printf("%d",a);
}
getch();
} 37
The goto Statement
• insert a label in your code at the desired destination for the
goto.
02/10/2025
• The label is always terminated by a colon.
• The keyword goto, followed by this label name, then takes you
to the label.
CSF,NU
• Note: There is almost never any need to use goto.
38
Example
#include<stdio.h>
#include<conio.h>
02/10/2025
main()
{
CSF,NU
printf(“Before goto statement”);
goto myLabel;
printf(“this statement will never be executed”);
myLabel:
printf(“this statement executed after goto statement”);
} 39