Decisions and Conditions: Mcgraw-Hill © 2009 The Mcgraw-Hill Companies, Inc. All Rights Reserved
Decisions and Conditions: Mcgraw-Hill © 2009 The Mcgraw-Hill Companies, Inc. All Rights Reserved
McGraw-Hill
Objectives (1 of 2)
Use If statements to control the flow of logic. Understand and use nested If statements. Read and create action diagrams that illustrate the logic in a selection process. Evaluate conditions using the comparison operators. Combine conditions using And, Or, AndAlso, and OrElse. Test the Checked property of radio buttons and check boxes. Perform validation on numeric fields.
2009 The McGraw-Hill Companies, Inc. All rights reserved.
McGraw-Hill
4-2
Objectives (2 of 2)
Use a Case structure for multiple decisions. Use one event procedure to respond to the events for multiple controls and determine which control caused the event. Call an event procedure from another procedure. Create message boxes with multiple buttons and choose alternate actions based on the user response. Debug projects using breakpoints, stepping program execution, and displaying intermediate results.
McGraw-Hill
4-3
If Statements
Used to make decisions If true, only the Then clause is executed, if false, only Else clause, if present, is executed Block IfThenElse must always conclude with End If. Then must be on same line as If or ElseIf. End If and Else must appear alone on a line. Note: ElseIf is 1 word, End If is 2 words.
McGraw-Hill
4-4
4-5
IfThenElse Example
unitsDecimal = Decimal.Parse(unitsTextBox.Text) If unitsDecimal < 32D Then freshmanRadioButton.Checked = True Else freshmanRadioButton.Checked = False End IF
McGraw-Hill
4-6
Charting If Statements
A Uniform Modeling Language (UML) activity diagram is a useful tool for showing the logic of an IF statement. Can be used to help programmers organize their thoughts and design projects more quickly UML includes several types of diagrams.
McGraw-Hill
4-7
Conditions
Test in an If statement is based on a condition. Six relational operators are used for comparison. Negative numbers are less than positive numbers. An equal sign is used to test for equality. Strings can be compared. Enclose strings in quotes. JOAN is less than JOHN HOPE is less than HOPELESS Numbers are always less than letters. 300ZX is less than Porsche
McGraw-Hill
4-8
When entering IF statements, the editor automatically adds the Then and End If. The editor attempts to correct errors by supplying a colon if multiple statements are entered on a line. The colon is a statement terminator. Good programming practices dictate that there should be only one statement per line so remove the extra colon if found, and correct the syntax.
2009 The McGraw-Hill Companies, Inc. All rights reserved.
McGraw-Hill
4-9
The test in an IF statement if based on a condition. To form conditions, comparison operators are used.
> < = <> >= <=
McGraw-Hill
4-10
Comparing Strings
Comparison begins with the left-most character and proceeds one character at a time, left to right. If a character in one string is not equal to the corresponding character in the 2nd string, the comparison terminates. The string with the lower-ranking character is judged less than the other.
(collating sequence) for all letters, numbers, and special characters.
McGraw-Hill
4-11
Use ToUpper and ToLower methods of the String class to return the uppercase or lowercase equivalent of a string, respectively.
If nameTextBox.Text.ToUpper( ) = BASIC" Then ' Do something. End If When converting name TextBox.Text to uppercase, it must be compared to an uppercase literal (BASIC) if it is to evaluate as True.
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved.
4-12
Compound Condition
If maleRadioButton.Checked And _ Integer.Parse(ageTextBox.Text) < 21 Then minorMaleCountInteger += 1 End If If juniorRadioButton.Checked Or seniorRadioButton.Checked Then upperClassmanInteger += 1 End If
McGraw-Hill
4-13
Compound conditions can combine multiple logical conditions. When both And and Or are evaluated, And is evaluated before the Or. Use parenthesis to change the order of evaluation condition inside the parenthesis is evaluated first.
If saleDecimal > 1000.0D Or discountRadioButton.Checked _ And stateTextBox.Text.ToUpper( ) <> "CA" Then ' Code here to calculate the discount. End If
McGraw-Hill
4-14
Short-Circuit Operations
Visual Basic has 2 operators that provide shortcircuit evaluation for compound conditions: the AndAlso and OrElse. VB evaluates both expressions for True or False, then evaluates the And. The OrElse is designed to short circuit when the first condition evaluates True. AndAlso and OrElse are used for advanced programming when the 2nd expression should not be executed for some reason.
2009 The McGraw-Hill Companies, Inc. All rights reserved.
McGraw-Hill
4-15
Nested If Statements
If tempInteger > 32 Then If tempInteger > 80 Then commentLabel.Text = "Hot" Else commentLabel.Text = "Moderate" End If Else commentLabel.Text = "Freezing" End If
McGraw-Hill
4-16
McGraw-Hill
4-17
For longer, more complex messages, store the message text in a String variable and use that variable as an argument of the Show method. VB will wrap longer messages to a second line. Include ControlChars to control the line length and position of the line break in multiple lines of output. Combine multiple NewLine constants to achieve double spacing and create multiple message lines.
McGraw-Hill
4-18
4-19
McGraw-Hill
4-20
ControlChars Constants
ControlChar Constant CfLf Cr Lf Description Carriage return/linefeed character combination Carriage return Line feed
NewLine
NullChar Tab Back
FormFeed
VerticalTab Quote
McGraw-Hill
4-21
Use MessageBoxButtons constants to display more than one button in the Message Box. Message Box's Show method returns a DialogResult object that can be checked to see which button the user clicked. Declare a variable to hold an instance of the DialogResult type to capture the outcome of the Show method.
McGraw-Hill
4-22
MessageBoxButtons.YesNo
McGraw-Hill
4-23
McGraw-Hill
4-24
Use a different signature for the Message Box Show method to specify a default button. Add the MessageBoxDefaultButton argument after the MessageBoxIcons argument. Set message alignment with MessageBoxOptions argument.
McGraw-Hill
4-25
Input Validation
Check to see if valid values were entered by user before beginning calculationscalled validation. Check for a range of values (reasonableness).
McGraw-Hill
4-26
McGraw-Hill
4-27
Good professional technique is to set up a modulelevel variable to hold the selection a user makes. Key to using a shared event procedure is the sender argument.
McGraw-Hill
4-28
Examples
OR
[Call] ProcedureName ( ) Keyword Call is optional and rarely used. Call clearButton_Click (sender, e)
ClearButton_Click (sender, e)
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved.
4-29
A form with buttons that perform overlapping functions The New Order button must do the same tasks as Clear for Next Item.
4-30
McGraw-Hill
Debugging Tools
Use Debug Menu and Debug options on VB Standard toolbar. Place Debug.WriteLine method in code. Set BreakPoints to stop at a particular location in code and watch what happens. Step Into, Step Over, Step Out Edit and Continue Locals Window, and Autos Window
expressions, and conditions.
McGraw-Hill
4-31
The debugging options on the Debug menu showing the keyboard shortcut keys
4-32
McGraw-Hill
McGraw-Hill
4-33
Breakpoints
4-34
When attempting to continue execution after making changes in Debugging mode, this dialog box appears if the edits are too majorClick Restart to recompile and run again.
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved.
4-35
Shows values of local variables that are within scope of current statement
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved.
4-36
Autos Window
Automatically adjusts to show variables and properties that appear in previous and next few lines
McGraw-Hill 2009 The McGraw-Hill Companies, Inc. All rights reserved.
4-37