170 likes | 183 Views
Clearly Visual Basic: Programming with Visual Basic 2008. Chapter 2 I Am Not a Control Freak!. Objectives. Describe the three control structures Write simple algorithms using the sequence, selection, and repetition structures. Control Structures. Control structures
E N D
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 2 I Am Not a Control Freak!
Objectives • Describe the three control structures • Write simple algorithms using the sequence, selection, and repetition structures Clearly Visual Basic: Programming with Visual Basic 2008
Control Structures • Control structures • Sequence, selection, and repetition • Control the flow of a program’s logic • Most programs • Will use both selection and repetition structures Clearly Visual Basic: Programming with Visual Basic 2008
The Sequence Structure • Directs computer to process program instructions in a particular order • Example: Rob, the mechanical man • Has a limited instruction set • Algorithm • Set of step-by-step instructions that accomplish a task Clearly Visual Basic: Programming with Visual Basic 2008
The Selection Structure • Also called the decision structure • Makes a decision and then takes appropriate action based on that decision • Used every time you drive your car and approach an intersection Clearly Visual Basic: Programming with Visual Basic 2008
The Repetition Structure • Directs computer to repeat one or more instructions until some condition is met • Also referred to as a loop or as iteration Clearly Visual Basic: Programming with Visual Basic 2008
Summary • Algorithm • Set of step-by-step instructions that accomplish a task • Control structures • Sequence, selection, and repetition • Also called logic structures • Sequence structure • Directs computer to process program instructions one after another Clearly Visual Basic: Programming with Visual Basic 2008
Summary (continued) • Selection structure • Directs computer to make a decision and then selects appropriate action to take • Repetition structure • Directs computer to repeat one or more program instructions until some condition is met • Sequence structure • Used in all programs Clearly Visual Basic: Programming with Visual Basic 2008
Review Questions – Page 13 • The set of instructions for adding together 2 numbers:control, repetition, selection, or sequence • The recipe instruction “beat until smooth: control, repetition, selection, or sequence • The instruction “If it rains outside, then take an umbrella to work: control, repetition, selection, or sequence • Which control is used to determine if a credit card holder is over his credit limit:repetition, selection, both repetition and sequence • A company pays 3% annual bonus to employees who have been with the company more than 5 years; others get 1% bonus. Which is used to calculate every bonus? repetition, selection, both repetition and sequence
Answers to Page 13 • Sequence • Repetition • Selection • Selection • Both repetition and selection Clearly Visual Basic: Programming with Visual Basic 2008
Exercise – Page 14 & 15Do first two together- divide other problems between groups of studentsGroup 1 take prob. 3, Group 2 take prob. 4, Group 3 take prob. 5, Group 4 take prob. 6 Walk Forward, sit down, stand up, pick the flower with your right hand, pick the flower with your left hand, drop the toy in the toy chest, turn right 90 degrees, jump over the box, throw the box out of the way, if the box is red, do this: otherwise do this: repeat x times, repeat until you are directly in front of the chair, repeat until you are directly in front of the toy chest
Continue- Exercises – Page 14-17 #7: A store gives a 10% discount to customers who are at least 65 years old. Create an algorithm: Assign 10% as the discount rate Assign 0% as the discount rate Calculate the amount due by subtracting discount rate from the number 1, and then multiplying the result times the item price If the customer is greater than or equal to 65, do this: If the customer is less than 65, do this: Otherwise do this: Print the amount due Read the customer’s age and item price
Continue- Exercises – Page 14-17 #9: Calculates and prints gross pay for 5 employees. If an employee works more than 40 hours, he gets time and a half. Create an algorithm: _________?__________ read the employees name, hours work & pay rate _________?__________ Calculate gross pay = hours worked * 40 otherwise, do this: calculate regular pay= pay rate * 40 calculate overtime hours = hours worked – 40 calculate overtime pay= ____________________ calculate gross pay = ______________________ Print the employee’s name and gross pay
Walk into the maze (this is #11: debug the maze program) • Turn left 90 degrees • Repeat until you are directly in front of a wall: walk forward • Turn right 90 degrees • Repeat until you are directly in front of a wall: walk forward • Turn right 90 degrees • Repeat until you are directly in front of a wall: walk forward • Turn right 90 degrees • Repeat until you are directly in front of a wall: walk forward • Turn right 90 degrees • Repeat until you are directly in front of a wall: walk forward • Turn left 90 degrees • Repeat until you are directly in front of a wall: turn right 90 degrees • Repeat until you are out of the maze: walk forward