8
Comparison of the loop control structures Prakash Khaire B V Patel Inst. of BMC & IT, GopalVidyanagar

Lecture15 comparisonoftheloopcontrolstructures.ppt

Embed Size (px)

DESCRIPTION

comparision of looping statement in c

Citation preview

Page 1: Lecture15 comparisonoftheloopcontrolstructures.ppt

Comparison of the loop control structures

Prakash KhaireB V Patel Inst. of BMC & IT, GopalVidyanagar

Page 2: Lecture15 comparisonoftheloopcontrolstructures.ppt

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);

Page 3: Lecture15 comparisonoftheloopcontrolstructures.ppt

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

Page 4: Lecture15 comparisonoftheloopcontrolstructures.ppt

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.

Page 5: Lecture15 comparisonoftheloopcontrolstructures.ppt

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

Page 6: Lecture15 comparisonoftheloopcontrolstructures.ppt

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.

Page 7: Lecture15 comparisonoftheloopcontrolstructures.ppt

#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; }

Page 8: Lecture15 comparisonoftheloopcontrolstructures.ppt