290 likes | 413 Views
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.
E N D
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
Example of Repetition int num; for ( num = 1 ; num <= 3 ; num++ ) { printf( “ % d Potato \n ” , num ); }
Example of Repetition ? num int num; for ( num = 1 ; num <= 3 ; num++ ) printf( “ % d Potato \n ” , num ); OUTPUT
Example of Repetition 1 num int num; for ( num = 1 ; num <= 3 ; num++ ) printf( “ % d Potato \n ” , num ); OUTPUT
Example of Repetition 1 num true int num; for ( num = 1 ;num <= 3; num++ ) printf( “ % d Potato \n ” , num ); OUTPUT
1Potato Example of Repetition 1 num int num; for ( num = 1 ; num <= 3 ; num++ ) printf( “ % d Potato \n ” , num ); OUTPUT
1Potato Example of Repetition 2 num int num; for ( num = 1 ; num <= 3 ;num++) printf( “ % d Potato \n ” , num ); OUTPUT
1Potato Example of Repetition 2 num true int num; for ( num = 1 ;num <= 3; num++ ) printf( “ % d Potato \n ” , num ); OUTPUT
1Potato 2Potato Example of Repetition 2 num int num; for ( num = 1 ; num <= 3 ; num++ ) printf( “ % d Potato \n ” , num ); OUTPUT
1Potato 2Potato Example of Repetition 3 num int num; for ( num = 1 ; num <= 3 ;num++) printf( “ % d Potato \n ” , num ); OUTPUT
1Potato 2Potato Example of Repetition 3 num true int num; for ( num = 1 ;num <= 3; num++ ) printf( “ % d Potato \n ” , num ); OUTPUT
1Potato 2Potato 3Potato Example of Repetition 3 num int num; for ( num = 1 ; num <= 3 ; num++ ) printf( “ % d Potato \n ” , num ); OUTPUT
1Potato 2Potato 3Potato Example of Repetition 4 num int num; for ( num = 1 ; num <= 3 ;num++) printf( “ % d Potato \n ” , num ); OUTPUT
1Potato 2Potato 3Potato Example of Repetition 4 num false int num; for ( num = 1 ;num <= 3; num++ ) printf( “ % d Potato \n ” , num ); OUTPUT
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.
Count-controlled Loop int count ; for ( count = 4 ; count > 0 ; count-- ) { printf( “ %d \n “ , count ) ; } printf ( “Done” ) ; OUTPUT: 4 3 2 1 Done
What is the output? int count; for ( count = 0 ; count < 10 ; count++ ) { printf( “*” ); }
What output from this loop? int count; for (count = 0; count < 10; count++) ; { printf ( “*” ); }
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 ( )
Find the output for ( m=1 ; m<=5 ; m=m+1 ) { x = pow ( m , 2 ); printf(“ %d %d \n “, m , x ); }
Find the output for ( m=1 ; m<=9 ; m=m+2 ) { x = pow ( m , 2 ); printf(“ %d %d \n “, m , x ); }
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
Example Write a program to display all the numbers divisible by 5 in the range 0 to 5000.
Nested Loops initialize outer loop while (outer loop condition) { . . . initialize inner loop while (inner loop condition) { inner loop processing and update } . . . }
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
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