New version page

UCR CS 5 - Decision Making

This preview shows page 1-2-3-4-5-6-7-8-55-56-57-58-59-60-61-62-112-113-114-115-116-117-118-119 out of 119 pages.

View Full Document
View Full Document

End of preview. Want to read all 119 pages?

Upload your study docs or become a GradeBuddy member to access this document.

View Full Document
Unformatted text preview:

Chapter 4 – Decision MakingSlide 2Slide 3Slide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Slide 36Slide 37Slide 38Slide 39Slide 40Slide 41Slide 42Slide 43Slide 44Slide 45Slide 46Slide 47Slide 48Slide 49Slide 50Slide 51Slide 52Slide 53Slide 54Slide 55Slide 56Slide 57Slide 58Slide 59Slide 60Slide 61Slide 62Slide 63Slide 64Slide 65Slide 66Slide 67Slide 68Slide 69Slide 70Slide 71Slide 72Slide 73Slide 74Slide 75Slide 76Slide 77Slide 78Slide 79Slide 80Slide 81Slide 82Slide 83Slide 84Slide 85Slide 86Slide 87Slide 88Slide 89Slide 90Slide 91Slide 92Slide 93Slide 94Slide 95Slide 96Slide 97Slide 98Slide 99Slide 100Slide 101Slide 102Slide 103Slide 104Slide 105Slide 106Slide 107Slide 108Slide 109Slide 110Slide 111Slide 112Slide 113Slide 114Slide 115Slide 116Slide 117Slide 118Slide 119The Visual Basic .NET Coach1Chapter 4 – Decision MakingA useful computer requires the ability to make decisions. The key to programming the computer to make correct decisions is making sure you understand how to represent and evaluate the expression representing the decision properly.The Visual Basic .NET Coach2Chapter 4 – Decision Making4.1 If StatementsVisual Basic .NET offers more than one way for decisions to be made.The If statement matches the idea of a single decision to a single result.You can program an If statement by using the following code illustrating its syntax:If (Expression) Then Program statements to execute if expression evaluates to TrueEnd IfAn If statement consists of an expression that determines whether a program statement or statements execute. An expression can be the comparison of two values. To compare values you may use any of the following operators:<Less than>Greater than<=Less than or equal to>=Greater than or equal to=Equal to<>Not equal toThe Visual Basic .NET Coach3Chapter 4 – Decision MakingWith either a variable or a constant value placed on both sides of the operator, an expression can be evaluated to either True or False. When the condition in an If statement evaluates to True, the statements immediately following it are executed until an End If statement is reached. If the If statement evaluates to False, the statements immediately after it, until the End If, are not executed.1 = 12 >= 12 >= 21 <= 21 < 21 <> 2“a” <> “c”“A” <> “a”“D” = “D”Here are some expressions that evaluate to True:Here are some expressions that evaluate to False:1 = 22 <= 12 < 23 > 41 >= 21 <> 1“A” = “a”“D” <> “D”The Visual Basic .NET Coach4Chapter 4 – Decision MakingDrill 4.1Indicate whether each expression evaluates to True or False1 (5 >= 4) Answer: True2 (-3 < -4) Answer: False3 (5 = 4) Answer: False4 (5 <> 4) Answer: True5 (4 >= 4) Answer: True6 (4 <= 4) Answer: TrueThe Visual Basic .NET Coach5Chapter 4 – Decision MakingSimple If StatementWrite a program that will output a message if the user enters the word “Yes” in a text box. By using an If statement, you can determine if the value that is entered in a text box is equal to the value you desire.The following code is an example that compares the text box contents to the String “Yes”. The code assumes that a btnIf button has been created to place the code and a txtInput text box and a lblOutput label have been created to hold the input and output.Private Sub btnIf_Click(... If (txtInput.Text = “Yes”) Then lblOutput.Text = “This will output, because the user entered Yes” End IfEnd SubThe Visual Basic .NET Coach6Chapter 4 – Decision MakingSimple If Statement ContinuedWhat do you think would be contained in lblOutput:1 If the user enters “Yes” in the txtInput text box?Answer: The condition will evaluate to True and the text “This out output, because the user entered Yes” is placed in the lblOutput label.2 If the user enters “No” in the txtInput text box?Answer: The condition will evaluate to False and the txtOutput text box remain empty.The Visual Basic .NET Coach7Chapter 4 – Decision MakingSimple If Statement with Code Following ItSome statements execute based on a decision and some regardless of the evaluation of the condition.The following program is modified from the previous one.Private Sub btnIf_Click(... If (txtInput.Text = “Yes”) Then lblOutput.Text = “This will output, because the user entered Yes” End If lblOutput.Text &= “ and this is here as well”End SubThe Visual Basic .NET Coach8Chapter 4 – Decision MakingSimple If Statement with Code Following It ContinuedWhat do you think would be contained in lblOutput:1 If the user enters “Yes” in the txtInput text box?Answer: The output will be “This will output, because the user entered Yes and this is here as well”2 If the user enters “No” in the txtInput text box?Answer: The output will be “ and this is here as well”The Visual Basic .NET Coach9Chapter 4 – Decision MakingDrill 4.2Given the following code, what do you think would be contained in lblOutput?Private Sub btnIf_Click(... Dim intUserValue As Integer intUserValue = Val(txtInput.Text) If (intUserValue > 2) Then lblOutput.Text = “The first statement prints” End If lblOutput.Text = lblOutput.Text & “ and the second statement prints”End Sub1 If the user enters 1 in the txtInput text box?Answer: The output will be “ and the second statement prints”2 If the user enters 2 in the txtInput text box?Answer: The output will be “ and the second statement prints”3 If the user enters 3 in the txtInput text box?Answer: The output will be “The first statement prints and the second statement prints”The Visual Basic .NET Coach10Chapter 4 – Decision MakingDrill 4.3Given the following code, what do you think would be contained in lblOutput?Private Sub btnIf_Click(... Dim intUserValue As Integer intUserValue = Val(txtInput.Text) If (intUserValue < 2) Then lblOutput.Text = “The first statement prints” End If lblOutput.Text = lblOutput.Text & “ and the second statement prints”End Sub1 If the user enters 1 in the txtInput text box?Answer: The output will be “The first statement prints and the second statement prints”2 If the user enters 2 in the txtInput text box?Answer: The output will be “ and the second statement prints”3 If the user enters 3 in the txtInput text box?Answer: The


View Full Document
Loading Unlocking...
Login

Join to view Decision Making and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Decision Making and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?