© 2012 emc publishing, llc slide 1 chapter 4 the if…then statement conditional control...
TRANSCRIPT
© 2012 EMC Publishing, LLC
Slide 1
Chapter 4
The If…Then StatementChapter 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 statementIf x = 5 Then
y = 20End If
assigns the value 20 to y only if x is equal to 5.
© 2012 EMC Publishing, LLC
Slide 2
Chapter 4
Relational OperatorsChapter 4
Relational Operators
Operator Meaning= equal to< less than<= less than or equal to> greater than>= greater than or equal to<> not equal to
© 2012 EMC Publishing, LLC
Slide 3
Chapter 4
The If…Then…Else StatementChapter 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 Theny = 20
Elsey = 10
End Ifassigns the value 20 to y if x is equal to 5 or the value 10 if x is not equal to 5.
© 2012 EMC Publishing, LLC
Slide 4
Chapter 4
Nested If…Then…Else Statements
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 Theny = 20
Else If x > 5 Then
y = 10Else
y = 0End If
End Ifevaluates the nested If…Then…Else only when x is not equal to 5.
© 2012 EMC Publishing, LLC
Slide 5
Chapter 4
The If…Then…ElseIf StatementChapter 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 Theny = 20
ElseIf x < 10 Theny = 40
ElseIf x < 15 Theny = 80
End If would give very different results if the conditions were ordered differently.
© 2012 EMC Publishing, LLC
Slide 6
Chapter 4
The Select…Case StatementChapter 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 2Me.lblMessage.Text = "human"
Case 4 Me.lblMessage.Text = "beast"
Case 8 Me.lblMessage.Text = "insect"
Case Else Me.lblMessage.Text = "???"
End Select
© 2012 EMC Publishing, LLC
Slide 7
Chapter 4
The Select…Case Is StatementChapter 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 scoreCase Is < 10
Me.lblMessage.Text = "Nice try."Case Is < 25
Me.lblMessage.Text = "Good."Case Is >= 25
Me.lblMessage.Text = "Great!"End Select
© 2012 EMC Publishing, LLC
Slide 8
Chapter 4
The Rnd() FunctionChapter 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:
Int(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.
© 2012 EMC Publishing, LLC
Slide 9
Chapter 4
AlgorithmsChapter 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.
Flowcharts are another option
Algorithms allow a programmer to think through a program before actually typing code, which may reduce errors in logic.
© 2012 EMC Publishing, LLC
Slide 10
Chapter 4
Static VariablesChapter 4
Static Variables
Declared with the keyword Static instead of Dim.
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.
© 2012 EMC Publishing, LLC
Slide 11
Chapter 4
Compound Boolean ExpressionsChapter 4
Compound Boolean Expressions
More than one Boolean expression in a single condition.
Formed using the And, Or, or Not operators.
Order of operations: Not evaluated first, And second, Or last.
© 2012 EMC Publishing, LLC
Slide 12
Chapter 4
And Truth TableChapter 4
And Truth Table
And
Exp1 Exp2 Result
True True True
True False False
False True False
False False False
© 2012 EMC Publishing, LLC
Slide 13
Chapter 4
Or Truth TableChapter 4
Or Truth Table
Or
Exp1 Exp2 Result
True True True
True False True
False True True
False False False
© 2012 EMC Publishing, LLC
Slide 14
Chapter 4
Not Truth TableChapter 4
Not Truth Table
Not
Exp Result
True False
False True
© 2012 EMC Publishing, LLC
Slide 15
Chapter 4
The MessageBox ClassChapter 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)
© 2012 EMC Publishing, LLC
Slide 16
Chapter 4
Counter VariablesChapter 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 + CONSTANT
Should be initialized when declared and updated by an unchanging amount.
© 2012 EMC Publishing, LLC
Slide 17
Chapter 4
Assignment OperatorsChapter 4
Assignment Operators
Operator Operation+= addition and then assignment-= subtraction and then assignment
© 2012 EMC Publishing, LLC
Slide 18
Chapter 4
The CheckBox ControlChapter 4
The CheckBox Control
Check boxes allow the user to select options.. Unlike Radio buttons, more than one can be selected.
(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.
© 2012 EMC Publishing, LLC
Slide 19
Chapter 4
Implicit Line ContinuationChapter 4
Implicit Line Continuation
A statement typically fits on one line, but can be continued onto the next line using a line-continuation sequence:
If Not (Me.chkBed.Checked And Me.chkLunch.Checked _And Me.chkHomework.Checked And Me.chkTeeth.Checked) Then...
In many cases, you can continue a statement on the next consecutive line without using the underscore character (_):• after a comma (,)• after an open parenthesis (() or before a closing parenthesis ())• after an open curly brace ({) or before a closing curly brace (})• after assignment operators (=, &=, :=, +=, -=, *=, /=, \=, ^=)• after binary operators (+, -, /, *, Mod, <>, <, >, <=, >=, And, Or)