Introduction To Looping Control Structures - 1
Introduction To Looping Control Structures - 1
A repetition structure allows the programmer to specify that an action be repeated a number
of times, depending
on the value of a condition. A Loop is used to repeat the same process multiple times until it
meets the specified
condition in a program. By using a loop in a program, a programmer can repeat any number
of statements
up to the desired number of repetitions. A loop also provides the suitability to a programmer
to repeat the
statement in a program according to the requirement. A loop is also used to reduce
the program complexity, easy to understand, and easy to debug.
Advantages of loops
o It provides code iteration functionality in a program.
o It executes the
statement until the
specified condition is true.
o It helps in reducing the
size of the code.
1. for loop
2. while loop
3. do...while loop
while loop
while (testExpression)
{
// statements inside the body of the loop
}
The while loop evaluates the test expression inside the parenthesis ().
If the test expression is true, statements inside the body of while loop are executed. Then,
the test expression is evaluated again.
The process goes on until the test expression is evaluated to false.
If the test expression is false, the loop terminates (ends).
A conditional expression is used to check the condition. The statements defined inside the
while loop will repeatedly execute until the given condition fails.
The condition will be true if it returns 0. The condition will be false if it returns any non-
zero number.
In while loop, the condition expression is compulsory.
Running a while loop without a body is possible.
We can have more than one conditional expression in while loop.
If the loop body contains only one statement, then the braces are optional.
#include <stdio.h>
intmain()
{
inti = 1;
while (i<= 5)
{
printf("%d\n", i);
++i;
}
return 0;
}
Output
1
2
3
4
5
1. When i is 1, the test expression i<= 5 is true. Hence, the body of the while loop is
executed. This prints 1 on the screen and the value of i is increased to 2.
2. Now, i is 2, the test expression i<= 5 is again true. The body of the while loop is executed
again. This prints 2 on the screen and the value of i is increased to 3.
3. This process goes on until i becomes 6. When i is 6, the test expression i<= 5 will be false
and the loop terminates.
Example 2
#include <stdio.h>
int main(void)
{
int counter;
int marks;
int total;
int average;
total = 0;
counter =1;
while (counter<=10)
printf(“enter marks”);
scanf (“%d”,&marks);
total= total +marks;
counter = counter+1;
average=total/counter;
printf(“class average is %d\n”, average);
return 0;
}
Example 3
program to calculate sum of even numbers between 20 and 40
#include <stdio.h>
intmain()
{
Int i;
int sum=0;
i=20;
while(i<=40)
{
if(i%2==0)
{
printf(“%d is even\n”, i);
sum=sum+i;
}
i++;
}
printf(“sum of even numbers between 20 and 40 =%d\n”,sum);
}
return 0;
}
Example 4:
#include<stdio.h>
int main(){
int i=1,number=0,b=9;
printf("Enter a number: ");
scanf("%d",&number);
while(i<=10){
printf("%d \n",(number*i));
i++;
}
return 0;
}
Output
Enter a number: 50
50
100
150
200
250
300
350
400
450
500