Presentation is loading. Please wait.

Presentation is loading. Please wait.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures.

Similar presentations


Presentation on theme: "Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures."— Presentation transcript:

1 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10

2 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 2Winter Quarter Repetition Structures Repetition structures are used to repeat statements or blocks of code. The decision whether to repeat the code is based on the evaluation of a logical expression. If the expression is true, the code is executed. If false, the code is not executed. Repetition structures may repeat the code a definite number of times (usually for loops) or an indefinite number of times (usually while or do while loops).

3 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 3Winter Quarter Repetition Structures Both for loops and while loops are top test loops. They evaluate the logical expression before executing any of the code in the loop. If the expression is false, the block of code does not get executed. A do while loop is a bottom test loop. It executes the block of code once and then evaluates the logical expression to determine whether or not to execute it again.

4 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 4Winter Quarter While Loops (Syntax) The syntax of a while loop is: while ( logical expression is true ) statement ; or while ( logical expression is true ) { statement ; }

5 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 5Winter Quarter While Loops /* This program associates letter grades with numeric test scores. It continues to request input until the user types in a 0 and then it terminates with a message. */ #include int main ( ) { int score ;

6 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 6Winter Quarter While Loops score = -1 ; while (score != 0) { printf ("enter a test score (0 to quit) >") ; scanf ("%d", &score) ; if (score >= 90) printf ("Your score of %d is a A\n", score) ; else if (score >= 80 && score <90) printf ("Your score of %d is a B\n", score) ; else if (score >= 70) printf ("Your score of %d is a C\n", score) ;

7 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 7Winter Quarter While Loops else if (score >= 60) printf ("Your score of %d is a D\n", score) ; else if (score != 0) printf ("Your score of %d is an E\n", score) ; else printf ("Terminating at your request.\n\n") ; }/* End of While loop */ }/* End of "main" function */

8 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 8Winter Quarter Do-While Loops (Syntax) do statement ; while ( logical expr is true ) ; or, more commonly do { statement ; } while ( logical expression is true ) ;

9 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 9Winter Quarter Do-While Loops /* This program associates letter grades with numeric test scores. It continues to request input until the user types in a 0 and then it terminates with a message. */ #include int main ( ) { int score;

10 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 10Winter Quarter Do-While Loops do { printf ("enter a test score (0 to quit) >") ; scanf ("%d", &score) ; if (score >= 90) printf ("Your score of %d is a A\n", score) ; else if (score >= 80 && score <90) printf ("Your score of %d is a B\n", score) ; else if (score >= 70) printf ("Your score of %d is a C\n", score) ;

11 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 11Winter Quarter Do-While Loops else if (score >= 60) printf ("Your score of %d is a D\n", score) ; else if (score != 0) printf ("Your score of %d is an E\n", score) ; else printf ("Terminating at your request.\n\n") ; } while (score != 0) ; }

12 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 12Winter Quarter For Loops (Syntax) for (expr1 ; expr2 ; expr3) { statement(s) ; } Expression1 sets initial conditions. It can be a single math expression or multiple math expressions separated by commas(,). It gets executed only once before the loop executes.

13 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 13Winter Quarter For Loops for ( expr1 ; expr2 ; expr3 ) { statement(s) ; } Expression2 is the logical expression to be evaluated as true or false. It does NOT need to contain any of the variables that are in expression1. It gets evaluated after expression1 and again after expression3.

14 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 14Winter Quarter For Loops for ( expr1 ; expr2 ; expr3 ) { statement(s) ; } Expression3 defines changes to conditions. It can be multiple expressions separated by commas. It gets executed after the statements in the loop are executed. The expressions do NOT have to be related to either those in expression1 or in expression2 (but they often are).

15 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 15Winter Quarter Expressions in For Loops Any (or all) of the expressions (i.e., expression1, expression2, or expression3) can be eliminated, but the semicolons MUST be used. Eliminating expression1 and expression3 will cause the for loop to behave like a while loop since only expression2 (the logical one) will be left. Eliminating expression2 will create a loop that will never terminate unless there is a break or exit statement inside the loop (or someone pulls the plug.)

16 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 16Winter Quarter Simple For Loop Example int counter; for (counter = 1; counter <= 10; counter++) { printf ("The counter value is: %2d\n", counter); }

17 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 17Winter Quarter Equivalency of While Loop vs. For Loop The Loop: expression1 ; while ( expression2 ) { statement ; expression3 ; } Is Equivalent to: for ( expr1 ; expr2 ; expr3 ) statement ;

18 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 18Winter Quarter Equivalency of While Loop vs. For Loop The Loop: expression1 ; while ( expression2 ) expression3 ; Is Equivalent to: expression1 ; for ( ; expression2 ; ) expression3 ;

19 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 19Winter Quarter Simple While Loop Example int counter ; counter = 1 ; while ( counter <= 10 ) { printf ("The counter value is: %2d\n", counter) ; counter++ ; }

20 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 20Winter Quarter Example of For Loop #include int main ( ) { int k, n ; for (k = 1, n = 12 ; k 6 ; k++, n--) printf ("k=%d, n=%d\n", k, n ) ; }

21 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 21Winter Quarter Another Example of For Loop #include int main ( ) { int k = 1, n = 12 ; for ( ; k 6 ; ) printf ("k=%d, n=%d\n", k++, n-- ) ; }

22 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 22Winter Quarter A More Complex Example of For Loop #include int main ( ) { int rand_num, seed, k = 1 ; printf ("Enter a seed for the generator >") ; scanf ("%d", &seed) ; srand (seed) ; for ( ; ; ) { rand_num = * rand ( ) / RAND_MAX ; printf ("The number is %d and k = %d\n", rand_num, k) ; if (rand_num == 6 || k == 2000) break ; k++ ; }

23 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 23Winter Quarter Problem G09 Problem G09 combines the while loop, the for loop, and the switch/case structure plus other program elements that we have used previously. From the algorithm develop your own pseudo code for this program. Break the program into parts and then write, compile, and test the individual parts. Do NOT try to write the entire program without compiling Make variables mnemonic using three or more characters

24 Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 24Winter Quarter General Structure of Program #include int main( ) { –/*declare variables */ –/* start loop */ }


Download ppt "Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures."

Similar presentations


Ads by Google