|
DecisionsBinary DecisionsThe 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 OperatorsA 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.
Examples:
Logical OperatorsMany 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.
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.
If ... Then ... End If StructureOne 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 StructureThe 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 |