Module 3: Control Structure - Selectors Flashcards
are used to control the flow of program execution by employing decision making, looping and branching, enabling your program to conditionally execute particular blocks of code
Control Structures
depends on the supplied data values and the conditional logic specified
Program Execution Order
Three Types of Control Statements
- Conditional/Selector Statement
- Iteration/Loop Statement
- Jump Statement
What are the conditional/selector statements?
- if statement
- if-else statement
- nested-if statement
- switch-case statement
What are the iteration/loop statements?
- for statement
- while statement
- do-while statement
What are the jump statements?
- break
- continue
decide which statement to execute and when
Selection/Decision Making Statement
Selection statements evaluate the _____ and control the program flow depending upon the result of the condition provided
Boolean expression
Three Types of Selection Statements
- Single Way Selection (if)
- Two Way Selection (if-else)
- Multiple Selection (nested if and switch-case)
- It is a simple decision-making statement
- It is used to decide whether the statement or block of statements should be executed or not
- Block of statements will be executed if the given condition is true otherwise the block of the statement will be skipped
Single Way Selection (If Statement)
Syntax for If Statement
Syntax: if (condition) { stmt1; stmt2; }
is an extension to the if-statement, which uses another block of code, i.e., else block
If-Else Statement
is executed if the condition of the if block is evaluated as false false
Else Block
Syntax for If-Else Statement
Syntax: if (condition) { stmt1; stmt2; } else { stmt3; stmt4; }
contains the if-statement followed by multiple else-if statements
Nested-If/If-Else-If Statement
It is the _____ that creates a decision tree where the program may enter in the block of code where the condition is true.
chain of if-else statements
The last else statement will be executed if all if condition is _____
False
Syntax for Nested-If/If-Else-If Statement
Syntax: if (condition1) { stmt1; stmt2; } else if (condition2){ stmt3; stmt4; } else if (condition3){ stmt5; stmt6; } else { stmt7; stmt8; }
are similar to if-else-if statements
Switch Statements
The _____ contains multiple blocks of code called _____ and a single _____ is executed based on the variable which is being switched.
switch statement; cases; case
The switch statement is easier to use instead of _____.
if-else-if statements
Syntax for Switch Statement
Syntax: switch () { case : stmt1; stmt2; break; case : stmt3; stmt4; break; case : stmt5; stmt6; break; default: stmt7; stmt8; } // end switch
set what variable needs to be tested based on its value
Switch (Variable)
value tested base on the value of the variable indicated in switch
Case (Value)
terminates the enclosing switch statement.
Break
are necessary because without them, statements in switch blocks fall through
Break Statements
this statement is performed if none of the case labels are true. It is similar to the last else of the nested-if statement.
Default
is used when dealing with conditions whose value to be tested has a single value only.
Switch Statement
Only ______ case body in the switch statement is executed
ONE
used to test a range of values
Nested If
used to test a constant value
Switch-case