Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC110 Fall 20041 Chapter 5: Decision Visual Basic.NET.

Similar presentations


Presentation on theme: "CSC110 Fall 20041 Chapter 5: Decision Visual Basic.NET."— Presentation transcript:

1 CSC110 Fall 20041 Chapter 5: Decision Visual Basic.NET

2 2CSC110 Fall 2004 Objectives n Understand relational and logical operators and use them in logical expressions n Code the If block structure to solve various programming problems that involve decisions n Appreciate and design various alternatives to the If block n Understand and use the Select Case block structure

3 3CSC110 Fall 2004 Logical Expressions n If statement has simple syntax: –If Condition Then Statement »Condition: an expression that can be evaluated as true or false »Statement: a VB statement

4 4CSC110 Fall 2004 Relational Operators n Used to compare operands and decide if the relationship is true –Examples include =, >, and, and <= n 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 n 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

5 5CSC110 Fall 2004 Logical Operators n 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 –Or operator returns True if one or more expressions are True

6 6CSC110 Fall 2004 Order of Operations n If a1 a4 and a5 a4 and a5 < a6 Then n And has precedence over Or n All comparison operators have precedence over all logical operators n Use parentheses to alter the order of evaluation

7 7CSC110 Fall 2004 The If Block n 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 n Terminated with End If statement

8 8CSC110 Fall 2004 The If…Then…Else…End If Block n Adds an Else clause –Contains statements to be executed if the condition is False n Terminated with End If keyword n 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

9 9CSC110 Fall 2004 If…Then…ElseIf…Then…Else… End If Block n Used when a decision depends on several conditions n ElseIf keyword used to define each condition n Often has a “catch-all” Else clause n 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

10 10CSC110 Fall 2004 If Statement Considerations n Varying conditions –The ElseIf block is useful when conditions vary –Conditions can be based on entirely different factors n 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 n Use comments liberally –Especially for nested If blocks

11 11CSC110 Fall 2004 "If" statement and option buttons

12 12CSC110 Fall 2004 Messages in Message boxes n Special window displaying message to user n Form: MsgBox “message” [,buttons][, “t.b. caption”] n Example: MsgBox “Numeric ID only”, vbOkOnly, “Error”

13 13CSC110 Fall 2004 Displaying a Message String n Use & to concatenate strings (“Concatenate” means join end to end) n The VB intrinsic constant vbCRLF creates a new line in a string MsgBox strMessage, vbOKOnly, stTitle

14 14CSC110 Fall 2004 Message box return values ConstantValueDescription vbOK1OK button pressed. vbCancel2Cancel button pressed. vbAbort3Abort button pressed. vbRetry4Retry button pressed. vbIgnore5Ignore button pressed. vbYes6Yes button pressed. vbNo7No button pressed.

15 15CSC110 Fall 2004 Input Validation n Checking a data type: IsNumeric & IsDate n IsNumeric checks & returns true or false If IsNumeric(txtQty.Text) Then lblDue.Caption = curPrice + CInt(txtQty.text) lblDue.Caption = curPrice + CInt(txtQty.text) n Validating value ranges If Val(txtHours.Text) > 10 And _ Val(txtHours.Text) <= 80 Then... Val(txtHours.Text) <= 80 Then...

16 16CSC110 Fall 2004 Data Validation n IsDate returns true or false depending on whether or not a value is a date If IsDate(txtData.text) Then … n the VarType function return a number that corresponds to the data type stored in a variant. If VarType(varValue) = 0 Then...

17 17CSC110 Fall 2004 Calling Event Procedures n An event procedure is a subprocedure that reacts to a specific event such as a button click. n You can call any given event procedure from multiple locations, as long as the procedure is in the same form or is public Example: Call cmdCalculate_Click Example: Call cmdCalculate_Click n Suffix is event, prefix is object name

18 18CSC110 Fall 2004 Hands on Programming Example

19 19CSC110 Fall 2004 Select Case Block n 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 Select statement n Criteria are mutually exclusive –Once a criterion is found to be true, that block is executed –Place most restrictive criterion at top

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

21 21CSC110 Fall 2004 Nesting Select Case Blocks n Similar to nesting If blocks –Nested blocks must terminate before returning to outer block –Make extensive use of comments

22 22CSC110 Fall 2004 If Statement Nested Inside Select Case Statement If block is terminated before returning to Select Case block

23 23CSC110 Fall 2004 Application Example: Tuition Calculator n Analyze and determine system requirements –Calculate student tuition »Based on residence status and hours taken n 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

24 24CSC110 Fall 2004 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

25 25CSC110 Fall 2004 Code Solution Select Case with If then Blocks

26 26CSC110 Fall 2004 Code solution Nested Select Case

27 27CSC110 Fall 2004 An Alternative Solution n Use combo boxes to display residence status and range of hours taken –Offers more flexibility, but code is not as clear

28 28CSC110 Fall 2004 Visual Interface Items added to combo box at runtime; SelectedIndex property used to set default value

29 29CSC110 Fall 2004 Code the Solution SelectedIndex property used to determine which option is selected

30 30CSC110 Fall 2004 Block Level Declarations n 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 module-level variable with same name

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

32 32CSC110 Fall 2004 Summary If Block n If operational precedence is confusing, use parentheses to encloses expression(s) n Four ways to construct If structure –Simple If statement –Simple If block –If…Else block –If…ElseIf…Else block n If blocks can be nested

33 33CSC110 Fall 2004 Summary n Use comments to explain the purpose of the condition n Computer evaluates logical or relational expressions differently than we might interpret them n String comparisons can be “Binary” (case sensitive) or “Text” (case insensitive) –A < a “Cat” < “cat”

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


Download ppt "CSC110 Fall 20041 Chapter 5: Decision Visual Basic.NET."

Similar presentations


Ads by Google