Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program
Objective-C programming language assumes any
- non-zero and non-null values as true
- And if it is either zero or null, then it is assumed as false value.
Objective-C programming language provides following types of decision making statements.
- switch statement
- The ? : Operator: We have covered conditional operator ? : in previous chapter
The expression used in a switch statement must have
- An integral
- enumerated type
- class type in which the class has a single conversion function to an integral or enumerated type.
You can have any number of case statements within a switch.
Each case is followed by the value to be compared to and a colon.
The constant-expression for a case must be the same data type as the variable in the switch,
It must be a constant or a literal.
When the variable being switched on is equal to a case,
- The statements following that case will execute until a break statement is reached.
When a break statement is reached
- the switch terminates,
- And the flow of control jumps to the next line following the switch statement.
Note : every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.