Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Switch 
 

Sometimes it becomes cumbersome to write lengthy programs using if and if-else statements. To avoid this we can use Switch statements in Java.

 

Switch

                         

Sometimes it becomes cumbersome to write lengthy programs using if and if-else statements. To avoid this we can use Switch statements in Java. The switch statement is used to select multiple alternative execution paths. This means it allows any number of possible execution paths. However, this execution depends on the value of a variable or expression. The switch statement in Java is the best way to test a single expression against a series of possible values and executing the code.
Here is the general form of switch statement:

switch (expression){
case 1:
code block1
case 2:
code block2
.
.
.
default:
code default;
}
The expression to the switch must be of a type byte, short, char, or int. Then there is a code block following the switch statement that comprises of multiple case statements and an optional default statement. 
The execution of the switch statement takes place by comparing the value of the expression with each of the constants. The comparison of the values of the expression with each of the constants occurs after the case statements. Otherwise, the statements after the default statement will be executed. 

Now, to terminate a statement following a switch statement use break statement within the code block. However, its an optional statement. The break statement is used to make the computer jump to the end of the switch statement. Remember, if we won't use break statement the computer will go ahead to execute the statements associated with the next case after executing the first statement. 
Here is an example which will help you to understand more easily:

switch (P {       // assume P is an integer variable
case 1:
System.out.println("The number is 1.");
break;
case 2:
case 4:
case 8:
System.out.println("The number is 2, 4, or 8.");
System.out.println("(That's a power of 2!)");
break;
case 3:
case 6:
case 9:
System.out.println("The number is 3, 6, or 9.");
System.out.println("(That's a multiple of 3!)");
break;
case 5:
System.out.println("The number is 5.");
break;
default:
System.out.println("The number is 7,");
System.out.println(" or is outside the range 1 to 9.");
}

For example the following program Switch, declares an int named week whose value represents a day out of the week. The program displays the name of the day, based on the value of week, using the switch statement.

class Switch{
  public static void main(String[] args){
    int week = 5;
        switch (week){
            case 1:  System.out.println("monday"); break;
            case 2:  System.out.println("tuesday"); break;
            case 3:  System.out.println("wednesday"); break;
            case 4:  System.out.println("thursday"); break;
            case 5:  System.out.println("friday"); break;
            case 6:  System.out.println("saturday"); break;
            case 7:  System.out.println("sunday"); break;
            
            default: System.out.println("Invalid week");break;
        }
    }
}

Output:

C:\javac>javac Switch.java

C:\javac>java Switch
friday

In this case, "friday" is printed to standard output.
One other point to note here is that the body of a switch statement is known as a switch block. The appropriate case gets executed when the switch statement evaluates its expression.

                         

» View all related tutorials
Related Tags: c class io nested definition source while ini member e il it not init art ce in no part as

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

2 comments so far (
post your own) View All Comments Latest 10 Comments:

hai urs teaching is very good i will visit ur site once in a day

Posted by karthik reddy on Friday, 03.7.08 @ 17:28pm | #51845

how used to string variable in Switch statement

it possible or not

Posted by vijay on Monday, 01.28.08 @ 12:59pm | #46383

Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.