Loop Control Statements 129
6.10 Create an infinite for loop. Check each value of the for loop. If the value is even, display
it otherwise continue with iterations. Print even numbers from 1 to 21. Use break
statement to terminate the program.
# include <stdio.h>
# include <conio.h>
void main()
{
int i = l ;
c l r s c r ();
printf ("
t Table o f Even numbers from 1 to 20")}
printf ("
===== == ==== ======= ==== = == ==n");
fo r ( ;;)
{
if (i~21)
break;
else if (i%2~0)
{
printf ("%dt",i);
i++;
continue;
}
else
I
/++;
continue;
I
I
I
qu j zu ii
Table of Even numbers from 1 to 20
2 4 6 8 10 12 14 16 18
Explanation In the above programbreak and continue both statements are in use. The program
displays only even numbers from 1 to 21. An infinite loop is created and in the loop variable ' i ' is
increased. The value of ' i ' is checked every time. If it is even, it is printed otherwise continue
statement is executed, which passes control at the beginning of the for loop. When the value o f ' i '
reaches to 21 the break statement is executed and it terminates the program.
6.11 Calculate the sum of first five numbers and their squares. Display their results,
m ain()
{
in t i , sum=0, sqsum =0;
c l r s c r ( );