Aug 15
  • The break is used to take the control out of the switch.
  • Switch is useful when we wish to check the value of variable against a particular set of values.
  • Compiler generates a jump table for a switch during compilation.
  • It is not necessary to use a break in every switch statement.
  • We can put the cases in any order.

Example

main()

int i;

switch(i)

{

case  7 :  ………….. ;

break;

case 4 : ………….. ;

break;

}

  • We can use char values in cases.

Example

main()

int i;

switch(i)

{

case ‘v’ :

case ‘a’ :

}

  • We can execute a common set of statements for multiple cases.

Example

main()

char ch;

printf(”Enter a,b or c”);

scanf(”%c”,&ch);

switch(ch)

{
case ‘a’:

case ‘A’:

printf(”America”);

break;

case ‘b’ :

case ‘B’:

printf(”border”);

break;

case ‘c’:

case ‘C’:

printf(”cat”);

break;

}

  • Even if there are multiple statements to be executed in each case,there is no need to enclose them within a pair of braces.
  • If a statement doesn’t belong to any case,the compiler won’t report an error. The statement would never get executed.
  • We can check the value of any expression in a switch.

Example

switch(i+j*k)

{

}

switch(23+45%4*k)

{

}

switch(a<4||b>7)

{

}

  • Float expression cannot be tested using switch.
  • Cases can never have variable expression.
Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Blogplay
  • LinkedIn
  • MySpace
  • Reddit
  • RSS
  • Twitter

written by Shweta

Jul 29

When actions to be taken depending upon the value of the control variable,are large in no. ,then the use of control structure (nested if else) makes the problem complex. There switch statement can be used.

Syntax:

switch(expression)

{

case expresssion 1 :

block of instructions 1;

break;

case expresssion 2 :

block of instructions 2;

break;

.

.

.

.


case expresssion n :

block of instructions n;

break;

default: default block of instructions;

}

Switch evaluates expression and check if it equal to expression 1. If it is,it executes block of instructions 1 until it find the break keyword,moment it’l find the break ,the control will go to the end of switch.

If expression is not equal to expression 1,it’ll check the expression 2 and if it is equal,it’ll execute the block of instructions 2 until it finds the break.

If the expression has not matched with any of the cases,only the statements following the default are executed.

Example

main()

{

int i=1;

switch(i-2)

{

case  -1 : printf(”hello\n”);

case  0 : printf(”bye\n”);

case   1 : printf(”hi\n”);

default : printf(”bye bye\n “);

};

Output

Hello

Bye

Hi

bye bye

the output is definetely not what expected. Program prints all the cases. This is because break is not used. Switch will execute the case where a match is found and all the subsequent cases and default as well.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Blogplay
  • LinkedIn
  • MySpace
  • Reddit
  • RSS
  • Twitter

written by Shweta

Jul 16

To show a multiway decision based several condition, we use the else if statement.

if (condition_1)

{

statements_1;

}

else if(condition_2)

{

statements_2;

}


…………

else if(condition_n)

{

statements_n;

}

else

{

statements;

}

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Blogplay
  • LinkedIn
  • MySpace
  • Reddit
  • RSS
  • Twitter

written by Shweta