This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: 1 Chapter 3 Decision Making 2 Overview Decision Making If statement If IfElse IfElseIfElse 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 676.58 time (sec) y (meters) 100.00 2 80.38 4 21.52 4.5152 0.00 676.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 &lt; 0 Set y = 0 FALSE TRUE If Statement y = 10 y &lt; 0 Set y = 0 FALSE TRUE y = 10 y &lt; 0 Set y = 0 FALSE TRUE y = 10 y &lt; 0 Set y = 0 FALSE TRUE Example : If (y &lt; 0) Then y = 0 End If10 10 Assume, y = y = 10 y &lt; 0 Set y = 0 FALSE TRUE y = 10 y &lt; 0 Set y = 0 FALSE TRUE y = 10 y &lt; 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 &lt; 0) Then returns false If (b &gt; 0) Then returns true If (b &lt;= 30) Then returns true If (c = F) Then returns false If (c &lt;&gt; 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
This note was uploaded on 10/25/2011 for the course COP 2271 taught by Professor Staff during the Fall '11 term at University of Florida.
 Fall '11
 Staff

Click to edit the document details