Professional Documents
Culture Documents
4 Topic Four - Looping
4 Topic Four - Looping
4 Topic Four - Looping
#include <stdio.h>
main(){
int x = 10;
do {
printf("This printf statement is executed at least once\n");
x++;
} while ( x < 10 ); //end do while loop
while ( x < 10 ) {
printf("This printf statement is never executed\n");
x++;
} //end while loop
} //end main function
Now let us see a program which uses do while loop for
printing numbers 1 to 10.
#include <stdio.h>
int main()
{
int i=1;
do
{
printf(“Number is: %d\n”,i);
i=i+1;
}
while( i<=10);
return 0;
}
THE FOR LOOP
for loop is commonly used in building loops
when the number of iterations is already known.
#include <stdio.h>
main()
{
int x;
for ( x = 10; x > 5; x-- )
printf("The value of x is %d\n", x);
} //end main function
A single for loop statement contains three separate
expressions, as described in the following bulleted list.
• Variable initialization
• Conditional expression
• Increment/decrement
In the example, the first expression, variable
initialization, initializes the variable to 1.It was not
initialized in the variable declaration statement because
it would have been a duplicated and wasted effort. The
next expression is a condition (x > 5) that is used to
determine when the for loop should stop iterating. The
last expression in the for loop (x--) decrements the
variable x by 1.
Now let us see a program which uses for loop for
printing numbers 1 to 10.
#include <stdio.h>
int main()
{
int i;
for( i=1; i<= 10; i++)
{
printf(“Number: %d\n”,i);
}
return 0;
}
You may write a program using for loop for finding the sum
of a series:
1+2+3+4+……………..+n. This program will be like:
//Program for finding sum of series: 1+2+3+4+……………..+n
#include <stdio.h>
main(){
int i,n,sum=0;
printf(“Give the Number of n:”);
scanf(“%d”,&n);
for( i=1; i<= n; i++){
sum = sum+i;}
printf(“The sum of the series is: %d”,sum);
return 0;}
Write a program that will display even numbers
between 10 and 50
#include<stdio.h>
main(){
int a;
for(a=12;a<50;a+=2){
printf("%d\n",a);}
return 0;}
Convert the following while loop to an equivalent for
loop:
#include <stdio.h>
int main() {
int x=1;
int y;
while (x <=10) {
y=x*x;
printf("%d %d \n",x,y);
x+=3; }
return 0; }
BREAK AND CONTINUE STATEMENTS
The break and continue statements are used to manipulate program
flow in structures such as loops.
When a break statement is executed in a loop, the loop is terminated
and program control returns to the next statement following the end
of the loop.
#include <stdio.h>
main(){
int x;
for ( x = 10; x > 5; x-- ) {
if ( x == 7 )
break;
} //end for loop
printf(“\n%d\n”, x);
return 0;}
In this program, the condition (x == 7) becomes true after the third
iteration. Next, the break statement is executed and program control is
sent out from the for loop and continues with the printf statement.
continue statement
The continue statement is also used to manipulate program
flow in a loop structure. When executed, though, any
remaining statements in the loop are passed over and the
next iteration of the loop is sought.
The next program block demonstrates the continue
statement.
#include <stdio.h>
main(){
int x;
for ( x = 10; x > 5; x-- ) {
if ( x == 7 )
continue;
printf("\n%d\n", x);
} //end for loop
return 0;}
when executed number 7 won’t appear in the
output because when the condition x == 7 is
true, the continue statement is executed, thus
skipping the printf() function and continuing
program flow with the next iteration of the for
loop.