Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 The If…Then Statement

Similar presentations


Presentation on theme: "Chapter 4 The If…Then Statement"— Presentation transcript:

1 Chapter 4 The If…Then Statement
5/19/ :42 PM 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 For example, the statement If x = 5 Then y = 20 End If assigns the value 20 to y only if x is equal to 5. Refer to page 93 in the text. © 2010 Lawrenceville Press

2 Chapter 4 Relational Operators
5/19/ :42 PM Chapter 4 Relational Operators Operator Meaning = equal to < less than <= less than or equal to > greater than >= greater than or equal to <> not equal to Refer to page 93 in the text. Relational operators are used to form Boolean expressions. Boolean expressions evaluate to True or False. Note that the equal sign is used as both an assignment operator and a relational operator. © 2010 Lawrenceville Press

3 Chapter 4 The If…Then…Else Statement
5/19/ :42 PM Chapter 4 The If…Then…Else Statement Contains an Else clause that is executed when the If condition evaluates to false. For example, the statement If x = 5 Then y = 20 Else y = 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. Refer to pages 94 and 95 in the text. © 2010 Lawrenceville Press

4 Chapter 4 Nested If…Then…Else Statements
5/19/ :42 PM Chapter 4 Nested If…Then…Else Statements Should be indented to make the logic clear. Nested statement executed only when the branch it is in is executed. For example, the statement If x = 5 Then y = 20 Else If x > 5 Then y = 10 Else y = 0 End If End If evaluates the nested If…Then…Else only when x is not equal to 5. Refer to page 95 in the text. © 2010 Lawrenceville Press

5 Chapter 4 The If…Then…ElseIf Statement
5/19/ :42 PM Chapter 4 The If…Then…ElseIf Statement Used to decide among three or more actions. Conditions must be properly ordered for the statement to evaluate as expected. For example, the statement If x < 5 Then y = 20 ElseIf x < 10 Then y = 40 ElseIf x < 15 Then y = 80 End If would give very different results if the conditions were ordered differently. Refer to page 96 in the text. © 2010 Lawrenceville Press

6 Chapter 4 The Select…Case Statement
5/19/ :42 PM 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 numLegs Case 2 Me.lblMessage.Text = "human" Case 4 Me.lblMessage.Text = "beast" Case 8 Me.lblMessage.Text = "insect" Case Else Me.lblMessage.Text = "???" End Select Refer to pages 96 and 97 in the text. © 2010 Lawrenceville Press

7 Chapter 4 The Select…Case Is Statement
5/19/ :42 PM Chapter 4 The Select…Case Is Statement Compares the result of an expression to a range of values to determine which statements to execute. For example: Select Case score Case Is < 10 Me.lblMessage.Text = "Nice try." Case Is < Me.lblMessage.Text = "Good." Case Is >= Me.lblMessage.Text = "Great!" End Select Refer to page 97 in the text. © 2010 Lawrenceville Press

8 Chapter 4 The Rnd() Function
5/19/ :42 PM 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. Refer to pages 98 and 99 in the text. © 2010 Lawrenceville Press

9 A set of steps that outline how to solve a problem.
5/19/ :42 PM Chapter 4 Algorithms A set of steps that outline how to solve a problem. 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. Refer to page 101 in the text. © 2010 Lawrenceville Press

10 Chapter 4 Static Variables
5/19/ :42 PM Chapter 4 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. A better choice than a global variable because the scope of the variable can be limited. Refer to page 102 in the text. Static variables are necessary in event procedures with variables that should be retained in memory throughout program execution. © 2010 Lawrenceville Press

11 Chapter 4 Compound Boolean Expressions
5/19/ :42 PM Chapter 4 Compound Boolean Expressions More than one Boolean expression in a single condition. Formed using the And, Or, or Not operators. Refer to pages 104 and 105 in the text. Compound Boolean expressions use more than one Boolean expression to determine if a condition is true or false. © 2010 Lawrenceville Press

12 Chapter 4 And Truth Table
5/19/ :42 PM Chapter 4 And Truth Table And Exp1 Exp2 Result True False Refer to page 104 in the text. © 2010 Lawrenceville Press

13 Or Exp1 Exp2 Result True False Chapter 4 Or Truth Table
5/19/ :42 PM Chapter 4 Or Truth Table Or Exp1 Exp2 Result True False Refer to page 104 in the text. © 2010 Lawrenceville Press

14 Chapter 4 Not Truth Table
5/19/ :42 PM Chapter 4 Not Truth Table Not Exp Result True False Refer to page 104 in the text. © 2010 Lawrenceville Press

15 Chapter 4 The MessageBox Class
5/19/ :42 PM Chapter 4 The MessageBox Class A predefined dialog box that displays a message to the user. Includes the Show() method for displaying the dialog box. For example: MessageBox.Show(message) Refer to page 107 in the text. © 2010 Lawrenceville Press

16 Chapter 4 Counter Variables
5/19/ :42 PM 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. Refer to page 108 in the text. Many algorithms involve counting. Applications written for algorithms that involve counting use a counter variable for storing a number that is incremented by a constant value. © 2010 Lawrenceville Press

17 Chapter 4 Assignment Operators
5/19/ :42 PM Chapter 4 Assignment Operators Operator Operation += addition and then assignment -= subtraction and then assignment Refer to page 108 in the text. © 2010 Lawrenceville Press

18 Chapter 4 The CheckBox Control
5/19/ :42 PM 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. Refer to page 109 in the text. A Click event is sometimes coded for a check box. This procedure executes when a check box is clicked and usually includes code to determine the state of the check box and then performs actions depending on whether the check box was selected or cleared. © 2010 Lawrenceville Press

19 Chapter 4 Line-Continuation Character
5/19/ :42 PM 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. Refer to page 110 in the text. © 2010 Lawrenceville Press


Download ppt "Chapter 4 The If…Then Statement"

Similar presentations


Ads by Google