Presentation is loading. Please wait.

Presentation is loading. Please wait.

IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.

Similar presentations


Presentation on theme: "IS 1181 IS 118 Introduction to Development Tools VB Chapter 03."— Presentation transcript:

1 IS 1181 IS 118 Introduction to Development Tools VB Chapter 03

2 Copyright (c) 2003 by Prentice Hall Chapter 5: Decision Visual Basic.NET

3 IS 1183 Objectives Understand relational and logical operators and use them in logical expressions Code the If block structure to solve various programming problems that involve decisions Appreciate and design various alternatives to the If block Understand and use the Select Case block structure

4 IS 1184 Logical Expressions If statement has simple syntax:  If Condition Then Statement Condition: an expression that can be evaluated as true or false Statement: a VB statement

5 IS 1185 Relational Operators Used to compare operands and decide if the relationship is true  Examples include =, >, and <= With string data, “a” is greater than “A”  To make comparison case insensitive, set Option Compare to “Text” Set in code with Option Compare Text statement Set as default in properties page for project Logical expression can be part of assignment statement  Left side of expression must be a variable or a property of a control that can be set to True or False

6 IS 1186 Logical Operators Compares two or more expressions and returns appropriate value  Not operator negates operand on its right  And operator returns True when all expressions are True  AndAlso operator returns True when all expressions are true; stops evaluating when a False expression is found  Or operator returns True if one or more expressions are True  OrElse operator returns True if one or more expressions are true; stops evaluating when a True expression is found  XOr operator returns True if exactly one expression is True

7 IS 1187 Logical Operator Considerations AndAlso operator is more efficient than And  AndAlso stops evaluating when a false expression is found, but And evaluates all  Put conditions more likely to be false first OrElse operator is more efficient than Or  OrElse stops evaluating when a true expression is found, but Or evaluates all  Put conditions more likely to be true first

8 IS 1188 The If Block Similar to If statement, but statements to be executed are not on same line as If keyword  Allows you to execute more than one statement if condition is True Terminated with End If statement

9 IS 1189 The If…Then…Else…End If Block Adds an Else clause  Contains statements to be executed if the condition is False Terminated with End If keyword Either clause can be left blank  Often used for statements that will be carried out only if condition is False If chkSort.Checked Then cboSort.Enabled = True Else cboSort.Enabled = False End If

10 IS 11810 The If…Then…ElseIf…Then…Else… End If Block Used when a decision depends on several conditions ElseIf keyword used to define each condition Often has a “catch-all” Else clause Terminated with End If If Score >= 90 Then Grade = “A” ElseIf Score >=80 Then Grade = “B” Else Grade = “C” End If ElseIf Clause also contains Then keyword

11 IS 11811 If Statement Considerations Varying conditions  The ElseIf block is useful when conditions vary  Conditions can be based on entirely different factors Nesting If Blocks  Using another If block in either the Then clause or the Else clause  Nested block must be terminated before returning to the outer block Indent each nested block to enhance readability Use comments liberally  Especially for nested If blocks

12 IS 11812 Alternatives to the If Block Immediate If (IIf) function  Similar to the If function in Excel  Syntax: IIf(condition, expression1, expression2) Returns expression1 if true, expression2 if false  Function is inefficient All three arguments passed to function are evaluated Computation  Set variables or properties by computation i.e. cboSortField.Enabled = chkSort.Checked rather than setting the Enabled property in an If block

13 IS 11813 Select Case Block Useful when decision depends on different results of the same expression  Begins with Select Case statement Each condition coded with Case criterion  Should have “catch all” Case Else clause  Terminated with End If statement Criteria are mutually exclusive  Once a criterion is found to be true, that block is executed  Place most restrictive criterion at top

14 IS 11814 Syntax Rules To test for equality, give the value,  i.e. Case 80 To test several values for equality, separate list with commas,  i.e. Case 80, 81, 85 To specify a closed range, insert the “To” keyword between upper and lower bounds  i.e. Case 80 to 90 To specify an open range, use the “Is” keyword  i.e. Case Is < 0

15 IS 11815 Nesting Select Case Blocks Similar to nesting If blocks  Nested blocks must terminate before returning to outer block  Make extensive use of comments

16 IS 11816 Nested Select Statement Each nested block is indented Nested block terminates before returning to outer block

17 IS 11817 If Statement Nested Inside Select Case Statement If block is terminated before returning to Select Case block

18 IS 11818 Application Example: Tuition Calculator Analyze and determine system requirements  Calculate student tuition Based on residence status and hours taken Design visual interface  Need controls for hours taken and residence Use radio buttons for status, since limited number of options that won’t change Use text box for hours taken

19 IS 11819 Visual Interface Tuition displayed in label, formatted to look like text box Checked property of In State radio button set to true to create Default

20 IS 11820 Code The Solution Use If…ElseIf structure inside Select Case Use Nested Select Case Statement

21 IS 11821 An Alternative Solution Use combo boxes to display residence status and range of hours taken  Offers more flexibility, but code is not as clear

22 IS 11822 Visual Interface Items added to combo box at runtime; SelectedIndex property used to set default value

23 Copyright (c) 2003 by Prentice Hall23 Code the Solution SelectedIndex property used to determine which option is selected

24 IS 11824 Block Level Declarations Variables may be declared inside a block  Variables exist only inside the block  You may declare the same variable name inside each block i.e. a variable named ID may be declared in the If block, the ElseIf block, and Else block While you can do this, it is confusing to read and debug  You may not declare a variable name if you have declared a procedure-level variable with same name

25 IS 11825 Summary Two structures commonly used to handle decisions: If and Select Case If structure involves testing whether the condition following If keyword is True or False Relational operators include =, <>, and <. Each compares two operands to determine if relation is True Commonly used logical operators include And, Or, AndAlso, and OrElse

26 IS 11826 Summary String comparisons can be “Binary” (case sensitive) or “Text” (case insensitive) If operational precedence is confusing, use parentheses to encloses expression(s) Four ways to construct If structure  Simple If statement  Simple If block  If…Else block  If…ElseIf…Else block If blocks can be nested

27 IS 11827 Summary Use comments to explain the purpose of the condition Computer evaluates logical or relational expressions differently than we might interpret them The AndAlso and OrElse operators are more efficient then the And and Or operators In some cases, computations can replace the use of If blocks  Can be more efficient, but can be harder to read

28 IS 11828 Summary Select Case structure can replace If…ElseIf block when decision depends on the result of a single expression Select Case structure can be nested and can also be nested with If structure Tuition calculation example illustrates how Select Case structure can be nested You can declare block level variables in both the If blocks and the Case blocks


Download ppt "IS 1181 IS 118 Introduction to Development Tools VB Chapter 03."

Similar presentations


Ads by Google