Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2006 Lawrenceville Press Slide 1 Chapter 5 The If…Then Statement (One-Way)  Conditional control structure, also called a decision structure  Executes.

Similar presentations


Presentation on theme: "© 2006 Lawrenceville Press Slide 1 Chapter 5 The If…Then Statement (One-Way)  Conditional control structure, also called a decision structure  Executes."— Presentation transcript:

1 © 2006 Lawrenceville Press Slide 1 Chapter 5 The If…Then Statement (One-Way)  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.

2 © 2006 Lawrenceville Press Slide 2 Chapter 5 Relational Operators OperatorMeaning = equal to < less than <= less than or equal to > greater than >= greater than or equal to <> not equal to

3 © 2006 Lawrenceville Press Slide 3 Chapter 5 The If…Then…Else Statement (Two Way) 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.

4 © 2006 Lawrenceville Press Slide 4 Chapter 5 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.

5 © 2006 Lawrenceville Press Slide 5 Chapter 5 The If…Then…ElseIf Statement (Multi Way)  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.

6 © 2006 Lawrenceville Press Slide 6 Chapter 5 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

7 © 2006 Lawrenceville Press Slide 7 Chapter 5 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 = 25 Me.lblMessage.Text = "Great!" End Select

8 © 2006 Lawrenceville Press Slide 8 Chapter 5 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.

9 © 2006 Lawrenceville Press Slide 9 Chapter 5 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.

10 © 2006 Lawrenceville Press Slide 10 Chapter 5 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.

11 © 2006 Lawrenceville Press Slide 11 Chapter 5 Compound Boolean Expressions  More than one Boolean expression in a single condition.  Formed using the And, Or, or Not operators.

12 © 2006 Lawrenceville Press Slide 12 Chapter 5 And Truth Table And Exp1Exp2Result True False TrueFalse

13 © 2006 Lawrenceville Press Slide 13 Chapter 5 Or Truth Table Or Exp1Exp2Result True FalseTrue FalseTrue False

14 © 2006 Lawrenceville Press Slide 14 Chapter 5 Not Truth Table Not ExpResult TrueFalse True

15 © 2006 Lawrenceville Press Slide 15 Chapter 5 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)

16 © 2006 Lawrenceville Press Slide 16 Chapter 5 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.

17 © 2006 Lawrenceville Press Slide 17 Chapter 5 Assignment Operators OperatorOperation += addition and then assignment example: counter += 1 -= subtraction and then assignment example: counter -= 1

18 © 2006 Lawrenceville Press Slide 18 Chapter 5 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.

19 © 2006 Lawrenceville Press Slide 19 Chapter 5 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.


Download ppt "© 2006 Lawrenceville Press Slide 1 Chapter 5 The If…Then Statement (One-Way)  Conditional control structure, also called a decision structure  Executes."

Similar presentations


Ads by Google