Loops
Loops
problem solving
using C
Dr Manaswini Jena 1
Looping Statements in C
• The purpose of the C loop is to repeat the same code a number
of times – It executes a sequence of statements many times until
the stated condition becomes false.
4
While loop and do…while Loop
while (condition) do
{ {
//instruction body //instruction body
//statement 1; //statement 1;
//statement 2; //statement 2;
} }
while(condition); 5
while loop do…while loop
6
Syntax of ‘for’ Loop
for loop
7
for loop
8
Comparing for loop and while loop
9
Comparing all the loops
Sl.
Loop Type Description
No.
• In while loop, a condition is evaluated before
processing a body of the loop.
1. While Loop • If a condition is true then and only then the body of
a loop is executed.
#include<stdio.h> Output:
int main() 1
{ 2
int number; 3
for(number=1;number<=10;number++) 4
//for loop to print 1-10 numbers 5
{ 6
printf("%d\n",number); 7
} 8
return 0; 9
} 10 16
For loop execution process
• We have declared a variable of an int data type to store
values.
• In for loop, in the initialization part, we have assigned value 1
to the variable number. In the condition part, we have
specified our condition and then the increment part.
• In the body of a loop, we have a print function to print the
numbers on a new line in the console. We have the value one
stored in number, after the first iteration the value will be
incremented, and it will become 2. Now the variable number
has the value 2. The condition will be rechecked and since the
condition is true loop will be executed, and it will print two on
the screen. This loop will keep on executing until the value of
the variable becomes 10. After that, the loop will be
terminated, and a series of 1-10 will be printed on the screen. 17
Some more possible formats in for loop
• It can have multiple expressions separated by commas in each
part, For example:
do {
statement(s); Nested
do {
do…while
statement(s); loop syntax
}while( condition );
21
}while( condition )
WAP to output multiplication tables
#include <stdio.h>
int main() 1x0=0
1x1=1
{
1x2=2
int i, j; 1x3=3
int table = 2; 1x4=4
1x5=5
int max = 5;
for (i = 1; i <= table; i++) { // outer loop 2x0=0
for (j = 0; j <= max; j++) { // inner loop 2x1=2
2x2=4
printf("%d x %d = %d\n", i, j, i*j);
2x3=6
} 2x4=8
printf("\n"); /* blank line between tables */ 2 x 5 = 10
22
}
}
Infinite loop
• A loop that repeats indefinitely and does not terminate is
called an “Infinite loop” or “Endless loop.”
• So, when the control conditions are not well defined, the
loop does not stop executing and processes the
statements number of times, then it leads to an infinite
loop.
• Following are some characteristics of an infinite loop:
1. No termination condition is specified.
2. The specified conditions never meet.
23
Example of infinite loop
• Here the value of i is not updated.
int i = 1; So after each iteration, the value
while(i<10) of i remains the same. As a result,
the condition (i<10) will always be
{ true.
printf("%d\n", i); • For the loop to work correctly i++;
} is to be added, just after the
printf() statement.
//loop statements.
continue;
//some lines of the code which is to be skipped
26
Example for using break
#include <stdio.h>
int main() Output:
{
int num = 5; 5
while (num > 0) 4
{
if (num == 3)
break;
printf("%d\n", num);
num--;
}
} 27
Example for using continue
#include <stdio.h>
int main() Output:
{
6
int nb = 7;
4
while (nb > 0) 3
{ 2
nb--; 1
if (nb == 5)
continue;
printf("%d\n", nb);
} 28