
Decision making
If… Else… EndIf statement
If [Statement] then
[Do this Statement if it is true]
else
[Do t Statement if it is false]
endif
E.g.
If UserChoice = “Y” then
@ 5,5 say “You have chosen Yes”
else
@ 5,5 say “You have chosen No”
endif
E.g. 2 using "not" operator
If not UserChoice = “N” then
@ 5,5 say “You have chosen Yes”
else
@ 5,5 say “You have Chosen No”
Endif
E.g. 3 using “or” operator
If Age > 0 or Age < 80 then
@ 5,5 say “You age is “ + age
else
@ 5,5 say “Re-enter your age!”
Endif
In an example 3, If .and. operator is used instead, It will not work. Because it is impossible for Age tobe less than 0 AND more than 80 at the same time. It is therefore vital to understand conditional operators to use them effectively.
- AND –All statements have to be true, then statement is true
- OR –One of statements is true, then statement is true
- NOT –Whatever the statement is, the result is the opposite
*’In
Mr. Brettell
worksheets, it uses .and. , .or. , .not. with parentheses. They are not necessary and by removing them may help to make it more readable. However, you may want to use it if you want to specify how the statements should be evaluated for more complex statements.
More Logical way of doing it when you have different levels of logical order
Do case
case [Statement]
[Do statement if it is true and exit case, else endcase]
case [statement]
[Do statement if it is true and exit case, else endcase]
case [Statement]
[Do Statement if it is true and exit case, else endcase]
endcase
E.g.
TestPercent = 0
Do case
case TestPercent >= 90
@ 5,5 say “You got “ + LTrim(str(TestPercent)) + “%. Excellent effort!!!”
case TestPercent >= 75
@ 5,5 say “You got “ + LTrim(str(TestPercent)) + “%. Well done!”
case TestPercent >= 60
@ 5,5 say “You got “ + LTrim(str(TestPercent)) + “%. Very good!”
case TestPercent >= 45
@ 5,5 say “You got “ + LTrim(str(TestPercent)) + “%. Try harder next time”
case TestPercent >= 30
@ 5,5 say “You got “ + LTrim(str(TestPercent)) + “%. You fail!”
case TestPercent >= 0
@ 5,5 say “You got less than 30%! Did you revise for the test?”
endcase
Click here to download an example of using If Statement and Case statement.
Uses to keep something going on until the statement is true
Do while loop
Do while [statement]
[If statement above is true then, continue or exit the loop]
[Statements]
Enddo
E.g.
Do while Number <= 10
If Number is less than 10, then
? Number Print the Number
Number = Number + 1 Add 1 to the number
Enddo Back to first statement
Common problem: when using @ command. Most people noticed that it always print in the same place because you may give only a specific point like @ 2,2 then it always print at the same place. To solve the problem, below is an example:
YPosition = 5
Number = 0
Do While Number <= 10
@ YPosition,5 say Number Using Extra Variable to keep changing the location
Number = Number + 1
YPosition = YPosition + 1
Enddo