swith-case

switch-case is used to execute statement depends on condition. It is similar to if-else.
Syntax:
switch(expression)
{
case constant1:
statement1;
break;
case constant2:
statement2;
break;
.
.
.
case constantN:
statementN;
break;
default:
statement4;

}
Explanation:

The expression inside switch() will be checked against constant1, constant2… constantN. If any constant matches with the expression then corresponding statements will be executed. Here break; is used to skip the rest of case

Example:

OUTPUT

Enter a positive value : 84
Entered value is even

OUTPUT

Enter a positive value : 81
Entered value is odd

NOTE

Expression inside switch() should contain only char or int type variable or constant.


Next Previous