Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4: Decisions and Conditions

Similar presentations


Presentation on theme: "Chapter 4: Decisions and Conditions"— Presentation transcript:

1 Chapter 4: Decisions and Conditions
REV 01 Chapter 4: Decisions and Conditions Objectives Use If statements to control the flow of logic. Understand and use nested If statements. 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. Use a Case structure for multiple decisions. Use one event procedure to respond to the events for multiple controls Call an event procedure from another procedure. Create message boxes with multiple buttons Debug projects using breakpoints, stepping program execution, and displaying intermediate results. DDC Programming III

2 Chapter 4: If Statements
REV 01 Chapter 4: If Statements Used to make decisions If true, only the Then clause is executed, if false, only Else clause, if present, is executed Block If…Then…Else 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. A powerful capability of the computer is its ability to make decisions and to take alternate courses of action based on the outcome. DDC Programming III 2

3 Chapter 4: If…Then…Else - General Form
REV 01 Chapter 4: If…Then…Else - General Form If (condition) Then statement(s) [ElseIf (condition) Then statement(s)] [Else End If A decision made by the computer is formed as a question: Is a given condition true or false? If it is true, do one thing, if it is false, do something else. Example: If the sun is shining Then (condition) go to the beach (action to take if condition is true) Else go to class (action to take if condition is false) End If Logic of an If /Then/ Else statement Logic of an If statement without the Else DDC Programming III 3

4 Chapter 4: If…Then…Else - Example
REV 01 unitsDecimal = Decimal.Parse(unitsTextBox.Text) If unitsDecimal < 32D Then freshmanRadioButton.Checked = True Else freshmanRadioButton.Checked = False End IF When the number of unitsDecimal is less than 32, select the radio button for Freshman; otherwise, make sure the radio button is deselected. DDC Programming III 4

5 Chapter 4: Charting If Statements
REV 01 Chapter 4: 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 design projects more quickly UML includes several types of diagrams. Activity diagram-visual planning tool for decisions/actions for either the entire application or single procedure Diamond shape symbol (decision symbol) represents a condition. Branches from the decision symbol indicate which path to take for different results of the decision. DDC Programming III 5

6 Chapter 4: Conditions Test in an If statement is based on a condition.
REV 01 Chapter 4: 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 Note that strings can be compared only to other strings, and numeric values can be compared only to other numeric values, whether a variable, constant, property, or arithmetic expression. VB actually stores string characters in Unicode, a coding system that uses 2 bytes to store every character. DDC Programming III 6

7 Chapter 4: The Helpful Editor
REV 01 Chapter 4: The Helpful Editor 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. The VS code editor is very helpful when If statements are entered. If you type EndIf without the space, the editor adds the space for you. DDC Programming III 7

8 Chapter 4: The 6 Relational Operators
REV 01 Chapter 4: The 6 Relational Operators The test in an IF statement if based on a condition. To form conditions, comparison operators are used. *Refer to Table 4.1 <> not equal to >= greater than or equal to Integer.Parse(QuantityTextBox.Text) >= 500 <= less than or equal to Name1TextBox.Text <= Name2TextBox.Text > < = <> >= <= DDC Programming III 8

9 Chapter 4: Comparing Strings
REV 01 Chapter 4: 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. Ranking is based on ANSI code, an established order (collating sequence) for all letters, numbers, and special characters. String variables can be compared to other string variables, string properties, or string literals enclosed in quotes. ANSI stands for American National Standards Institute. DDC Programming III 9

10 Chapter 4: Comparing Upper and Lowercase Characters
REV 01 Chapter 4: Comparing Upper and Lowercase Characters 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 comparing strings, the case of the characters is important—an uppercase Y is not equal to a lowercase y. Users may type a name or word in upper or lower case or a combination of both; there are checks for all possibilities. General Form: TextString.ToUpper() TextString.ToLower() When converting name TextBox.Text to uppercase, it must be compared to an uppercase literal (“BASIC”) if it is to evaluate as True. DDC Programming III 10

11 Chapter 4: Compound Condition
REV 01 Chapter 4: 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 Compound conditions can be used to test more than one condition. Create compound conditions (key term) by joining conditions with logical operators (key term). The logical operators are Or, And, Not, AndAlso, OrElse, and Xor. Each side of the logical operator must be a complete condition. Example: CountInteger > 10 Or < 0 is incorrect. Instead, it must be CountInteger > 10 Or CountInteger < 0 DDC Programming III 11

12 Chapter 4: Combining Logical Operators
REV 01 Chapter 4: Combining Logical Operators 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 > D Or discountRadioButton.Checked _ And stateTextBox.Text.ToUpper( ) <> "CA" Then ' Code here to calculate the discount. End If DDC Programming III

13 Chapter 4: Short-Circuit Operations
REV 01 Chapter 4: Short-Circuit Operations Visual Basic has 2 operators that provide short-circuit 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. In a regular Or operation, if one or the other condition is True, the entire compound condition is True. DDC Programming III 13

14 Chapter 4: Nested If Statements
REV 01 Chapter 4: Nested If Statements If tempInteger > 32 Then If tempInteger > 80 Then commentLabel.Text = "Hot" Else commentLabel.Text = "Moderate" End If commentLabel.Text = "Freezing" If statements that contain additional If statements are said to be nested If statements. You can nest Ifs in both the Then and Else. In fact, you may continue to nest Ifs within Ifs as long as each If has an End If. DDC Programming III 14

15 Chapter 4: Using If Statements with Radio Buttons & Check Boxes
REV 01 Chapter 4: Using If Statements with Radio Buttons & Check Boxes Instead of coding the CheckedChanged events, use If statements to see which are selected. Place your code in the Click event of Buttons, such as an OK or Apply button; VS checks to see which options are selected. Use If Statements to determine which options are selected. No action will occur when you click on a radio button or check box. Instead, when you click on the OK button, VS checks to see which options are selected. DDC Programming III 15


Download ppt "Chapter 4: Decisions and Conditions"

Similar presentations


Ads by Google