Tuesday, 5 October 2010

21. Switch Statement

Taken from Mamun.
Switch statement as like as else if statement. When we create a program than need a lot of statement but when we need one of them statement then we use switch statement.
Structure of switch statement:
Switch (expression)
{
case constant_1:
statement_1;
break;
Case constant_2:
statement_2;
break;
............
default:
default_statement_x;
break;
}

switch statement have 4 keyword
1. switch
2. case.
3. break.
4. default.
Now i discuss about 4 keyword
Switch: Let be known the compiler by the switch that switch statement are working in that program. Most probably we compare it with if statement. As like as it statement written condition expression in '(  )' . Condition expressions are not

switch (expression)
           {
            ..................
            }
written relational or logical expression. It is only normal variable in conditional expression. Case statements are depending on conditional expression.

Case: Case depends on switch condition. Using constant value before case. That is depend on coalition expression. Using the colon (:) after case constant. Which case constant are equal the conditional expression then case statement will word.

Break: Those simple or compound statements are using with case that is used the break after simple or compound statement. When compiler fined the break statement than switch statement is closed.

Default: default as like as else statement. When conditional expression value is not equal with case constant than attach statement is defaults are compiled in the program.

Note that using the colon (:) after default keyword.
default:
statement ();
break;
Note:
1. Constant of two or more case are not same in switch statement.
2. You may used maximum 256 cases in switch statement according to ANSI value.
3. Replace the case constant you can be used char or int. If you use char than you write char in the single quotation (' ')
case 'a':

No comments:

Post a Comment