Selection Control Structures - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Selection Control Structures

Description:

In a Null Else, one or no statement is executed ... With AND, both conditions have to be true for the first statement to be executed ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 29
Provided by: ed9158
Category:

less

Transcript and Presenter's Notes

Title: Selection Control Structures


1
CHAPTER 4 Selection Control
Structures
2
OBJECTIVES
  • To introduce the 4 types of selection control
    structures
  • To introduce the case structure
  • To familiarize students with the usage of
    selection control structures

3
Types of IF statements
  • Simple If
  • Null Else
  • Combined If
  • Nested If

4
Simple IF
  • If . Then
  • Else
  • EndIf

5
Simple IF
  • If (condition is true) Then
  • execute statement 1
  • Else
  • execute statement 2
  • EndIf

6
Simple IF
  • In a Simple If, one of two statements is executed
  • One will always be executed
  • The second statement acts as a default statement

7
Null Else
  • If .. Then
  • EndIf

8
Null Else
  • If (condition is true) Then
  • execute statement
  • EndIf

9
Null Else
  • In a Null Else, one or no statement is executed
  • This structure allows the program block to NOT
    execute selectively

10
Combined IF
  • Uses the keywords AND and OR
  • Allows more complex selection conditions to be
    performed

11
Combined IF - AND
  • If AND . Then
  • Else
  • EndIf

12
Combined IF - AND
  • If (condition 1 is true) AND
  • (condition 2 is true) Then
  • execute statement 1
  • Else
  • execute statement 2
  • EndIf

13
Combined IF - AND
  • With AND, both conditions have to be true for the
    first statement to be executed
  • The default statement executes otherwise (or in
    the case of a Null Else, nothing happens)

14
Combined IF - OR
  • If . OR .. Then
  • Else
  • EndIf

15
Combined IF - OR
  • If (condition 1 is true) OR
  • (condition 2 is true) Then
  • execute statement 1
  • Else
  • execute statement 2
  • EndIf

16
Combined IF - OR
  • With OR, either condition has to be true for the
    first statement to be executed
  • Thus, the more conditions there are, the higher
    the chances for execution of the first statement

17
Nested IF
  • Two or more Simple If/Null Else structures used
    together
  • Enables multiple conditional execution of
    statements
  • 2 types linear and non-linear

18
Linear Nested IF
  • If Then
  • Else If Then
  • Else
  • EndIf
  • EndIf

19
Linear Nested IF
  • If (condition 1 is true) Then
  • execute statement 1
  • Else If (condition 2 is true) Then
  • execute statement 2
  • Else
  • execute statement 3
  • EndIf
  • EndIf

20
Non-Linear Nested IF
  • If Then
  • If Then
  • Else
  • EndIf
  • Else
  • EndIf

21
Non-Linear Nested IF
  • If (condition 1 is true) Then
  • If (condition 2 is true) Then
  • execute statement 1
  • Else
  • execute statement 2
  • EndIf
  • Else
  • execute statement 3
  • EndIf

22
Notes on Nested IF
  • Non-Linear Nested Ifs are messy and its usage is
    not encouraged
  • Usually can be rewritten using Combined IF
    structures
  • Likewise, Linear Nested Ifs can sometimes be
    rewritten using the Case structure

23
Non-Linear Nested IF
  • If (condition 1 is true) Then
  • If (condition 2 is true) Then
  • execute statement 1
  • EndIf
  • EndIf

24
Non-Linear Nested IFRewritten
  • If (condition 1 is true) AND
  • (condition 2 is true) Then
  • execute statement 1
  • EndIf

25
Case
  • Rewrites the Linear Nested If
  • Basically compares variables for the one to
    execute.
  • Usually used for menus
  • Does not use conditions

26
Case
  • Case Of single variable
  • value_1 execute statement 1
  • value_2 execute statement 2
  • value_n execute statement n
  • value_other execute default statement
  • Endcase

27
Case Example
  • Case Of char_stored
  • A Display A
  • B Display B
  • C Display C
  • other Display X
  • Endcase

28
Questions?
Write a Comment
User Comments (0)
About PowerShow.com