break and continue
These interrupt normal flow of control
break causes
an exit from the innermost enclosing loop
continue causes
the current iteration of a loop to stop and the next iteration to begin
immediately
do-while
§When a loop is constructed using
while, the test for continuation is
carried out at the beginning of each
pass
while, the test for continuation is
carried out at the beginning of each
pass
§With do-while the test for continuation
takes place at the end of each pass
takes place at the end of each pass
do
{
statement
} while (expression);
Example
int i = 1,
sum = 0;
do
{
sum = sum + i;
i= i + 1;
}while(i<=10);
printf(“Sum
= %d\n”, sum);
while vs. do-while
§while -- the expression is tested first, if
the result is false, the loop body is never
executed
the result is false, the loop body is never
executed
§do-while -- the loop body is always
executed once. After that, the expression is tested,
if the result is false, the loop body is not executed
again.
executed once. After that, the expression is tested,
if the result is false, the loop body is not executed
again.
No comments:
Post a Comment