1 / 28

Loop (2) C Programming 2011

Loop (2) C Programming 2011. For Loop. SYNTAX for ( initialization ; test expression ; update ) { 0 or more statements to repeat }. The for loop contains an initialization an expression to test for continuing an update to execute after each iteration of the body.

Download Presentation

Loop (2) C Programming 2011

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Loop (2) C Programming 2011

  2. For Loop SYNTAX for ( initialization ; test expression ; update ) { 0 or more statements to repeat }

  3. The for loop contains • an initialization • an expression to test for continuing • an update to execute after each iteration of the body

  4. Example of Repetition int num; for ( num = 1 ; num <= 3 ; num++ ) { printf( “ % d Potato \n ” , num ); }

  5. Example of Repetition ? num int num; for ( num = 1 ; num <= 3 ; num++ ) printf( “ % d Potato \n ” , num ); OUTPUT

  6. Example of Repetition 1 num int num; for ( num = 1 ; num <= 3 ; num++ ) printf( “ % d Potato \n ” , num ); OUTPUT

  7. Example of Repetition 1 num true int num; for ( num = 1 ;num <= 3; num++ ) printf( “ % d Potato \n ” , num ); OUTPUT

  8. 1Potato Example of Repetition 1 num int num; for ( num = 1 ; num <= 3 ; num++ ) printf( “ % d Potato \n ” , num ); OUTPUT

  9. 1Potato Example of Repetition 2 num int num; for ( num = 1 ; num <= 3 ;num++) printf( “ % d Potato \n ” , num ); OUTPUT

  10. 1Potato Example of Repetition 2 num true int num; for ( num = 1 ;num <= 3; num++ ) printf( “ % d Potato \n ” , num ); OUTPUT

  11. 1Potato 2Potato Example of Repetition 2 num int num; for ( num = 1 ; num <= 3 ; num++ ) printf( “ % d Potato \n ” , num ); OUTPUT

  12. 1Potato 2Potato Example of Repetition 3 num int num; for ( num = 1 ; num <= 3 ;num++) printf( “ % d Potato \n ” , num ); OUTPUT

  13. 1Potato 2Potato Example of Repetition 3 num true int num; for ( num = 1 ;num <= 3; num++ ) printf( “ % d Potato \n ” , num ); OUTPUT

  14. 1Potato 2Potato 3Potato Example of Repetition 3 num int num; for ( num = 1 ; num <= 3 ; num++ ) printf( “ % d Potato \n ” , num ); OUTPUT

  15. 1Potato 2Potato 3Potato Example of Repetition 4 num int num; for ( num = 1 ; num <= 3 ;num++) printf( “ % d Potato \n ” , num ); OUTPUT

  16. 1Potato 2Potato 3Potato Example of Repetition 4 num false int num; for ( num = 1 ;num <= 3; num++ ) printf( “ % d Potato \n ” , num ); OUTPUT

  17. Example of Repetition 4 num false int num; for ( num = 1 ;num <= 3; num++ ) printf( “ % d Potato \n ” , num ); When the loop control condition is evaluated and has value false, the loop is said to be “satisfied” and control passes to the statement following the For statement.

  18. Count-controlled Loop int count ; for ( count = 4 ; count > 0 ; count-- ) { printf( “ %d \n “ , count ) ; } printf ( “Done” ) ; OUTPUT: 4 3 2 1 Done

  19. What is the output? int count; for ( count = 0 ; count < 10 ; count++ ) { printf( “*” ); }

  20. What output from this loop? int count; for (count = 0; count < 10; count++) ; { printf ( “*” ); }

  21. OUTPUT • no output from the for loop! Why? • the ; right after the ( ) means that the body statement is a null statement • in general, the Body of the for loop is whatever statement immediately follows the ( )

  22. Find the output for ( m=1 ; m<=5 ; m=m+1 ) { x = pow ( m , 2 ); printf(“ %d %d \n “, m , x ); }

  23. Find the output for ( m=1 ; m<=9 ; m=m+2 ) { x = pow ( m , 2 ); printf(“ %d %d \n “, m , x ); }

  24. Example Write a program to ask the user for a positive integer, and then display its factorial. Given that factorial(n) is 1 X 2 X 3 …… x n

  25. Example Write a program to display all the numbers divisible by 5 in the range 0 to 5000.

  26. Nested Loops initialize outer loop while (outer loop condition) { . . . initialize inner loop while (inner loop condition) { inner loop processing and update } . . . }

  27. 1 5 18 14 15 17 12 2 2 19 21 3 3 23 22 26 ... Temperature example A program calculates the average temperature for a city, based upon the average temperature of each month. Each month has a different number of temperature readings. Month howMany Readings

  28. Example Write a program that displays the multiplication tables ( 1 - 12 ). 1 x 1 = 1 1 x 2 = 2 …. 1 x 12 = 12 2 x 1 = 2 …. 12 x 12 = 144

More Related