Lecture15 comparisonoftheloopcontrolstructures.ppt

1,121
-1

Published on

comparision of looping statement in c

Published in: Health & Medicine, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,121
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
52
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Lecture15 comparisonoftheloopcontrolstructures.ppt

  1. 1. Comparison of the loop control structures Prakash KhaireB V Patel Inst. of BMC & IT, GopalVidyanagar
  2. 2. for while do…whileA for loop is used to A while loop is used to A do while loop is used toexecute and repeat a execute and repeat a execute and repeat astatement block depending statement block depending statement block dependingon a condition which is on a condition which is on a condition which isevaluated at the beginning evaluated at the beginning evaluated at the end of theof the loop. of the loop. loop.Example : Example Examplefor(i=1; i<=5; i++) i=1; i=1;{ while(i <=5 ) do s = s + i; { { p = p * i; s = s + i; s = s + i;} p = p * i; p = p * i; i++; i++; } }while(i<=5);
  3. 3. for while do…whileA variable value isA variable value isA variable value isinitialized at the beginninginitialized at the beginninginitialized before the loopof the loop and is used inor before the loop and isor assigned inside the loopthe condition. used in the condition. is used in the condition.A statement to change theA statement to change theA statement to change thevalue of the condition or tovalue of the condition or tovalue of the condition or toincrement the value of theincrement the value of theincrement the value of thevariable is given at thevariable is given inside thevariable is given inside thebeginning of the loop. loop. loop.The statement block will The statement block willThe statement block willnot be executed when thenot be executed when thenot be executed when thevalue of the condition isvalue of the condition isvalue of the condition isfalse. false. false, but the block is executed at least once irrespective of the value of the condition
  4. 4. for while do…whileA for loop is commonlyA while loop is also widelyA do-while loop is used inused by manyused by manysome cases where theprogrammers. programmers. condition need to be checked at the end of the loop.
  5. 5. 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
  6. 6. 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.
  7. 7. #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;}

×