Chapter3 - 1 Chapter 3 Decision Making 2 Overview Decision...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 1 Chapter 3 Decision Making 2 Overview Decision Making If statement If If-Else If-ElseIf-Else Sample Programs BallDrop DivideFunction AreaVolume 3 Introduction In chapter 2 we have learned the following: Basic input/output operations Arithmetic operations Sequential programming Ask for input Receive input (Input Box) Process input (Arithmetic operations) Display output (Message Box) Read in sides side1 = InputBox() side2 = InputBox() Calculate hypotenuse hypot = Math.Sqrt(...) Display hypotenuse MsgBox(hypot, ...) 4 Introduction In this chapter, we will deal with more sophisticated problems that require a certain amount of decision making on the part of the program. We will use If statements to incorporate decision making in our program. 5 BallDrop.vb Objective Write a program that computes the height of a ball in free fall. The inputs are: height of the ball before it was dropped, time at which the height from the ground is computed. y he igh t 2 ) ( 2 1 time G height y- = 6 BallDrop.vb The final height would be negative, because the equation does not account for the fact that the ball has hit the floor and stopped! 2 ) ( 2 1 time G height y- = What would happen if the initial height ( height) is small and time is large? time (sec) y (meters) 100.00 2 80.38 4 21.52 4.5152 0.00 6-76.58 time (sec) y (meters) 100.00 2 80.38 4 21.52 4.5152 0.00 6-76.58 7 Code Details 8 If Statement When the program encounters an If statement, it evaluates the condition. If the condition is true, it moves on to the next instruction. If the condition is not true, the program skips one or more instructions and jumps to another instruction further down the list. expression condition statement FALSE TRUE 9 y = 10 y < 0 Set y = 0 FALSE TRUE If Statement y = -10 y < 0 Set y = 0 FALSE TRUE y = -10 y < 0 Set y = 0 FALSE TRUE y = -10 y < 0 Set y = 0 FALSE TRUE Example : If (y < 0) Then y = 0 End If-10 10 Assume, y = y = 10 y < 0 Set y = 0 FALSE TRUE y = 10 y < 0 Set y = 0 FALSE TRUE y = 10 y < 0 Set y = 0 FALSE TRUE 10 Comparison Operators As the name implies, comparison operators are used to make comparisons between expressions. Examples: Assume, a = 5, b = 30, c = D If (a < 0) Then returns false If (b > 0) Then returns true If (b <= 30) Then returns true If (c = F) Then returns false If (c <> F) Then returns true 11 Exercise: BallDropWithError.vb Objective Revise BallDrop.vb to include error checks for input values. The program will display an error message if either the initial height or the time is negative. 12 DividedFunction1.vb Objective Write a program that computes the value of f(x), which is represented by different functions for different ranges of x....
View Full Document

Page1 / 36

Chapter3 - 1 Chapter 3 Decision Making 2 Overview Decision...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online