Loops Flow
Loops Flow
For Loop:
Initialization
Condi- FALSE
tion
TRUE
Statements
Incrementation
Rest of Program
While Loop:
Condi- FALSE
tion
TRUE
Page 1 of 5
do..while Loop:
Statements
Condi- FALSE
tion
Page 2 of 5
break statement:
The break keyword used to terminate a loop. It terminates the loop statements block execution
though the loop condition present stuts is true.
Generally, break appears in association with a condition. The condition that is different than loop
condition.
The break can also be used to terminate execution of indefinite loops.
The following is the format of indefinite loops.
for ( ; ; ) /* no initialization, condition and incrementation */
{
statements;
}
do
{
statements;
} while(1);
The following program verifies if the given number num is prime. The for loop terminates when the
first divisor found between 2 and num-1.
#include <stdio.h>
void main()
{
int num, I;
printf(“Enter number : “);
scanf(“%d”, &num);
Page 3 of 5
Another example program to illustreate break given below.
continue:
continue is another keyword that can be used in loops.
The continue keyword skips execution of the statements block that exists after it.
The continue generally used in association with a condition where the condition differ from
loop condition.
In for loop, when continue executed, it sends the control to loop incrementation (step)
statement.
In while and do..while loops, continue sends the control to verify the loop condition status.
statements-II;
}
while (condition)
{
statements-I;
if (condition)
continue;
statements-II;
}
do
{
statements-I;
if (condition)
continue;
statements-II;
} while (condition);
Statements-II block execution skipped when continue is executed.
Page 4 of 5
The following is example program to illustrate use of continue keyword.
The program adds sum of even numbers. It skips adding the number to sum when the number is
odd.
void main()
{
int num, i, sum=0;
if (num % 2 != 0 )
continue;
sum+=num;
}
printf("\nSum of even numbers : %d", sum);
}
Page 5 of 5