Switch Statement

Computer Education, Training & Tutorial Resources - ComputerEducationWorld.com
Home » Free Educations » Free Programming Training » C Programming » C For Beginners »

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;
}


• • •
 



captcha PHP Script Free PHP captcha script free php
Website Design by WebWalas.com