DOC PREVIEW
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.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 119 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 119 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 119 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 119 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 119 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 119 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 119 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 119 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 119 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 119 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 119 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 119 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 119 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 119 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 119 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 119 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 119 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 119 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 119 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 119 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 119 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 119 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 119 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 119 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 119 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

The 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) ThenProgram 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:Equal to=Not equal to<>Greater than or equal to>=Less than or equal to<=Greater than>Less than<The 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.“D” = “D”“A” <> “a”“a” <> “c”1 < 21 <> 21 <= 22 >= 22 >= 11 = 1Here are some expressions that evaluate to True:Here are some expressions that evaluate to False:“D” <> “D”“A” = “a”1 >= 21 <> 13 > 42 < 22 <= 11 = 2The 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 txtInputtext box and a lblOutput label have been created to hold the input and output.Private Sub btnIf_Click(...If (txtInput.Text = “Yes”) ThenlblOutput.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”) ThenlblOutput.Text = “This will output, because the user entered Yes”End IflblOutput.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 IntegerintUserValue = Val(txtInput.Text)If (intUserValue > 2) ThenlblOutput.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 IntegerintUserValue = Val(txtInput.Text)If (intUserValue < 2) ThenlblOutput.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 output will be “ and the second statement prints”The Visual Basic .NET Coach11Chapter 4 – Decision MakingDrill 4.4Given the following code, what do you think would be contained in lblOutput?Private Sub btnIf_Click(...Dim intUserValue As IntegerintUserValue = Val(txtInput.Text)If (intUserValue >= 2) ThenlblOutput.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 “The first statement prints 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 Coach12Chapter 4 – Decision MakingDrill 4.5Given the following code, what do you think would be contained in lblOutput?Private Sub btnIf_Click(...Dim intUserValue As


View Full Document

UCR CS 5 - Decision Making

Download Decision Making
Our administrator received your request to download this document. We will send you the file to your email shortly.
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 2 2 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?