7_Objective-C Decision Making

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.

  • if
  • if…else
  • else if

  • switch statement
  • The ? : Operator: We have covered conditional operator ? : in previous chapter

Example of (If , if else if ,else if)

About Switch 

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.

Example of  switch statement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: