Week 4 Control Structures Repetition BJ Furman 18FEB2010 The Plan for Today Review control structures Sequence Selection Repetition Review relational operators pseudocode and flowcharting Repetition structures While Do While For Repetition structure example Learning Objectives Explain the three basic types of control structures Explain how the three kinds of repetition structures work Apply the concept of repetition control structures to a practical problem Control Structures Review All programs can be written in terms of three control structures like building blocks Sequence Built in to C Unless otherwise directed one statement after the next is executed Selection three types IF IF ELSE SWITCH Depending on a condition select between one statement or another If var1 is greater than 10 do this else do that Repetition three types WHILE DO WHILE FOR Depending on a condition execute one or more statements repeatedly Selection Structure Three kinds of selections structures IF also called single selection if condition is true Perform action if condition is false action is skipped IF ELSE also called double selection if condition is true Perform action else if condition is false Perform a different action SWITCH also called multiple selection Allows selection among many actions depending on the value of a variable or expression Repetition Structure t 0 Often need to repeat an action or calculation Ex Find and print the distance traveled by an object dropped from a height at t 0 sec each second over the next 10 seconds Repetition structures are often called loops t 10 A dynamics problem ME 101 F ma 1 2 d gt v0t d 0 2 mg Equation of motion 3 Types of Repetition Structures while Tests a condition at the beginning of the loop condition must first be true for the loop to run even once do while Tests a condition at the end of the loop loop will run at least once for Facilitates initializing and incrementing the variable that controls the loop Especially helpful for Looping for a known number of times Loops that count or that need to increment a variable while Loop Structure General form of a while loop while condition while condition not equal to zero statement1 execute this statement When condition becomes false looping stops and the next statement is executed Compound statement form while condition statement1 statement2 statement3 while Loop Pseudocode WHILE condition is TRUE repeat Statement1 Statement2 END WHILE while Loop Flowchart View Statement is executed while condition is true Note that the condition must first be true in order for the statement to be executed even once condition FALSE TRUE statement Program Development Define the problem Calculate and print the distance traveled each second by an object dropped from a height in a standard gravitational field beginning at t 0 sec over the next 10 seconds Simplify 1 2 d gt Assume v0 and d0 are both zero 2 Inputs Outputs time distance Solution Algorithm Pseudocode 1 Declare variables 2 3 d distance traveled t time Initialize variables While time is less than or equal to 10 while calculate d loop print time print distance increment time 4 End Flowchart Start Declare variables Initialize variables t 10 F T d 1 gt 2 print t d t t 1 End 2 Solution Code free fall d vs time c Practice Procedure Procedure Divide into groups of 4 Introduce yourselves Together all four in the group Define the problem Determine inputs and outputs Split into pairs sub groups One pair pseudocode the algorithm Other pair flowchart the algorithm Swap one from each pair and share pseudocode and flowchart Write code from flowchart or pseudocode Practice Problem Write a program that calculates the average exam score for a class Specifications Prompts user for the number of scores to average Prompts user for each score Echoes each score entered Calculates the average score Prints out the average score Practice Solution do while Loop Structure General form of a do while loop do statement1 execute this statement while condition while condition is TRUE When condition becomes false looping stops and the next statement after the while is executed Note statement1 will be executed at least once Remember to DO more than one statement in the loop enclose the statements in curly braces called a compound statement do while Loop Pseudocode REPEAT Statement1 Statement2 WHILE condition is TRUE do while Loop Flow Chart View statement is executed at least once statement condition FALSE TRUE for Loop Structure General form of a for loop for expression1 expression2 expression3 statement1 execute this statement expression1 initializes the variable controlling the loop i 0 expression2 is the condition for continuing the loop i 10 expression3 increments the control variable i same as i i 1 Note that there is NO semicolon after expression3 or after the closing parenthesis To execute more than one statement in the for loop enclose them in curly braces for Loop Pseudocode FOR n times REPEAT Statement1 Statement2 END FOR for Loop Flow Chart View expression1 Tests the loop control variable to see if it is time to quit looping ex i 10 expression2 F Initializes the loop control variable ex i 0 T statement expression3 Increments the loop control variable ex i for Loop Example double it c break and continue Statements break immediately exits from a loop Recall its use in the switch selection structure int x for x 1 x 10 x if x 5 break printf d x printf Broke out at x d n x 1 2 3 4 Broke out at x 5 continue skips remaining statements in the body of a repetition structure int x y for x 1 x 10 x if x 5 y x continue printf d x printf Skipped x d n y 1 2 3 4 6 7 8 9 10 Skipped x 5 Adapted from Deitel Deitel C How to Program 3rd ed p 119 Appendix Recap of Selection Structures Selection Structure Three kinds of selections structures if also called single selection if condition is true Perform action if condition is false action is skipped if else also called double selection if condition is true Perform action else if condition is false Perform a different action switch also called multiple selection Allows selection among many actions depending on the value of a variable or expression IF statement single selection Syntax if expression statement1 statement2 Notes if expression is TRUE not equal to zero then execute this statement otherwise execute this statement Indent statements Can have multiple statements Enclose a block of statements using curly braces if x 2 statement1 statement2 statement3 IF
View Full Document