is 1181 is 118 introduction to development tools vb chapter 03

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

Post on 22-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

IS 118 1

IS 118 Introduction to Development Tools

VB Chapter 03

Copyright (c) 2003 by Prentice Hall

Chapter 5:Decision

Visual Basic.NET

IS 118 3

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

IS 118 4

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

IS 118 5

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

IS 118 6

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

IS 118 7

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

IS 118 8

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

IS 118 9

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

IS 118 10

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

IS 118 11

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

IS 118 12

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

IS 118 13

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

IS 118 14

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

IS 118 15

Nesting Select Case Blocks

Similar to nesting If blocks Nested blocks must terminate before returning

to outer block Make extensive use of comments

IS 118 16

Nested Select Statement

Each nested block is indented

Nested block terminates before returning to outer block

IS 118 17

If Statement Nested Inside Select Case Statement

If block is terminated before returning to Select Case block

IS 118 18

Application Example:Tuition CalculatorAnalyze 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

IS 118 19

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

IS 118 20

Code The Solution

Use If…ElseIf structure inside Select Case

Use Nested Select Case Statement

IS 118 21

An Alternative Solution

Use combo boxes to display residence status and range of hours taken Offers more flexibility, but code is not as clear

IS 118 22

Visual Interface

Items added to combo box at runtime; SelectedIndex property used to set default value

Copyright (c) 2003 by Prentice Hall 23

Code the Solution

SelectedIndex property used to determine which option is selected

IS 118 24

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

IS 118 25

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

IS 118 26

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

IS 118 27

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

IS 118 28

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