11th Computer Science 12th Computer Science BCA CBSE CBSE JAC JAC

Switch Statement in C++ Language

Switch Statement in C++ Language
Written by AIPS

Switch statement is used to execute different set of statements according to different values of a single variable or an expression.

It is a conditional statement and used for decision making when we have multiple options. it is an alternative of ladder if-else.

Each condition is specified with ‘case’ and each case can have a break statement.

‘break’ is used to exit form case.

Multiple cases can have a single ‘break’ statement.

when none of the case is satisfied then ‘default’ block is executed.

It is similar to ladder if-else

Syntax

switch(variable /expression)

{

case value1:

……………………

……………………

break;

case value2:

……………………

……………………

break;

case value3:

……………………

……………………

break;

case value4:

……………………

……………………

break;

case value n:

……………………

……………………

break;

default:

……………………..

……………………..

break;

}

About the author

AIPS

Leave a Comment