Switch Statement

42 views

The C Switch case statements are a substitute for long if statements that compare a variable to several values. These values can be int ,char or any other types which allows multiple choice of a selection of items at one level of a conditional where it is a far neater way of writing multiple if statements:

Syntax :

switch (expression)

{
case item1:
       statement1;
break;
case item2:
       statement2;
break;

case itemn:
      statementn;
break;
default:
      statement;
break;

}

The expression of a switch statement is a value. The case says that if above expression has the value  whatever is after that case then do whatever follows the statements. The break is used to break out of the case statements. Break is a keyword that breaks out of the code block, usually surrounded by braces, which it is in.

Example :

switch (Days)
{
case `sunday’:

{

statements;

break;

}
     case `monday’:

{

statements;

break;

}

case `tuesday’:

{

statements;

break;

}

     case `wednesday’:

{

statements;

break;

}

     case `thursday’:

{

statements;

break;

}

case `friday’: 

{

statements;

break;

}     
 
default:

  statement;
       break;
}



« Css Introduction Css Syntax »
Posted on Thursday, June 12th, 2008 at 5:57 pm under C For Beginners | RSS 2.0 Feed

Post Comment

You must be logged in to post a comment.



ComputerEducationWorld.com All Rights Reserved © RSS | CBSE | Education Boards Of India | What is My IP?