OSU ENGR H192 - Lecture 10 - Repetition Structures

Unformatted text preview:

Repetition StructuresSlide 2Slide 3While Loops (Syntax)While LoopsSlide 6Slide 7Do-While Loops (Syntax)Do-While LoopsSlide 10Slide 11For Loops (Syntax)For LoopsSlide 14Expressions in For LoopsSimple For Loop ExampleEquivalency of While Loop vs. For LoopSlide 18Simple While Loop ExampleExample of For LoopAnother Example of For LoopA More Complex Example of For LoopLect 10 P. 1 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionRepetition StructuresLecture 10Lect 10 P. 2 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionRepetition 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).Lect 10 P. 3 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionRepetition 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.Lect 10 P. 4 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionWhile Loops (Syntax)The syntax of a while loop is:while ( logical expression is true ) statement ;orwhile ( logical expression is true ){ statement ; statement ;}Lect 10 P. 5 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionWhile 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 <stdio.h>int main ( ){ int score ;Lect 10 P. 6 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionWhile 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) ;Lect 10 P. 7 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionWhile 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 */Lect 10 P. 8 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionDo-While Loops (Syntax)do statement ; while ( logical expr is true ) ;or, more commonly do { statement ; statement ; } while ( logical expression is true ) ;Lect 10 P. 9 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionDo-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 <stdio.h>int main ( ){ int score;Lect 10 P. 10 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionDo-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) ;Lect 10 P. 11 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionDo-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) ;}Lect 10 P. 12 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionFor 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.Lect 10 P. 13 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionFor Loopsfor ( 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.Lect 10 P. 14 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionFor Loopsfor ( 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).Lect 10 P. 15 Engineering H192 - Computer Programming Winter QuarterThe Ohio State UniversityGateway Engineering Education CoalitionExpressions 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.)Lect 10 P. 16 Engineering H192 - Computer Programming Winter


View Full Document

OSU ENGR H192 - Lecture 10 - Repetition Structures

Documents in this Course
Strings

Strings

22 pages

Load more
Download Lecture 10 - Repetition Structures
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 10 - Repetition Structures and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 10 - Repetition Structures 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?