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 LoopEngineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 10 P. 1Winter QuarterRepetition StructuresLecture 10Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 10 P. 2Winter QuarterRepetition 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).Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 10 P. 3Winter QuarterRepetition 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.Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 10 P. 4Winter QuarterWhile Loops (Syntax)The syntax of a while loop is:while ( logical expression is true ) statement ;orwhile ( logical expression is true ){ statement ; statement ;}Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 10 P. 5Winter QuarterWhile 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 ;Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 10 P. 6Winter QuarterWhile 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) ;Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 10 P. 7Winter QuarterWhile 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 */ return 0;} /* End of "main" function */Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 10 P. 8Winter QuarterDo-While Loops (Syntax)do statement ; while ( logical expr is true ) ;or, more commonly do { statement ; statement ; } while ( logical expression is true ) ;Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 10 P. 9Winter QuarterDo-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;Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 10 P. 10Winter QuarterDo-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) ;Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 10 P. 11Winter QuarterDo-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) ; return 0;}Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 10 P. 12Winter QuarterFor 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.Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 10 P. 13Winter QuarterFor 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.Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 10 P. 14Winter QuarterFor 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).Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering Education CoalitionLect 10 P. 15Winter QuarterExpressions 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.)Engineering H192 - Computer ProgrammingThe Ohio State UniversityGateway Engineering


View Full Document

OSU ENGR H192 - Repetition Structures

Documents in this Course
Strings

Strings

22 pages

Load more
Download 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 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 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?