If-Then-Else Statements
Simple selection occurs when a choice is made between two alternative paths, depending on the result of a condition being true or false. If-Then-Else Statements are one way of implementing selection.
The general form of the If-Then-Else statement is:
If condition is true Then
code for true alternative
Else
code for false alternative
End If
After the If-Then-Else statement processing continues with the next statement.
For example a bank charges $5 service fee if an account balance is less than $500 and $2 otherwise. The code would be:
If AccountBalance < 500 Then
Charge = 5
Else
Charge = 2
End If
If the condition was true, say the account balance was $300 (ie less than 500), the path taken by the code would be:
If AccountBalance < 500 Then
Charge = 5
Else
Charge = 2
End If
The else statements would be skipped.
If the condition was false, say the account balance was $800 (ie not less than 500), the path taken by the code would be:
If AccountBalance < 500 Then
Charge = 5
Else
Charge = 2
End If
The if statements would be skipped.
The condition (AccountBalance < 500 in the above example) can use one of the following:
Equal to / =Less then
Greater than
Less than or equal to / <=
Greater than or equal to: / >=
Not equal to
Some examples include:
Comparison / Some Valid ValuesAge < 5 / 0 1 2 3 4
Age <= 5 / 0 1 2 3 4 5
Age > 5 / All ages except 5
If then..else if
If this condition is true Then
code for true condition
Elseif this condition is true Then
code for true condition
Elseif this condition is true Then
code for true condition
Elseif this condition is true Then
code for true condition
Elseif this condition is true Then
code for true condition
Elseif this condition is true Then
code for true condition
Else
code for false condition (all of the above are false)
End If
Loops
All loops have two parts:
1. the body of the loop (the statements being repeated)
2. a termination condition that stops the loop
If the termination condition does not work the loop will continue for ever.
For Loops
The format of the for loop is:
For variable = start value To end value Step change value
statements that compose body of loop
Next variable
where
variable / counter variable in the loopstart value / beginning value of the counter variable
end value / ending value of the counter variable
change value / amount the counter variable changes each time through the loop
Note that if the change value is omitted it defaults to 1.
Next variable / the end of the For loop
The name of the variable can be omitted.
An example loop is
Do-while and Do-Until Loops
With a For loop the number of times it is to be executed is known. When this is not known a Do While and Do Until loop should be used.
Do Until and Do While loops are done until some condition becomes true (for Do Until ) or false (for Do While). If the condition does not become true or false as required loop will not terminate
Pre-test loops - test at start
Do Until (or While) condition
body of loop
Loop
Post-test loops - test at end
Do
body of loop
Loop Until (or While) condition
The following diagram shows the logic used in these loops:
Notes prepared by Jodi Tutty