2. Objectives
Understand how relational operators are
used to create a condition
Understand and use IF
statements, including nested IF statements
Understand and use compound conditions
Develop and evaluate multiple solutions to
the same problem
Successfully write program solutions that
require decision making
2
3. Selection Statements
The most common decision structure is the IF statement.
A condition is a boolean expression that evaluates to either
true or false.
Comparison operators require numeric operands and
produce a logical result.
Conditions typically involve one of six relational operators.
Greater than >
Less than <
Greater than or equal >=
Less than or equal <=
Equal =
Not equal <>
3
7. VB.net code Example
Private Sub btnCheck_Click()
Dim Input, value1, value2 As Integer
If Input = value1 Then ‘ Condition 1 is True
process A ‘so only this code runs
ElseIf Input = value2 Then ‘ Condition 2 is True
process B ‘different response
Else ‘ Neither condition is True
process C ‘can be error message
End If
End Sub
Task 1: Create programs to perform the functions shown
in the flow chart examples.
Task 2: Create a version(2) of the Correct Change
program (from Input/Output) using selection statements.
7
8. Nested IF Statements (design)
The term nested IF refers to
an IF statement contained
within the true or false
branch of another IF
statement.
8
10. Compound Conditions
A compound condition consists of two
conditions within parentheses joined by a
logical operator.
Logical Operators require logical operands
and produce a logical result.
NOT Logical opposite
AND Both values are true
OR At least one value is true
XOR Exactly one value is true
10
12. Operator Precedence
When several operations occur in an
expression, each part is evaluated and
resolved in a predetermined order called
operator precedence.
1st : Evaluate all arithmetic/concatenation
operators
2nd : Evaluate all comparison operators
3rd : Evaluate all logical operators
12
14. Summary
A condition is an expression that evaluates
to either true or false.
IF statements use conditions to choose
between actions.
The true and false branches of an IF
statement may contain any valid
statement, including other IF statements.
A compound condition is two or more
conditions joined by a logical operator.
14