decisions
TRANSCRIPT
1
Selection Statements
VB.NET
222
Objectives
Understand how relational operators are used to create a conditionUnderstand and use IF statements, including nested IF statementsUnderstand and use compound conditionsDevelop and evaluate multiple solutions to the same problemSuccessfully write program solutions that require decision making
333
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 <>
444
The IF Statement Conditions
555
Simple IF Statements (design)
Decision symbol
Process symbol
666
Solving the Overtime Problem
777
VB.net code Example
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.
Private Sub btnCheck_Click()Dim Input, value1, value2 As IntegerIf Input = value1 Then ‘ Condition 1 is True
process A ‘so only this code runs
ElseIf Input = value2 Then ‘ Condition 2 is Trueprocess B ‘different response
Else ‘ Neither condition is True process C ‘can be error message
End IfEnd Sub
888
Nested IF Statements (design)
The term nested IF refers to an IF statement contained within the true or false branch of another IF statement.
999
True OR False Conditions
101010
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
111111
Logical Operators
121212
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
131313
The Select…..Case Statement
When more choices are available use the Select – Case statement, an example is shown below.
141414
SummaryA 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.