Wednesday, December 1, 2010

Java Tutorial #10

0
The Switch Statement in Java
The switch statement syntax is also the same as the C++.  It’s general syntax is:

switch(variable){

                case value1:
                Statements;
                break;

                case value2:
                Statements;
                break;


                default:
                Statements;
                break;
}

Where:
variable =  is the variable you want to switch
value1, value2 = is a Boolean value.   It can’t be a target of conditions.  It must be a numeral value not a character nor a string.

The switch statement is the enhanced version of if statement.  You maybe find it similar to if statement but switch statement is much more different to if statement.  The switch statement cannot be a target to any conditions of values.  The case can’t be a target of conditions to a value.  So, if you are about to use a conditions, still it is a good idea to use if-else if-else statement.   Consider this program.







No Response to "Java Tutorial #10"

Post a Comment