CSC430 - Chapter 3: Control Structures (Selection)
Chapter 3: Control Structures (Selection)
Introduction
This chapter covers control structures, specifically focusing on selection.
Topics include Boolean expressions,
ifstatements,if...elsestatements,if...elseif...elsestatements,switchstatements, and nestedifstatements.
Learning Outcome
By the end of this class, students should be able to:
Interpret the concept of relational and logical operators.
Develop a program using
ifstatements.Differentiate between
ifandswitchstatements.Produce programs using selection control.
Boolean Expression
A Boolean expression is a sequence of operands and operators that combine to produce one of the Boolean values,
trueorfalse.Two types of Boolean expressions:
Simple Boolean Expression (SBE)
Compound Boolean Expression (CBE)
Selection Criteria with Boolean Expression
Simple Boolean Expression (SBE):
Form:
expression1relational-operatorexpression2Relational operators allow comparisons.
Require two operands.
Return 1 if the expression is true, 0 otherwise.
Relational/Comparison Operators (SBE)
Supported by VBA (Visual Basic for Applications).
Assume variable A holds 10 and variable B holds 20.
A = B(Equal To): Returns False (0)A <> B(Not Equal To): Returns True (1)A > B(Greater Than): Returns False (0)A < B(Less Than): Returns True (1)A >= B(Greater Than or Equal To): Returns False (0)A <= B(Less Than or Equal To): Returns True (1)
Logical Operator (CBE)
Supported by VBA.
Assume variable A holds 10 and variable B holds 0.
And: Returns True only if both expressions are true.Or: Returns True if either expression is true.Not: Reverses the logical state of its operand.
The If Statement (One-Way Selection)
Syntax:
If (condition) Then statement 1 ... statement n End IfExample:
Private Sub yourbuttonname_Click() Dim x As Integer Dim y As Integer x = 234 y = 32 If x > y Then MsgBox "X is Greater than Y" End If End Sub
The If…Else Statement (Two-Way Selection)
Syntax:
If (condition) Then statement 1 ... statement n Else statement 1 ... statement n End IfExample:
Private Sub yourbuttonname_Click() Dim x As Integer Dim y As Integer x = 234 y = 324 If x > y Then MsgBox "X is Greater than Y" Else Msgbox "Y is Greater than X" End If End Sub
The If…Elseif, Else Statement (Multi-Way Selection)
Syntax:
If (condition) Then statement n ElseIf (condition) Then statement n Else statement n End IfExample:
Private Sub yourbuttonname_Click() Dim x, y As Integer x = 234 y = 234 If x > y Then MsgBox "X is Greater than Y" ElseIf y > x Then Msgbox "Y is Greater than X" Else MsgBox "X and Y are EQUAL" End If End Sub
Exercise Example
Illustrates using
Ifstatements to assign grades based on scores.Score >= 80: Grade A, Display "Excellent"
Score >= 70: Grade B, Display "Good"
Score >= 50: Grade C, Display "Fair"
Score < 50: Grade F, Display "Fail"
VBA Code Snippet Example:
Private Sub Worksheet_SelectionChange(ByVal Target As Range) End Sub
Exercise: Hungry and Foods Logic
Illustrates using nested
IFstatementsSTART INPUT hungry, foods IF hungry is TRUE AND foods is TRUE Then DISPLAY EATS ELSEIF hungry is TRUE AND foods is FALSE Then DISPLAY FIND FOODS ELSEIF hungry is FALSE AND foods is TRUE then DISPLAY GIVE TO FRIEND ELSEIF hungry is FALSE AND foods is FALSE then DISPLAY SLEEP END IF ENDImplementation uses ActiveX Control with a command button (
cmdHungry) and input/output viaMsgBox.
Switch Statement
Syntax:
Select Case (var to test) Case Value statement n Case Value statement n Case Else statement n End SelectExample:
Private Sub switch_demo_Click() Dim MyVar As Integer MyVar = 1 Select Case MyVar Case 1 MsgBox "Number 1" Case 2 MsgBox "Number 2" Case 3 MsgBox "Number 3" Case Else MsgBox "Unknown Number" End Select End Sub
Exercise: ActiveX Control with Option Buttons
Involves 4 Option Buttons (
optG,optH,optA,optNone) and 1 Command Button (cmdDisplayApartment).range("B11")displays the value chosen from the option buttons.The command button displays the full apartment name and rate in
MsgBoxbased onrange("B11").
Nested IF
An
ifstatement inside anotherifstatement.Syntax:
If (condition) Then 'outer IF If (condition) Then 'inner IF statement n ElseIf (condition) Then statement n Else statement n End If 'END for inner IF Else 'when hungry is FALSE If (condition) Then 'inner IF statement n ElseIf (condition) Then statement n Else statement n End If 'END for inner IF End If 'END for outer IF
Exercise: Hungry and Foods Logic (Nested IF)
Illustrates using nested
IFstatements based onhungryandfoodsinputs.START INPUT hungry, foods IF hungry is TRUE then 'outer IF IF foods is TRUE then 'inner IF DISPLAY EATS ELSE DISPLAY FIND FOODS END IF 'END for inner IF ELSE 'when hungry is FALSE IF foods is TRUE then 'inner IF DISPLAY GIVE TO FRIEND ELSE DISPLAY SLEEP END IF 'END for inner IF END IF 'END for outer IF ENDImplementation uses ActiveX Control with 2 Check Boxes (
chkHungry,chkFoods).range("B8")displays the action based on the selection ofchkHungryandchkFoods.