Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
1Activity
0 of .
Results for:
No results containing your search query
P. 1
LESSON 3 Branching and Looping

LESSON 3 Branching and Looping

Ratings: (0)|Views: 4 |Likes:
Published by lalitha

More info:

Published by: lalitha on Dec 06, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF or read online from Scribd
See more
See less

12/07/2010

 
B
RANCHING
 
AND
OOPING
3
Visual Basic includes a number of features that allow us to select among alternative pathways,or to repeat the execution of a particular block of statements. For example, we can choose toexecute one of several different blocks of statements, depending on the value of anexpression. This process is known as
selection
. Or, we can choose one of two different paths,depending on the outcome of a logical test (i.e., depending whether a
logical expression
is
true
or
false
). This process is known as
branching
.Many programs require that a group of instructions be executed repeatedly, until someparticular condition has been satisfied. This process is known as
looping
. Sometimes thenumber of passes through the loop will be known in advance (
unconditional
looping). In othersituations the looping action will continue indefinitely, until some logical condition has beensatisfied (
conditional
looping).In order to carry out branching operations in Visual Basic, we must be able to expressconditions of equality and inequality. To do so, we make use of the following
relationaloperators
(also called
comparison operators
):
NameVisual Basic Comparison Operator Sample Expression Meanin
Equal
=m = cm is equal to c
Not Equal
<>m <> cm is not equal toc
Greater than
>m > cm is greater thanc
Less than
<m < cm is less than c
Greater than or equal to
>=m >= cm is greater thanor equal to c
Less than or equal to
<=m <= cm is less than orequal to c
 These operators are used to compare numeric quantities (i.e., constants, numeric variables ornumeric expressions) or strings, thus forming
logical expressions
that are either
true
or
false
. The
operands
within a logical expression must be of the same type; i.e., both must be numericor both must be strings.In addition to the relational operators, Visual Basic contains several
logical operators
. They are
And
,
Or
,
Xor
 
(exclusive
Or
),
Not
,
Eqv
 
(equivalent) and
Imp
 
(implies). The first three operators(
And
,
Or
 
and
Xor
) are used to combine logical expressions, thus forming more complex logicalexpressions.
And
 
will result in a condition that is true if 
both
expressions are true.
Or
 
willresult in a condition that is true if 
either 
expression is true, or if they are both true;
Xor
,however, will result in a condition that is true
only 
if one of the expressions is true and theother is false.
Not
 
is used to reverse (
negate
) the value of a logical expression (e.g., from trueto false, or false to true).
Eqv
 
will result in a condition that is true if both expressions have the
same
logical value (either both true or both false); and
Imp
 
will always result in a truecondition unless the first expression is true and the second is false.
Information and Communication Technology DepartmentPalompon Institute of Technology
23
Relational Operators and Logical Expressions
 
B
RANCHING
 
AND
OOPING
3
Shown below are several logical expressions that make use of logical operators.
X = 27 And Student = “Smith”Not(Student = “Smith”) Or (Account = “CURRENT”)(Student = “Smith”) Eqv (Account = “CURRENT”)(Student = “Smith” Imp (Account = “CURRENT”)
Truth Table for operator 
And
.
Expression1 Expression2 Expression1 And Expression2 Resul
FalseFalseFalse And FalseFalseFalseTrueFalse And TrueFalseTrueFalseTrue And FalseFalseTrueTrueTrue And TrueTrue
Using the
And
 
operator, the code will execute if both conditions are
True
, such as this example:
If (UserName = “PIT”) And (Password = “ICT”) ThenPrint “Log-In Successful.”ElsePrint “Access Denied.”End If
Truth Table for operator 
Or
.
Expression1 Expression2 Expression1 Or Expression2 Resul
FalseFalseFalse Or FalseFalseFalseTrueFalse Or TrueTrueTrueFalseTrue Or FalseTrueTrueTrueTrue Or TrueTrue
Using the Or operator, the code will execute if either conditions are
True
, for example:
If (UserName = “PIT”) Or (UserName = “PITICT”) ThenPrint “Log-In Successful.”ElsePrint “Access Denied.”End If
Information and Communication Technology DepartmentPalompon Institute of Technology
24
 
B
RANCHING
 
AND
OOPING
3
BRANCHING WITH THE
If-Then
 
BLOCK
 
An
If-Then
block is used to execute a single statement or a block of statements on a conditional basis.The general form of an
If-Then
block is:
If
logical expression
Then. . . . . . . . . .
executable statements
. . . . . . . . . .End If
 The block of statements included between
If-Then
 
and
End
 
If
 
will be executed if the logicalexpression is
true
. Otherwise, this block of statements will be bypassed, and the statementfollowing
End If
 
will be executed next.
The following
If-Then
block permits a single group of statements to be executed conditionally.
If income <= 14000 Thentax = 0.2 * paynet = pay – taxEnd If
The assignment statements will be executed only if the logical expression income <= 14000 is true.
BRANCHING WITH THE
If-Then-Else
 
BLOCK
 
An
If-Then-Else
block permits one of two different groups of executable statements to be executed,depending on the outcome of a logical test. Thus, it permits a broader form of branching than is availablewith a single
If-Then
block.In general terms, an
If-Then-Else
block is written as
If
logical expression
Then. . . . . . . . . .e
xecutable statements
. . . . . . . . . .Else. . . . . . . . . .e
xecutable statements
. . . . . . . . . .End If
If the
logical expression
is true, the first group of executable statements will be executed. Otherwise, thesecond group of executable statements will be executed. Thus, one group of executable statements willalways be executed. Note that
If-Then, Else
and
End If
are separate statements that are usedtogether to create a complete
If-Then-Else
block.
Information and Communication Technology DepartmentPalompon Institute of Technology
25
Conditional Statements

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->