Slide 1 Chapter 4 The If…Then Statement Conditional control structure, also called a decision structure Executes a set of statements when a condition is true The condition is a Boolean expression The condition takes the form: If condition Then statements End If
Slide 2 If… Then Statement… For example, the statement: If intX = 5 Then intY = 20 End If assigns the value 20 to y only if x is equal to 5. Example 2: If intGuess = 7 Then Me.lblMessage.Text = “You guessed it!” End If
Slide 3 Chapter 4 Relational Operators – can be used to form Boolean expressions OperatorMeaning = equal to < less than <= less than or equal to > greater than >= greater than or equal to <> not equal to
Slide 4 Review: TestGrade – Part 1 of 5
Slide 5 Chapter 4 The If…Then…Else Statement Contains an Else clause that is executed when the If condition evaluates to false. Takes the form: If Condition Then Statements Else Statements End If
Slide 6 If…Then…ElseIf…Then…Else For example, the statement If intX = 5 Then intY = 20 Else intY = 10 End If assigns the value 20 to y if x is equal to 5 or the value 10 if x is not equal to 5. Example2 – If intGuess = int SECRECTNUMBER Then Me.lblMessage.Text = “You Guessed It!” Else Me.lblMessage.Text = “Try again” End If
Slide 7 Review: TestGrade – part 2 of 5 Review: Circle Area – part 2 of 2
Slide 8 Chapter 4 Nested If…Then…Else Statements Should be indented to make the logic clear. An If… Then.. Else statement can contain another If..Then… Else or If…Then statement Nested statement executed only when the branch it is in is executed. For example, the statement If intX = 5 Then intY = 20 Else If intX > 5 Then intY = 10 Else intY = 0 End If End If evaluates the nested If…Then…Else only when x is not equal to 5.
Slide 9 Nested… If…Then…Else Statements Example 2: If intGuess = intSECRETNUMBER Then Me.lblMessage.Text = “You guessed it!” Else If intGuess < intSECRETNUMBER Then Me.lblMessage.Text = “Too low” Else Me.lblMessage.Text = “Too hight.” End If * Nested statements should be indented…. As it is good programming style.
Slide 10 Chapter 4 The If…Then…ElseIf Statement Used to decide among three or more actions. Takes the form: If condition Then statements ElseIf condition Then statements Else statements End If There can be multiple ElseIf clauses and the last Else clause is optional.
Slide 11 Chapter 4 The If…Then…ElseIf Statement Conditions must be properly ordered for the statement to evaluate as expected. For example, the statement If intX < 5 Then intY = 20 ElseIf intX < 10 Then intY = 40 ElseIf intX < 15 Then intY = 80 End If would give very different results if the conditions were ordered differently.
Slide 12 The If…Then…ElseIf Statement Example 2: If intGuess = intSECRETNUMBER Then Me.lblMessage.Text = “You guessed it!” ElseIf intGuess < intSECRETNUMBER Then Me.lblMessage.Text = “Too low” Else Me.lblMessage.Text = “Too high.” End If
Slide 13 Review: TestGrade part 3 of 5
Slide 14 Chapter 4 The Select…Case Statement The result of an expression determines which statements to execute. The Case Else code is optional and is executed when none of the previous cases are met: Select…Case takes the form: Select expression Case value statements Case Else statements End Select
Slide 15 Chapter 4 The Select…Case Statement Example 1: Select Case intScore Case 0, 10 ‘score is 0 or 10 Me.lblMessage.Text = “Nice Try" Case 20 To 23 ‘score is 20, 21, 22, or 23 Me.lblMessage.Text = “Great!" Case Else Me.lblMessage.Text = “Invalid Score" End Select
Slide 16 Review: Hurricane pg 97
Slide 17 Chapter 4 The Select…Case Is Statement Compares the result of an expression to a range of values when a relational operator is part of the value. For example: Select Case score Case Is = 25 Me.lblMessage.Text = "Great!" End Select
Slide 18 Review: Test Grade part 4 of 5 Pg 98
Slide 19 Chapter 4 The Rnd() Function Uses a formula to generate a sequence of numbers that are each greater than 0 and less than 1 and then returns one number from the sequence. A random integer in a range is generated by using the formula: (highNum – lowNum + 1) * Rnd() + lowNum Random integers are produced by using the Int() function along with the Rnd() function: Int(21 * Rnd() + 10)'10 to 30 The Randomize() statement initializes the random number generator.
Slide 20 Review: Random Numbers Pg 99
Slide 21 Chapter 4 Algorithms & Pseudocode Algorithm -A set of steps that outline how to solve a problem. Algorithm can be implemented in plain English or in a mix of English and program code called pseudocode. Algorithms allow a programmer to think through a program before actually typing code, which may reduce errors in logic.
Slide 22 Algorithm Example (do not have to write) Algorithm for Number Guess Application: 1. Determine a secret number. 2. Get a number from the player. 3. Compare the player’s number with the secret number. 4. if the player’s number is the same as the secret number go to step 5, otherwise tell the player if the number entered was too low or too high and then go back to step Display a message telling the player the secret number was guessed.
Slide 23 Pseudocode for Number Guess Application: Sub btnCheckGuess_Click() intSecretNumber = 7 Get guess from text box If intGuess = intSecretNumber Then Display “You guessed it” ElseIf intGuess < intSecretNumber Then Display “Too low” Else Display “Too High” End If End Sub Pseudocode Example (do not have to write)
Slide 24 Chapter 4 Static Variables Variables have a lifetime in which they exist in memory. Local variables: duration of the procedure in which it was declared. Global: is the duration of the program. Static Variables: Declared with the keyword Static. Have a lifetime the duration of the program's running time. Used to extend the lifetime of local variables in a procedure. Should be explicitly initialized when declared (have a value). A better choice than a global variable because the scope of the variable should be as narrow as possible.
Slide 25 Complete Review: Guessing Game Part 1 of 4
Slide 26 Chapter 4 Compound Boolean Expressions CBE – Use more than one Boolean expression in a single condition to determine if the condition is true or false. Formed using logical operators: And, Or, or Not.
Slide 27 Chapter 4 And Truth Table And Exp1Exp2Result True False TrueFalse
Slide 28 Chapter 4 Or Truth Table Or Exp1Exp2Result True FalseTrue FalseTrue False
Slide 29 Chapter 4 Not Truth Table Not ExpResult TrueFalse True
Slide 30 Complete Review: Rock Paper Scissors Part 1 of 4 pg 105
Slide 31 Chapter 4 The MessageBox Class A predefined dialog box that displays a message to the user. Ex: Alert user of invalid data entered or a reminder of options required for an app to continue Includes the Show() method for displaying the dialog box. For example: MessageBox.Show(message)
Slide 32 Complete Review: Guessing Game – Part 2 of 4 pg108 Complete Review: Test Grade – Part 5 of 5 pg 108
Slide 33 Chapter 4 Counter Variables A variable that is incremented by a constant value. Used for counting guesses, the numbers of values entered, the number of times a button was clicked, and so on. The value of a counter is updated in a statement similar to: counter = counter + 1 Should be initialized when declared and updated by an unchanging amount.
Slide 34 Chapter 4 Assignment Operators OperatorOperation += addition and then assignment -= subtraction and then assignment Counters should be initialized when declared and declared as a Static variable so that it is initialized only once
Slide 35 Complete Review: Rock, Paper, Scissors Part 4 of 4 pg 109
Slide 36 Chapter 4 The CheckBox Control (Name) should begin with chk. Text is the text displayed next to the box. Checked is set to True if the box should be displayed as checked. An If…Then statement is often used to determine if a check box is checked or cleared.
Slide 37 Chapter 4 Line-Continuation Character The underscore character _ is the line-continuation character. There must be a space before and nothing after and cannot be within quotation marks. Used for dividing code to make it more readable.
Slide 38 Complete Review: Morning To Do Pg 110