Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 1023 Intro to Engineering Computing 3: Computer Programming Looping Statements Mark Kerstetter Department of Computer Science Western Michigan University.

Similar presentations


Presentation on theme: "CS 1023 Intro to Engineering Computing 3: Computer Programming Looping Statements Mark Kerstetter Department of Computer Science Western Michigan University."— Presentation transcript:

1 CS 1023 Intro to Engineering Computing 3: Computer Programming Looping Statements Mark Kerstetter Department of Computer Science Western Michigan University © Spring 2012

2 Conditions n n Generally used to control what to do next? n n Expressions that evaluate to True or False n n In C Programs conditions … o Follow keyword if o Follow keyword while o Contained within ( and ) n n Relational Operations: >=, >, = =, !=, <, <= o Compare numeric expressions o Compare characters and character strings n n Boolean (Logical) Operations: ! (not), && (and), | | (or) o Only used with logical expressions, i.e., those whose values are True or False o Never used with numeric or character expressions Important! = does not compare = = compares => and =< and =! are not legal

3 Looping Statements n Flow Control Statements – Repeating Instructions (Loops) Three kinds of loops o while-loop-statement – pre-test loop o do-while-loop-statement – post-test loop o for-loop-statements – counting loop n When to use loops? o Write pseudo code simply. Note repeated patterns. Repeats indicate loop. F E.g. Read 1 st x & y coordinates, Write coordinates, Read 2 nd x & y coordinates, Write coordinates, Read 3 rd x & y coordinates, Write coordinates, etc. F E.g., Calculate first tax return, calculate the next tax return, calculate the next tax return, calculate the next tax return, etc. n Loops vs. Selections o Do NOT use a loop to make an either-or decision. Use an if or if-else. o Use loops to repeat statements.

4 while-loop Statement n Flow Control Statements – Repeating Instructions (Loops) o while-loop statement – pre-test loop F Always checks at the beginning of another potential loop F Enters the “body of the loop” (repeating statements) another time when the condition is true. F while (condition) { statement(s) to be repeated another time ; } 1.{ and } needed to surround multiple statements that need to be repeated. 2.Value altering the condition must change within the body. ; while-statement ends here! Caution! A common mistake when writing while-loops or for-loops is to place a semicolon immediately at the end of the first line of the loop statement thus creating an empty loop. ? Body of Loop F T

5 Examples of while-loop Statement while (forestedAcres < desiredLimit) { /* Calculate new growth using formula */ /* Compute forestedAcres from new growth and established growth, i.e. previous forestedAcres */ } Timber Regrowth Simulation while (1==1) { printf(“This is the song that never ends,\n”); printf(“It just goes on and on my friend\n“); printf(“Some people started sing it, not knowing what it was,\n”); printf(“And they’ll continue singing it forever just because -\n”); } Song that Never Ends! What controls # of repeats? Variables in condition that control while-loop. 1. forestedAcres 2. desiredLimit The body of the loop must contain statements that cause one or more of these variables to change or the loop may never end! Infinite Loop!

6 do-while-loop Statement n Flow Control Statements – Repeating Instructions (Loops) o do-while-loop statement – post-test loop F Always executes the body of the loop at least once F Checks the condition at the bottom of the loop and repeats the “body of the loop” when the condition is true F do { statement(s) to be repeated another time ; } while (condition) ; 1.{ and } needed to surround multiple statements that need to be repeated. 2.Value altering the condition must change within the body. do-while-statement ends here! Statements end with a semicolon (;). F ? Body of Loop T

7 Examples of do-while-loop Statement do { /* Play Next Game */ printf(“Want to play again? “); } while (getchar() == ‘Y’); Computer Games do { /* Enter Next Transaction */ printf(“Want another transaction? “); answer = getchar(); } while (answer == ‘Y’ || answer == ‘y’); ATM Transactions do { /* Play Next Game */ printf(“Want to play again? “); answer = getchar(); } while (answer == ‘Y’ || answer == ‘y’); Better Alternative Code Declare answer to contain character data type. char answer; ? Body of Loop T F

8 n Flow Control Statements – Repeating Instructions (Loops) o for-loop-statements – counting loop F Used to repeat some instructions a specified number of time repeat count known before starting the loop F Can count up or count down… in single unit steps, e.g., +1 or -1 In multiple unit steps, e.g., in 2’s, 3’s, -5’s etc. F Acts like a while-loop since it tests before attempting the body F for ( starting value; stopping condition; how value changes ) { statement(s) to be repeated ; } for-loop Statement 1.{ and } needed to surround multiple statements that need to be repeated. 2.Control variable keeping track of the count can be used inside the body, but should not be changed inside the body. ; for-statement ends here! Caution! A common mistake when writing while-loops or for-loops is to place a semicolon immediately at the end of the first line of the loop statement thus creating an empty loop. ? Body of Loop F T loop control variable initialized, tested, and updated

9 Examples of for-loop Statement for (year=1; year<=20; ++year) { /* Compute the timber growth for*/ /* the next year and display it */ } Timber Growth Table for (k=1; k<=50; ++k) { /* Process Next Item in List */ } Process a List of Items for (beer=99; beer>=3; --beer) { printf(“%2d bottles of beer on the wall,\n“, beer); printf(“%2d bottles of beer!\n”, beer); printf(“Take one down, and pass it around.\n”) printf(“%2d bottles of beer on the wall.\n\n”, beer – 1); } 99 Bottles of Beer! Another Example Forest Regrowth Make a table of forest regrowth for 20 years using the annual regrowth formula. ? Body of Loop F T

10 Examples of for-loop Statement for (row=1; row<=50; ++row) { for (column=1; column<=25; ++column) { /* Display Next Table Entry */ printf(“%8.2f ”, table[row,column]); } printf(“\n”); } Display Tables degToRad = 2*3.142/360; for (degrees=0; degrees<=360; degrees = degrees + 15) { printf(“%3d\t%10.4f\n”, degrees, sin(degrees*degToRad)); } Step Sizes Other than +1 or -1 Nested Loops A nested loop is one loop inside of another loop. Here we use the first loop to establish a table’s row index and the inner loop to get the element column index.. This example of an array element will be explained in a later class.


Download ppt "CS 1023 Intro to Engineering Computing 3: Computer Programming Looping Statements Mark Kerstetter Department of Computer Science Western Michigan University."

Similar presentations


Ads by Google