switch, case and default

Branches control

switch causes control to branch to one of a list of possible statements in the block of statements. The syntax is
switch (expression) statement

case constant-expression :

where constant-expression must be an int and must be unique.

Once a value is computed for expression, the list of possible constant-expression values determined from all case statements is searched for a match. If a match is found, execution continues after the matching case statement and continues until a break statement is encountered or the end of statement is reached. If a match is not found and this statement prefix is found within statement,

default :

switch(expression)
{
    case '1':
    //some statements to execute when 1
    break;
    case '2':
    //some statements to execute when 2
    break;
    case '3':
    //some statements to execute when 3
    break;
    case '4':
    //some statements to execute when 4
    break;
    default: //some statements to execute when default; 
}

 

Advertisements

One Comment Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s