Lesson 1
Home Up Lesson 1 Lesson 2 Lesson 3 Lesson 4 Lesson 5 Assignment 6

ASCII table

Assignment 1

Decisions

Binary Decisions

The computer is very good at making comparisons between values. However it can only make true of false conclusions. This type of comparison is called binary comparison. As a programmer, you must come up with the correct comparison to make the computer act like it is thinking. The comparisons you make must involve two values that are to be compared, and a symbol called, a conditional operator that determines the rule of the comparison. Some comparisons can be combined with others by Logical Operators that define the rules on how the comparison is to be related.

Conditional Operators

A condition must involve a value on each side of a conditional operator. The conditional statement will result in either a true or a false result.

Conditional Operator Explanation
= Equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
<> Not equal to

Examples:

Conditional  Expression Boolean Result
6 = 7 False
6 < 7 True
6 > 7 False
6 <= 7 True
6 >= Error! there must be a value on each side of the operator!
6 <> 7 True

Logical Operators

Many times you are going to need an outcome based on several conditions. It will be necessary to combine two or more simple conditions together. This is done with the following Logical Operators.

Logical Operator Name of Operator Explanation
OR Or operator If either condition is true the whole expression is true
XOR Exclusive Or operator If only one condition is true the whole expression is true
AND And operator If both conditions are true the whole expression is true
NOT Invertor operator Inverts the condition following it.

For OR, XOR and AND operators, make sure there are complete conditions to the left and right of these symbols. For the NOT operator, make sure a complete condition is to the right of these symbols. If you need some conditions evaluated before others, use parenthesis to advance the order of operations.

Conditional  Expression Boolean Result
6 = 7 OR 5 = 5 True, because one condition is true.
6 > 4 XOR 3 < 8 False, because both conditions are true
6 = 7 XOR 6 = 6 True, because only one condition is true
6 < 7 AND 6 = 7 False, because one condition is false
4 < 5 AND 3 > -2 True, because both contitions are true
6 > 7 OR 7 < 6 False, because both conditions are false
NOT 6 <= 7 False, because condition is true
6 >= 4 OR 7> Error!, because code to the right of OR is not a complete condition
6 <> 7 NOT Error!, condition must be to the right of NOT
(6 = 6 AND 7 > 9) XOR 7 = 7 True, expression in parenthesis is false, therefore XOR will evaluate false XOR true

If ... Then ... End If Structure

One structure that lets the computer act on a condition being true or false is the

If <condition> Then  'Write a comment that explains the purpose of this structure

    <statement/s> 'make sure to indent the statements of an If structure

End If

structure. The condition be a valid condition as explained in the previous section. If the condition evaluates to true, then the statement or statements will be executed. If the condition evaluates to false, then none of the statements will be executed. In either case the instructions that follow the End If statement are executed.

Example:

If bytAge < 18 Then    'Display the message that this person is old enough to vote

    lblMajority.Caption = "This person is able to vote"

End If

If ... Then ... Else...End If Structure

The basic form of an If..Then..End If structure only allows for an action if the conditional expression evaluates to true. An extension of that structure is

If <condition> Then  'Write a comment that explains the purpose of this structure

    <statement/s> 'These statements are executed if the condition evaluates to true

Else

    <statement/s> 'These statements are executed if the condition evaluates to false

End If

This form now lets you perform some actions if the condition is true, and different actions if the condition is false.

Assignment