Vous êtes sur la page 1sur 24

Copyright 2011 Pearson Addison-Wesley

Addison Wesley
is an imprint of
2011 Pearson Addison-Wesley. All rights reserved.
Addison Wesley
is an imprint of
Chapter 4
Making Decisions
Copyright 2011 Pearson Addison-Wesley
Introduction

This chapter covers the Visual Basic decision


statements

IfThen

IfThenElse

IfThenElseIf

Select Case

It also discusses the use of

Radio Buttons

Check Boxes

Message Boxes

Input Validation
Chapter 4 lide
!
Copyright 2011 Pearson Addison-Wesley
Addison Wesley
is an imprint of
2011 Pearson Addison-Wesley. All rights reserved.
Addison Wesley
is an imprint of

ection 4"#
The Decision tructure

The decision structure allo$s a program%s


logic to have more than

one path of execution"


Copyright 2011 Pearson Addison-Wesley
&rder of tatement 'xecution

Thus far( our code has )een executed


se*uentiall+ in a se*uence structure

To $rite meaningful programs $e need


multiple paths of execution

ome statements should )e executed under


certain circumstances in a decision structure

This chapter presents the means to execute


statements conditionall+

,ext chapter presents the means to execute the


same statements repeatedl+
Chapter 4 lide
4
Copyright 2011 Pearson Addison-Wesley
The Decision tructure

-lo$chart of a
t+pical decision
structure

'valuate the
condition

Is it cold outside.

'xecute or skip
over some code

If +es( $ear a coat


Chapter 4 lide
/
Is it cold outside?
Wear a coat.
True
False
Copyright 2011 Pearson Addison-Wesley
Addison Wesley
is an imprint of
2011 Pearson Addison-Wesley. All rights reserved.
Addison Wesley
is an imprint of

ection 4"!
The If"""Then tatement

The If"""Then statement causes other


statements to execute onl+

$hen an expression is true"


Copyright 2011 Pearson Addison-Wesley
0eneral -ormat

If the expression is True( execute the


statements )et$een IfThen and End If

&ther$ise( the statements are skipped


Chapter 4 lide
1
If expression Then
statement
(more statements may follow)
'nd If
Copyright 2011 Pearson Addison-Wesley
Relational &perators

2suall+ a condition is formed using a relational


operator

3 relational operator determines if a specific


relationship exists )et$een t$o values
> 0reater than
< 4ess than
= '*ual to
<> ,ot e*ual to
>= 0reater than or e*ual to
<= 4ess than or e*ual to
Chapter 4 lide
5
Copyright 2011 Pearson Addison-Wesley
Boolean 'xpressions

Relational operators are )inar+ meaning


the+ use t$o operands( for example6
length > width Is length greater than width?
size <= 1 Is size less than or e!ual 1?

Relational operators are used in Boolean


expressions $hich +ield a true or false result
Chapter 4 lide
7
Copyright 2011 Pearson Addison-Wesley
8utting It 3ll Together
If9Then statement examples6
Chapter 4 lide
#:
If decSales > " Then
#essage$ox%Show&'(ou)*e earned a +onus,'-
decCo..ission/ate = %10
int1a2s3ff = int1a2s3ff 4 1
End If
If decSales > " Then
#essage$ox%Show&'(ou)*e earned a +onus,'-
End If
Copyright 2011 Pearson Addison-Wesley
Rules to Remem)er

The If and the Then must )e on the same line

&nl+ a remark ma+ follo$ the Then

The End If must )e on a separate line

&nl+ a remark ma+ follo$ the End If

Tutorial 4;# presents an application that uses


the IfThen statement
Chapter 4 lide
##
Copyright 2011 Pearson Addison-Wesley
8rogramming t+le

The code )et$een the IfThen and the End If


is indented

Visual Basic does not re*uire this

It is a convention among programmers to aid


in the reada)ilit+ of programs

B+ default( the Visual Basic editor $ill


automaticall+ do this indentation as +ou enter
+our program
Chapter 4 lide
#!
Copyright 2011 Pearson Addison-Wesley
2sing Relational &perators $ith Math
&perators

Math operators are evaluated )efore relational operators

