Looping in Programming Language C
Looping in Programming Language C
Name: S. Ashutosh
Looping in C
• Definition: In programming, a loop is used to repeat a block of code until the specified
condition is met.
• A loop statement allows us to execute a statement or group of statements multiple
times
• programming has three types of loops:
– for loop
– while loop
– do...while loop
• For-loop
– This is one of the most frequently used loops in C programming.
Syntax of for loop:
for (initialization; condition test; increment or decrement)
{
//Statements to be executed repeatedly
}
Flow Diagram of for-loop
Output:
00
01
02
10
11
12
• while loop
– Syntax of while loop:
while (condition test)
{
//Statements to be executed repeatedly
// Increment (++) or Decrement (--) Operation
}
Flow chart of while loop
• Description:
step1: The variable count is initialized with value 1 and then it has been tested for the
condition.
step2: If the condition returns true then the statements inside the body of while loop
are executed else control comes out of the loop.
step3: The value of count is incremented using ++ operator then it has been tested
again for the loop condition.
• Example of while loop
#include <stdio.h>
int main()
{
int count=1;
while (count <= 4)
{
printf("%d ", count); count++;
}
return 0;
}
Output:
1234
Example 1:
#include <stdio.h>
int main()
{
int var = 6;
while (var >=5)
{
printf("%d", var); var++;
}
return 0;
}
Infinite loop: var will always have value >=5 so the loop would never end.
Example 2:
#include <stdio.h>
int main()
{
int var =5;
while (var <=10)
{
printf("%d", var);
var--;
}
return 0;
}
Infinite loop: var value will keep decreasing because of –- operator, hence it will always
be <= 10.
Use of Logical operators in while loop
• Just like relational operators (<, >, >=, <=, ! =, ==), we can also use logical operators in
while loop.
The following scenarios are valid :
• while(num1<=10 && num2<=10)
-using AND(&&) operator, which means both the conditions should be true.
• while(num1<=10||num2<=10)
– OR(||) operator, this loop will run until both conditions return false.
• while(num1!=num2 &&num1 <=num2)
– Here we are using two logical operators NOT (!) and AND(&&).
• while(num1!=10 ||num2>=num1)
Example of while loop using logical operator
#include <stdio.h>
int main()
{
int i=1, j=1;
while (i <= 4 || j <= 3)
{
printf("%d %d\n",i, j);
i++; j++;
}
return 0;
}
Output:
11
22
33
44
• do..while loop
– Syntax of do-while loop
Do
{
//Statements
}while(condition test);
– Flow diagram of do while loop
Example:
#include <stdio.h>
int main()
{
int counter=10;
while (counter >=0)
{
if (counter==7)
{
counter--; continue;
}
printf("%d ", counter);
counter--;
}
return 0;
}
Output:
10 9 8 6 5 4 3 2 1 0
The print statement is skipped when counter value was 7.
break statement
• It is used
– to come out of the loop instantly. When a break statement is encountered
inside a loop, the control directly comes out of loop and the loop gets
terminated. It is used with if statement, whenever used inside loop.
– in switch case control structure. Whenever it is encountered in switch-case
block, the control comes out of the switch-case.
• Syntax:
break;
• Flow diagram of break statement
Example – Use of break in a while loop
#include <stdio.h>
int main()
{
int num =0;
while(num<=100)
{
printf("value of variable num is: %d\n", num);
if (num==2)
{
break;
} num++;
}
printf("Out of while-loop");
return 0;
}
Output:
value of variable num is: 0
value of variable num is: 1
value of variable num is: 2
Out of while-loop
Example – Use of break statement in switch-case
#include <stdio.h>
int main()
{
int num;
printf("Enter value of num:"); scanf("%d",&num);
switch (num)
{
case 1
printf("You have entered value 1\n");
break;
case 2:
printf("You have entered value 2\n");
break;
case 3:
printf("You have entered value 3\n");
break;
default:
printf("Input value is other than 1,2 & 3 ");
}
return 0;
}
Output:
Enter value of num:2
You have entered value 2
if we don’t use the break statement after every case block then the output of this
program would be:
Enter value of num: 2
You have entered value 2
You have entered value 3
Input value is other than 1, 2 & 3