Structured Programming Language: The Loop
Structured Programming Language: The Loop
Structured Programming Language: The Loop
Lecture 05
The Loop
loop
• Loops are used in C for repeating some portion of the
program either a specified number of times or until a
particular condition is being satisfied.
• A loop statement allows us to execute a statement or
group of statements multiple times.
• A loop is used for executing a block of statements
repeatedly until a given condition returns false.
loop
• There are three methods in C for repeating a
part of program
1. Using a for statement
2. Using a while statement
3. Using a do..while statement
Sr.No. Loop Type & Description
1 while loop: Repeats a statement or group of statements while a
given condition is true. It tests the condition before executing the
loop body.
2 for loop: Executes a sequence of statements multiple times and
abbreviates the code that manages the loop variable.
4 nested loops: You can use one or more loops inside any other
while, for, or do..while loop.
loop
• A looping process would include the following
four steps:
– Setting and initialization of a counter
– Test for a specified condition for execution of
the loop
– Execution of the statement in the loop
– Incrementing/decrementing the counter
The for loop
For example -
Observing for loops
Output:
* 1+2+3+…+n
#include <stdio.h>
void main()
{
int num, count, sum = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
// for loop terminates when n is less than count
for(count = 1; count <= num; count++)
{
sum += count;
}
printf("Sum = %d", sum);
}
* 3+11+19+…+1691
#include<stdio.h>
void main()
{
int i,n,sum = 0;
printf("Sum Of Series");
for(i=3;i<=1691;i=i+8)
{
sum = sum+i;
}
printf("sum = %d",sum);
}
What will be the output?
#include<stdio.h>
void main ()
{
int n,num,min,i;
min=5;
num=2;
for (i=1; i<5; i++)
{
if (num<i)
{
min=num;
printf(“Okay %d Bye\n",min);
}
printf(“Good %d Hello\n",min);
}
}
Nested for loop
• A for loop inside another for loop is called
nested for loop.
• We can have any number of nested loops as
required.
• Just like decision control instructions, a loop
control instruction can also be nested easily.
Syntax of nested for loop
• The syntax for a nested for loop statement in C is as
follows −
row = 1 col = 1
row = 1 col = 2
row = 1 col = 3
row = 2 col = 1
row = 2 col = 2
row = 2 col = 3
row = 3 col = 1
row = 3 col = 2
row = 3 col = 3
Sample Program
#include<stdio.h> ***
void main()
{ ***
int i,j;
for(j=1;j<=3;j++) ***
{
for(i=1;i<=3;i=i+1)
{
printf("*");
}
printf("\n");
}
}
Loop Control Statements
Loop control statements change execution from its normal sequence. When
execution leaves a scope, all automatic objects that were created in that scope
are destroyed.
C supports the following control statements.
#include <stdio.h>
void main () {
int a = 1;
while( a <= 10 ) {
printf("value of a: %d\n", a);
a++;
}
}
Find the output of the program
#include <stdio.h>
main()
{ Output:
int i = 10; Hello 10
while ( i > 0 ) Hello 9
Hello 8
{ Hello 7
printf("Hello %d\n", i );
i = i - 1;
if( i == 6 )
{
break;
}
}
}
The do…while loop
The do…while Loop
3. If the condition is not true first time If the condition is not Even if the condition is
than control will never enter in a loop true first time than not true for the first
control will never time the control will
enter in a loop. enter in a loop.