int5 4 int( and int6 7 int$ are evaluated first

Most programmers prefer to use parentheses to clarif+ the


order of operations
Chapter 4 lide
#<
If int5 4 int( > int6 7 int$ Then
l+l#essage%Text = 'It is true,'
End If
If &int5 4 int(- > &int6 8 int$- Then
l+l#essage%Text = 'It is true,'
End If
Copyright 2011 Pearson Addison-Wesley
2sing -unction Calls $ith Relational &perators

'ither or )oth relational operator operands


ma+ )e function calls

The return value of the function call is


compared to the value using the relational
operator
Chapter 4 lide
#4
If CInt&txtInput%Text- < 1 Then
l+l#essage%Text = 'It is true,'
End If
Copyright 2011 Pearson Addison-Wesley
2sing Boolean Varia)les as -lags

3 flag is a Boolean varia)le that signals $hen some


condition exists in the program

ince a Boolean varia)le is either True or 9alse( it can


)e used as the condition of an IfThen statement

ince a Boolean varia)le alread+ evaluates to True or 9alse(


an = operator is not re*uired
Chapter 4 lide
#/
If +ln:uota#et Then
l+l#essage%Text = '(ou ha*e .et 2our sales !uota'
End If
Copyright 2011 Pearson Addison-Wesley
Addison Wesley
is an imprint of
2011 Pearson Addison-Wesley. All rights reserved.
Addison Wesley
is an imprint of

ection 4"<
The If"""Then"""'lse tatement

The If"""Then"""'lse statement executes one


group of statements

if the Boolean expression is true and


another group of statements if

the Boolean expression is false"


Copyright 2011 Pearson Addison-Wesley
0eneral -ormat

If the
expression is
True

execute the
statements
)et$een If
Then and Else

If the
expression is
9alse

execute the
statements
)et$een Else
and End If
Chapter 4 lide
#1
If expression Then
statement
(more statements may follow)
'lse
statement
(more statements may follow)
'nd If
Copyright 2011 Pearson Addison-Wesley
-lo$chart and 8seudocode

Chapter 4 lide
#5
dblTemperature
< 40?
Display Message
A little cold, isnt it?
True
False
Display Message
Nice weather were
having!
If temperature < 40 Then
Display the message A little cold, isnt it?
!lse
Display the message "ice #eather #ere ha$ing%
!nd If
Copyright 2011 Pearson Addison-Wesley
T$o Mutuall+ 'xclusive Choices

The IfThenElse has t$o choices

The condition $ill either )e True or -alse

o either the Then clause or Else clause $ill )e


executed

These are t$o mutuall+ exclusive choices

Tutorial 4;! contains an example of the If


ThenElse construct
Chapter 4 lide
#7
Copyright 2011 Pearson Addison-Wesley
Addison Wesley
is an imprint of
2011 Pearson Addison-Wesley. All rights reserved.
Addison Wesley
is an imprint of

ection 4"4
The If"""Then"""'lseIf tatement

The If"""Then"""'lseIf statement is like a chain


of

If"""Then"""'lse statements" The+ perform


their tests(

one after the other( until one of them is


found to )e true"
Copyright 2011 Pearson Addison-Wesley
Multiple 8ossi)le Choices

The IfThenElseIf statement allo$s for an


entire series of possi)le choices

In pseudocode6
Chapter 4 lide
!#
If it is $ery cold Then
&ear a coat
!lseif it is chilly
&ear a light 'ac(et
!lseif it is #indy
&ear a #ind)rea(er
!lseif it is hot
&ear no 'ac(et
Copyright 2011 Pearson Addison-Wesley
Multiple 8ossi)le Choices

'ach of the series of conditions in an If


ThenElseIf is tested in se*uence

=hen a condition is true( the remaining


conditions are ignored

The order of the conditions is vital

=rong order can result in $rong decision ; called a


logic error

=hat if it%s chill+ and $ind+.

If $ind+ is tested )efore chill+( +ou%d go out $ith a


$ind)reaker $hen +ou need a >acket
Chapter 4 lide
!!
Copyright 2011 Pearson Addison-Wesley
0eneral -ormat

This construction is like a chain of If%%%Then%%%Else statements

The Else part of one statement is linked to the If part of


another
Chapter 4 lide
!<
If expression Then
statement
(more statements may follow)
'lseIf expression Then
statement
(more statements may follow)
?put as man+ 'lseIf statements as necessar+@
'lse
statement
(more statements may follow)
Copyright 2011 Pearson Addison-Wesley
-lo$chart
Chapter 4 lide
!4
Very cold?
!illy?
Windy?
Wear a heavy jacket
True
Wear a light jacket
True
Wear a windbreaker
True
False
False
False
"ot?
Wear no jacket
True
False

Vous aimerez peut-être aussi