Looping Statements
Looping statements
Looping is used to repeat an action a required number of times,
which may be determined by some condition or known appropriate
value. Any looping statement has three characteristics.
(i) Initilaization of an index to keep track of the count
(ii) Terminating condition
(iii) updation of the index.
The while Loop
•An action is repeated while some condition remains true
that is, loop repeated until condition becomes false
Format: while ( Expression)
{
statement 1;
}
statement 2;
When program execution reaches a while statement, the following
events occur:
1. The expression condition is evaluated.
2. If condition evaluates to false (that is, zero), the while statement
terminates, and execution passes to the first statement following
it.
3. If condition evaluates to true (that is, nonzero), the C
statement(s) in statement1 are executed and execution returns to
step 1.
Example:
int product = 2;
while ( product <= 1000 )
product = 2 * product;
In the above code the variable product is initialized to 2. The while
loop gets executed as long as the value of the variable is less than or
equal to 1000. The loop doubles the value of product every time it
executes.
The do-while Loop
•Similar to the while construct
•But condition for repetition tested after the body of the loop is
performed
•All actions are performed at least once
Format:
do {
statement1;
} while (expression);
statement2;
Example:
int counter = 1;
do
{
printf( "%d ", counter );
} while (++counter <= 10);
1 2 3 4 5 6 7 8 9 10
The above code first prints 1, increments the value of counter by 1,
then checks the condition (counter <= 10). If the condition evaluates
to true, the loop is executed. The loop is executed till the condition
becomes false. Thus integers from 1 to 10 are printed on standard
output device.
Note: Initialization, loop-continuation, and increment can contain
arithmetic expressions. For example,
main()
{ int x=2, y=10;
for(j=x; j<=4*x*y; j+=y/x)
is equivalent to:
for(j=2; j<=80; j+=5)
Example : (i)Write a program to find the sum and product of first n
natural numbers using all three loops.
(ii) Write a program to read 5 integers, find and display their average.
Extend it to find the average of n numbers.
1. #include<stdio.h>
int main()
{
float a=5.2;
if(a == 5.2) NOTE:Float data type doesnt work with == operator , hence the
output is not equal.
{ The same way %(modulo operator) also can’t be used with float
printf("equal"); data type.
}
else
{
printf("not equal");
}
}
What will be the output of the following C program?
#include <stdio.h>
int i=10;
void main()
{ 20
int i =20,n;
for(n=0;n<=i;n++)
{
int i=10;
i++;
}
printf("%d", i);
3. What will be output of this C snippet?
void main()
{
int i,j=1;
for(i=0;i<5;i++); 5 2
{
j=j+1;
}
printf("%d %d",i,j);
}
4. Find the output of the c program
#include <stdio.h>
void main()
{
int i=20,j,k=0;
for(j=1;j >i;j++)
{
k+=j<10?4:3; 0
}
printf("%d", k);
}
5. Find the output?
#include<stdio.h>
int main() loop loop loop loop loop
{ 10 1
int i = 1, j = 1;
for(--i && j++ ; i<10; i+=2)
{
printf("loop ");
}
printf("\n%d\t%d ",i,j);
return 0;
}
6. Find the output of given
program below
#include<stdio.h> Infinite loop with “Hello” printed
int main()
{
for(5;2;2)
printf("Hello");
return 0;
}
7. Find the output of given program below
#include<stdio.h> 8. int main()
int main() {
{ int x = 3, k;
for(5;2;2) ; while (x-- >= 0) {
printf("Hello"); printf("%d", x);
return 0; }
} return 0;
}
Infinite loop with no output 2 1 0 -1
9. #include <stdio.h>
int main() 10. int main()
{ {
int c = 5, no = 10; int i;
do { for(i=0;i<5;i++);
no /= c; {
} while(c--); printf("hello\n");
}
printf ("%d\n", no); }
return 0; NOTE: Semicolon isn't an "operator". It terminates a "statement",
and it's valid for the statement to contain nothing.
}
o/p : hello
11. int main()
{ 12. Observe the output
int i;
int main()
for(i=0;i<5;i++)
{ {
printf("hello\n"); int i;
} for(;i<5;)
} {
printf("hello\n");
o/p: }
hello
hello
}
hello
hello
hello
Infinite loop
13. Observe the 14. Observe the
output output
int main() int main()
{ {
int i; int i;
for(i=0;i<5;) for(; ;i++)
{ {
printf("hello\n"); printf("hello\n");
} }
} }
Infinite loop
#include<stdio.h>
int main()
{
int i;
for(i=1; i<=10; i++)
{
if(i==5)
{
break;
}
printf("%d\t", i); //1 2 3 4 break
}
#include<stdio.h>
int main()
{
int i;
for(i=1; i<=10; i++)
{
if(i==5)
{
continue;
}
printf("%d\t", i);
}
// 1 2 3 4 6 7 8 9 10 continue
}
#include<stdio.h>
int main()
{
int count=0, i,j;
for(i=1; i<=3; i++)
{
for(j=1; j<=3; j++)
count++;
} 9
printf("%d\n", count);
}
#include<stdio.h>
int main()
{
int count=0, i,j;
for(i=1; i<=3; i++)
{
for(j=1; j<=3; j++);
count++;
} 3
printf("%d\n", count);
}
#include<stdio.h>
int main()
{
int count=0, i,j;
for(i=1; i<=3; i++);
{
for(j=1; j<=3; j++);
count++;
} 1
printf("%d\n", count);
}
#include<stdio.h>
int main()
{
int count=50, i;
for(; count; count-10);
{
printf(“\nloop);
}
}
Infinite loop with no output
#include<stdio.h>
int main()
{
int count=50, i;
for(; count; count>>=2);
{
printf(“\nloop);
}
}
loop
#include<stdio.h>
int main()
{
int count=50, i;
for(; count; count>>=2)
{
printf(“\nloop);
}
}
loop
loop
loop
3. Find the output of the give program
#include <stdio.h>
int main()
{
int ip = 5;
switch (ip)
{
case 0+1: printf("Blue");
break;
case 1+2: printf("Red");
Ans: Green
break;
case 2+3: printf("Green");
break;
default: printf("Yellow");
}
return 0;
}