Upload
eshikshak
View
1.309
Download
0
Embed Size (px)
DESCRIPTION
comparision of looping statement in c
Citation preview
Comparison of the loop control structures
Prakash KhaireB V Patel Inst. of BMC & IT, GopalVidyanagar
for while do…while
A for loop is used to execute and repeat a statement block depending on a condition which is evaluated at the beginning of the loop.
Example :for(i=1; i<=5; i++){ s = s + i; p = p * i;}
A while loop is used to execute and repeat a statement block depending on a condition which is evaluated at the beginning of the loop.
Examplei=1;while(i <=5 ){ s = s + i; p = p * i; i++;}
A do while loop is used to execute and repeat a statement block depending on a condition which is evaluated at the end of the loop. Examplei=1;do { s = s + i; p = p * i; i++;}while(i<=5);
for while do…whileA variable value is initialized at the beginning of the loop and is used in the condition.
A variable value is initialized at the beginning or before the loop and is used in the condition.
A variable value is initialized before the loop or assigned inside the loop is used in the condition.
A statement to change the value of the condition or to increment the value of the variable is given at the beginning of the loop.
A statement to change the value of the condition or to increment the value of the variable is given inside the loop.
A statement to change the value of the condition or to increment the value of the variable is given inside the loop.
The statement block will not be executed when the value of the condition is false.
The statement block will not be executed when the value of the condition is false.
The statement block will not be executed when the value of the condition is false, but the block is executed at least once irrespective of the value of the condition
for while do…whileA for loop is commonly used by many programmers.
A while loop is also widely used by many programmers.
A do-while loop is used in some cases where the condition need to be checked at the end of the loop.
break Statement* The break statement, when executed in a
while, for, do/while, or switch structure, causes immediate exit for that structure.
* Program execution continues with the first statement after the structure.
* Common uses of the break statement are to escape early from a loop, or to skip the remainder of a switch structure
Continue Statement* The continue statement, when executed
in a while, for, or do/while structure, skips the remaining statements in the body of that structure, and proceeds with the next iteration of the loop.
* In while and do/while structures, the loop-continuation test is evaluated immediately after the continue statement is executed.
* In the for structure, the increment expression is executed, then the loop-continuation test is evaluated.
#include <stdio.h> #include <stdlib.h> int main () { unsigned x; x = 0; while (x < 10) { ++x;
if (x % 2 == 0) {
continue; } printf ("%i is an odd number.\n", x);
} return 0; }