Selection Statements Flashcards
Algorithm is a procedure for solving a problem in terms of:
- the actions to be executed
- the order in which these actions are to be executed
It is specifying the order in which statements are to be executed in a computer program
Program Control
It is an artificial and informal language that helps you develop
algorithms
Pseudocode
Not executed on computers – rather, helps us “think out” a program before starting to write the code in a programming language like C
Pseudocode
It is a graphical representation of an algorithm or of a portion of an algorithm
Flowcharts
Types of Control Structures
- Sequence
- Selection
- Iteration
- Function Call
Statements are executed in a specified order
Sequence
No statement is skipped and no statement is executed more than once.
Sequence
It selects a statement to execute on the basis
of condition
Selection
Examples of Selection
- if statement
- switch statement
- conditional operator
When statements are executed more than one time
Looping/Iteration
Examples of Looping/Iteration
- while loop
- for loop
- do-while loop
Allows a program to choose
between two alternatives by
testing the value of an expression
If Statement
Consists of a test expression
followed by one or more
statements
If Statement
Difference between if and if-else selection
- if statement performs when the condition is true
- if-else allows to specify different actions whether the condition is true or false
Syntax of single statement if clause
Great
Syntax of multi statement if clause
Good job
Syntax of if-else if else statements
Wow!
Syntax of if-else statements
Cool!
An alternative to if-else if-else statements
Switch statements
Expression in the switch must result
in
integer value
Syntax of switch statements
Rules of switch statement regarding break
break statement must be in the end in each case.
Rules of switch statement regarding case labels
Duplicate case labels or in the constant aren’t allowed
Rules of switch statement regarding order
Order of the cases does not matter