Facebook Twitter Google RSS

Sunday, 10 August 2014

SWITCH CASE Structure

Tapan     01:30  No comments

When decision from a number of choices is required, at that time switch-case default is used.


A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.

Syntax for the switch - case structure is as follows :

Switch ( integer expression)
{
case value1 : action1 ;
case value2 : action2 ;
default : action3 ;
}

First the expression is evaluated.
After getting the value it is matched one by one with constant values in case statements.
When a match is found, it executes the statements following that case and all subsequent case and default statements as well.

Switch Case Structure


Execution of the Switch:

  1. Integer expression is any constant or expression that evaluates to an integer.
  2. The keyword case is followed by an integer or a character constant.
  3. Float is not allowed as case value.
  4. Each constant in each case must be different from all others.


Notes while using the Switch:

  1. Order is not important while putting the case values.
  2. No enclosure is needed in case statements.
  3. break statement must be used to prevent the execution of subsequent cases.
  4. Use of continue will not take the control to beginning of switch.

Switch & if-else:

Switch is a better way of writing programs.
In certain situations switch is not useful.
Example : i <= 20
More structured program in switch

, , , ,

0 comments :

Popular Post

Website Worth

Contact Us

Name

Email *

Message *

© 2014 BCA Developer. Powered by E Friends.