Under what circumstances does a default case in a switch sta

Under what circumstances does a default case in a switch statement execute? AND Must a default case appear in every switch statement (yes-it must or no-it does not need to appear).

Solution

What is switch?

switch is allow you to test multiple condition.

The syntax for a switch statement

switch(expression) {

case constant-expression :
//some code
break; /* optional */
  
case constant-expression :
//some code
break; /* optional */
  
/* you can have any number of case statements */
default : /* Optional */
//some code
}

we do have a doubt .. can\'t we just recode using if, else-if, else statements?

yes , but it look difficult to understand.

switch statement uses branches where each branch hold a condition. if it does not match with any branch condition then default branch executes.

does default case appear in every switch statement?

no-it does

example when we find isEVEN number ?

switch(10%2 == 0)
{
case 0:
prints(\"even number\");
break;
case 1:
prints(\"odd number\");
break;
}

here there is no use of default case:

Under what circumstances does a default case in a switch statement execute? AND Must a default case appear in every switch statement (yes-it must or no-it does

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site