Making Decisions

    VB.NET



                   1
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
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
The IF Statement Conditions




                              4
Simple IF Statements (design)


Decision symbol

                     Process symbol




                                      5
Solving the Overtime Problem




                               6
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
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
True OR False Conditions




                           9
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
Logical Operators




                    11
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
The Select…..Case Statement
When more choices are available use the
Select – Case statement, an example is
shown below.




                                          13
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

Decisions

  • 1.
  • 2.
    Objectives Understand how relationaloperators 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 mostcommon 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
  • 4.
    The IF StatementConditions 4
  • 5.
    Simple IF Statements(design) Decision symbol Process symbol 5
  • 6.
  • 7.
    VB.net code Example PrivateSub 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
  • 9.
    True OR FalseConditions 9
  • 10.
    Compound Conditions A compoundcondition 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
  • 11.
  • 12.
    Operator Precedence When severaloperations 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
  • 13.
    The Select…..Case Statement Whenmore choices are available use the Select – Case statement, an example is shown below. 13
  • 14.
    Summary A condition isan 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