KEMBAR78
Decision Condition Loop | PDF | Control Flow | Computer Programming
0% found this document useful (0 votes)
86 views16 pages

Decision Condition Loop

The document discusses various types of decision, condition, and loop statements in programming languages including: - IF statements with logical and relational operators to evaluate conditions - Nested IF/ELSEIF statements - DO loops with pre-test and post-test conditions to repeat blocks of code - FOR/NEXT loops using a counter variable to repeat code a specified number of times Examples are provided of using these statements to calculate areas and discounts, evaluate roots of quadratic equations, and calculate future values in loops.

Uploaded by

Aasrith Kandula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views16 pages

Decision Condition Loop

The document discusses various types of decision, condition, and loop statements in programming languages including: - IF statements with logical and relational operators to evaluate conditions - Nested IF/ELSEIF statements - DO loops with pre-test and post-test conditions to repeat blocks of code - FOR/NEXT loops using a counter variable to repeat code a specified number of times Examples are provided of using these statements to calculate areas and discounts, evaluate roots of quadratic equations, and calculate future values in loops.

Uploaded by

Aasrith Kandula
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Decision, Condition

and
Loop Statements

2
1 of 16
Relational Operators and Logical Expressions
Equal: =
Not equal: <>
Less than: <
Less than or equal to: <=
Greater than: >
Greater than or equal to: >=

To compare numeric quantities or strings, thus


forming logical expressions that are either true or
false
The operands within a logical expression must be of
the same type
3

String Comparisons
String expressions involving operators <. <=, >
and >= refer to alphabetical ordering; i.e., these
operators are interpreted as comes before or
comes after rather than less than or greater
than.

Carried out on a character-by-character basis, from


left to right.
Uppercase chars. Precede lowercase chars, and
blank spaces precede nonblank chars.

4
2 of 16
Logical Operators
And, Or, Not,
And: condition is true if both expressions are true
Or: condition is true if either expression is true, or if they are
both true
Not: reverses the value of a logical expression
Xor (exclusive Or)
condition is true only if one of the expression is true and the
other is false
Eqv (equivalent)
condition is true if both expressions have the same logical
value (both true or both false)
Imp (implies)
Will always result in a true condition unless the first
expression is true and the second is false.
5

Conditions

Comparing Compound conditions


numeric variables & If a1 <= a2 or a3 > a4
constants and a5 < a6 then
strings
text property of text And has precedence
boxes over Or
comparing text in VB
is case sensitive
All comparison/
to make it case
insensitive, add the relational operators
statement Option have precedence over
Compare Text in all logical operators
Gen-declaration
section
Use parentheses to
uppercase and alter the order of
lowercase characters evaluation
6
3 of 16
IF Statement Structure (1)

If logical expression then executable expression

True
Sleepy?

Form:
If condition Then
False Go to bed

statement(s)
..
End If

If Val(txtMonthlyInvestment) <= 0 Then


MsgBox "Enter a positive number."
End If
7

IF Statement Structure (2)

Form:
If condition Then
t-statement(s)
Else False Is SAT > True
f-statement(s)
End If 600?

Do Not
Admit
Admit

If optFutureValue Then
CalculateFutureValue
Else
CalculateMonthlyInvestment
End If

8
4 of 16
example
pi = 3.141593
If (form = circle) THEN circle
area = pi * radius ^2
circumference = 2* pi* r
Else rectangle
area = length * width
circumference = 2 * (length + width)
End If

IF Statement Structure (3)

Form:
If condition-1 Then
If condition-2 Then
False Is SAT > True
t-statement-2
600?
Else
f-statement-2
End If Do Not False Is GPA > True
Else 3.75?
Admit
f-statement-1
End If Consider Admit

If condition Then
statements
[ElseIf condition-n Then
statements] ...
[Else
statements]
End If
10
5 of 16
example (roots of a quadratic equation)
Dim a,b,c,d,x,x1,x2,real,imag variant data types
d = (b^2 4*a*c)
If d>0 Then
x1 = (-b + Sqr(d)) / (2 * a)
x2 = (-b - Sqr(d)) / (2 * a)
Elseif d = 0 then
x = -b / (2 * a)
Else
real = -b / (2 * a)
imag = Sqr(-d) / (2 * a)
End If

11

An If statement with ElseIf clauses


If iQuantity = 1 Or iQuantity = 2 Then
fDiscount = 0
ElseIf iQuantity >= 3 And iQuantity <= 9
fDiscount = .1
ElseIf iQuantity >= 10 Or iQuantity <= 24
fDiscount = .2
ElseIf iQuantity >= 25
fDiscount = .3
Else
MsgBox "Invalid quantity."
End If

12
6 of 16
Nested If statements
If Val(txtInterestRate) > 0 _
And Val(txtYears) > 0 Then
If optFutureValue _
And Val(txtMonthlyInvestment) > 0 Then
CalculateFutureValue
ElseIf optMonthlyInvestment _
And Val(txtFutureValue) > 0 Then
CalculateMonthlyInvestment
Else
MsgBox "Invalid data.
End If
Else
MsgBox "Invalid data."
End If

13

The syntax for the Do statement with


the test first
Do [{While|Until} condition]
.
statements

Loop

The syntax for the Do statement with


the test last
Do

statements

Loop [{While|Until} condition] 14
7 of 16
Do Loops (1)
Allow repeated execution Do While/Until
(pretest)
of instructions

