Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 2 I Am Not a Control Freak!

17
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 2 I Am Not a Control Freak!

Transcript of Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 2 I Am Not a Control Freak!

Page 1: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 2 I Am Not a Control Freak!

Clearly Visual Basic: Programming with Visual Basic 2008

Chapter 2 I Am Not a Control Freak!

Page 2: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 2 I Am Not a Control Freak!

Clearly Visual Basic: Programming with Visual Basic 2008

Objectives

• Describe the three control structures

• Write simple algorithms using the sequence, selection, and repetition structures

2

Page 3: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 2 I Am Not a Control Freak!

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

3

Page 4: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 2 I Am Not a Control Freak!

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

4

Page 5: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 2 I Am Not a Control Freak!

Clearly Visual Basic: Programming with Visual Basic 2008 5

Page 6: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 2 I Am Not a Control Freak!

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

6

Page 7: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 2 I Am Not a Control Freak!

Clearly Visual Basic: Programming with Visual Basic 2008 7

Page 8: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 2 I Am Not a Control Freak!

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

8

Page 9: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 2 I Am Not a Control Freak!

Clearly Visual Basic: Programming with Visual Basic 2008 9

Page 10: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 2 I Am Not a Control Freak!

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

10

Page 11: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 2 I Am Not a Control Freak!

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

11

Page 12: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 2 I Am Not a Control Freak!

Review Questions – Page 131. The set of instructions for adding together 2 numbers:

control, repetition, selection, or sequence

2. The recipe instruction “beat until smooth: control, repetition, selection, or sequence

3. The instruction “If it rains outside, then take an umbrella to work: control, repetition, selection, or sequence

4. Which control is used to determine if a credit card holder is over his credit limit:repetition, selection, both repetition and sequence

5. 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

Page 13: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 2 I Am Not a Control Freak!

Answers to Page 13

• Sequence

• Repetition

• Selection

• Selection

• Both repetition and selection

Clearly Visual Basic: Programming with Visual Basic 2008 13

Page 14: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 2 I Am Not a Control Freak!

Exercise – Page 14 & 15Do first two together- divide other problems between groups of students

Group 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

Page 15: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 2 I Am Not a Control Freak!

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

Page 16: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 2 I Am Not a Control Freak!

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

Page 17: Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 2 I Am Not a Control Freak!

1. Walk into the maze (this is #11: debug the maze program)

2. Turn left 90 degrees

3. Repeat until you are directly in front of a wall: walk forward

4. Turn right 90 degrees

5. Repeat until you are directly in front of a wall: walk forward

6. Turn right 90 degrees

7. Repeat until you are directly in front of a wall: walk forward

8. Turn right 90 degrees

9. Repeat until you are directly in front of a wall: walk forward

10. Turn right 90 degrees

11. Repeat until you are directly in front of a wall: walk forward

12. Turn left 90 degrees

13. Repeat until you are directly in front of a wall: turn right 90 degrees

14. Repeat until you are out of the maze: walk forward