Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Java - The switch construct in Java

                         

Switch is the control statement in java which also turns the normal flow control of the program as per conditions. It works same as If-Else construct. This is the difference between Switch and If-Else construct that switch is used for reduce the if statements. If the multiple choices are available then we generally use the If-Else construct otherwise Switch is easier than the If-Else construct. Switch checks your choice and jump on that case label if the case exists otherwise control is sent to the default label.

In this Program you will see that how to use the switch statement. This program take a number and check weather the number lies between 1 to 7. If user enters the number between 1 to 7 then program print the name of the day in sequence like for 1 prints Sunday, for 2 prints Monday and so on otherwise prints the message Invalid entry!. If user enters any character then the message will be printed by catch block. This program also using the break statement. The normal flow of control quits from the Switch block whenever break statement occurs. Full running program code is provided with the example.

Here is a code of program:-

import java.io.*;

public class Switch{
  public static void main(String args[]) throws Exception{
      int ch;
    System.out.println(
"Enter 1 for Sunday.");
    System.out.println(
"Enter 2 for Monday.");
    System.out.println("Enter 3 for Tuesday.");
    System.out.println(
"Enter 4 for Wednesday.");
    System.out.println("Enter 5 for Thrusday.");
    System.out.println(
"Enter 6 for Friday.");
    System.out.println("Enter 7 for Saturday.");
    System.out.print(
"your choice is : ");
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    try{
      ch=Integer.parseInt(in.readLine());
      switch(ch){        
        case 1:  System.out.println("Sunday");
 
           break;
        case 2:  System.out.println("Monday");
 
           break;
        case 3:  System.out.println("Tuesday");
            break;
        case 4:  System.out.println("Wednesday");
 
           break;
        case 5:  System.out.println("Thrusday");
            break;
        case 6:  System.out.println("Friday");
 
           break;
        case 7:  System.out.println("Saturday");
            break;
        default: System.out.println("Invalid entry!");
 
            break;
      }
    }
    catch(NumberFormatException ex){
      System.out.println(ex.getMessage() + " is not a numeric value.");
      System.exit(
0);
    }
  }
}

Download The Switch Example

                         

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 
Latest Searches:
Photoshop Refrozen Eff
Vector
get method of from in
insert data to text fi
image upload
program to validate us
rational robot tool in
customise css in jsp
Math.random()
vector class
ascii
Call Cookies from Serv
struts.jar download
how to take two diment
decimal to ANSI
class connect database
Photoshop Drawing Drag
Scrum
selecting option value
spring dao
show calender page
give me some java pro
validate radio button
calculate perimeter tr
Jigloo
html2pdf
count row in resultset
tattoo efectes
Thread Sleep Method
export database data i
Date Examples java
The eXo platform
linked list and GUI
toInt()
primitive data types
abstract class and abs
jsp login by insertin
jdbcrealm
jQuery To Hide the Div
line break in javascri
div tag
projections
ajax
retrieve image url fro
JSON rest
what is basic stack in
infix to pasfix into s
my clips
jdbc mysql connection
Photoshop Drawing Awes
apache derby embedded
two dimensional array
J2se Tutorial
jquery using .net 2.0
jsp mysql login
java Concurrency utili
string Compareing
word
DOJO Container
A
how to subtract calend
objective type questio
Get Array Size
get url
Tomcat deploy JSP
typecast between strin
how to reate a textfie
connect to sql databas
javascript separate in
informatica
sorting program
adding label and textb
java constants
prime numbers
displaying text in a d
wireless c
give the input text fi
iterator
transposition
menu bar in java
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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 | 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.