Terminate on a condition
one specifies True
Loop Statements in
condition loop

Can test for condition at


Top of loop (pre-test) False

Bottom of loop (post-test)


Loop
Complete

15

Do Loops (2)
Loops can continue while a Do . While/Until (posttest)
condition is either true or false

Go while True:
While condition Statements in loop

Go while False:
Until condition

Loop condition
Be sure to change the loop True
variable within the loop or it will
loop infinitely!
False

16
8 of 16
examples

17

A Do loop with the test first that


calculates future value
Dim iIndex As Integer
Dim iMonths As Integer
Dim cFutureValue As Currency
iMonths = txtYears * 12
iIndex = 1
Do Until iIndex > iMonths
cFutureValue = (cFutureValue + _
txtMonthlyInvestment) * _
(1 + txtInterestRate / 12)
iIndex = iIndex + 1
Loop
18
9 of 16
A Do loop with the test last that
calculates future value
Dim iIndex As Integer
Dim iMonths As Integer
Dim cFutureValue As Currency
iMonths = txtYears * 12
iIndex = 1
Do
cFutureValue = (cFutureValue + _
txtMonthlyInvestment) * _
(1 + txtInterestRate / 12)
iIndex = iIndex + 1
Loop While iIndex <= iMonths
19

For/Next Loops
For/Next allows you to repeat For Loop
(pretest)
series of instructions using a
counter (a loop index)
Initialize
Index
Variable
For loopindex = initialvalue To endvalue [Step k]
body of loop
Next [loopindex]
False Statements in
Limit
Loop Body
Reached?

NB: the value of loopindex will be endvalue


during the last pass through the loop Tru
e
Increment
Loop Index

20
10 of 16
example

21

For/Next Loops

You can loop backwards by using a negative


increment

Loop testing is done before executing body

Do not change the loop control inside the body

Halt infinite loops by pressing Ctrl+Break

Exit For allows early For loop exit from


within the body
22
11 of 16
For/Next Loops
Use For/Next loop when you know the number of
times a loop will run

When loop decrements, the test is for strictly less


than the ending value

The For/Next ending condition may exist before


entering the loop. If so, then the loop body is
skipped

23

For Each/Next Statements


For Each loops let you reference all elements w/o
knowing how many there are
For Each element In group
statements
Next [element]
Form:
For Each variant-var In arrayname
statement(s)
Next

Also a convenient way to set all controls on a form


, 0, or some other value

24
12 of 16
A ForNext statement that puts the
numbers 1 through 10 in an array
Dim iIndex As Integer, _
iArray (1 To 10) As Integer
For iIndex = 1 To 10
iArray (iIndex) = iIndex
Next iIndex

A ForNext statement that puts even


numbers in 5 of the items in an array
Dim iIndex As Integer, _
iArray (1 To 10) As Integer
For iIndex = 2 To 10 Step 2
iArray (iIndex) = iIndex
Next iIndex 25

A ForNext statement that


calculates a future value
Dim iIndex As Integer
Dim iMonths As Integer
Dim cFutureValue As Currency
iMonths = txtYears * 12
For iIndex = 1 To iMonths
cFutureValue = (cFutureValue + _
txtMonthlyInvestment) * _
(1 + txtInterestRate / 12)
Next iIndex
26
13 of 16
The syntax of the With statement
With object
statements
End With

Three lines of code without a With


statement
txtMonthlyInvestment.Enabled = True
txtMonthlyInvestment.BackColor = vbWhite
txtMonthlyInvestment.SetFocus

The same code with a With statement


With txtMonthlyInvestment
.Enabled = True
.BackColor = vbWhite
.SetFocus
End With 27

Case structure
Select Case testexpression
[Case expressionlist
statements] ...
[Case Else
statements]
End Select

Case is another selection structure similar to


Ifthenelse if structure

Use case when a variable or expression can


be one of several values and if statement
would be cumbersome

A Case statement could be coded as a series


of nested If statements.
28
14 of 16
Case (continued)

Elements used for constant list are like these:


Case 2, 5, 9
Case 25 to 50
Case is < 14

Case expression can be text as well as


numeric

Case Else is a default case activated when


none of the other cases apply

29

A Select Case statement that tests a


variable named iIndex
Select Case iIndex
Case 0
frmVendors.Show
Case 1
frmInvoices.Show
Case 2
frmPayments.Show
Case 3
frmCredits.Show
Case Else
MsgBox "Invalid index number."
End Select
30
15 of 16
A Select Case statement that sets
discount percentages
Select Case iQuantity
Case 1, 2
fDiscount = .0
Case 3 To 9
fDiscount = .1
Case 10 To 24
fDiscount = .2
Case Is >= 25
fDiscount = .3
Case Else
MsgBox "Invalid Quantity"
End Select

How to code the expression list


Syntax Meaning
To Specifies a range of values.
Is Precedes a conditional expression. 31

Validation

Checking a data type:


IsNumeric
checks & returns true or false
If IsNumeric(txtQty.Text) Then
lblDue.Caption = curPrice + Val(txtQty)
IsDate
returns true or false depending on whether or not a
value is a date
If IsDate(txtData) Then
VarType
return a number that corresponds to the data type
stored in a variant.
If VarType(varName) = 0 Then...
Validating value ranges
If Val(txtHours.Text) > 10 And _
Val(txtHours.Text) <= 80 Then ...
32
16 of 16

You might also like