Login or Register

  Welcome: Guest


Home >> Article >> Programming >> Java >> Post New Article Bookmark and Share

Use of switch in java programming

May 28, 2013  by: Joseph  Points: 12   Category: Java  Earning $0.20   Views: 476

Switch is a conditional statement which used to pick a statement if the condition and expression are equal. This article explains about the use and functioning switch along with example.

         

In this article I would like to explain about the use of switch in Java. Switch is a conditional statement which used to pick a statement if the condition is satisfied with the choose of the user. Switch is a very usefull tool in programming which help the programmer to avoid the use of if else conditional statement. The syntax for the switch condition is the following:-

Switch(expression)
{
case1: statement 1;
break;
case2: statement 2;
break;
case3: statement 3;
break;
:
case n: statement n;
break;
default: statement x;
break;
}


Above is the syntax of the switch condition. As you can see that in the switch we give an expression. If that expression is equal to 1 then the case1 statement will appear else if the expression is equal to 2 then the case2 statement will appear and so on, if none of the condition is satisfied with the user choose and cases then default statement will appear. This is the function of switch conditional statement. Below is an example for the switch condition statement.

Import java.util.scanner;
public class week
{
public static void main(string[] args)
{
System.out.PrintIn(" Enter the no: of the day(1-7)");
scanner s= new scanner(system.in);
int a= s.nextInt();
switch(a)
{
case1: System.out.PrintIn("Sunday);
break;
case2: System.out.PrintIn("Monday");
break;
case3: System.out.PrintIn("Tuesday");
break;
case4: System.out.PrintIn("Wednesday");
break;
case5:
System.out.PrintIn("Thursday");
break;
case6: System.out.PrintIn("Friday");
break;
case7: System.out.PrintIn("Saturday");
break;
default: System.out.PrintIn(" Illegal Choice");
break;
}
}
}


Above is an example of switch condition statement. From this program we can see that there are 7 cases with one expression that is a, the function of the program is to print the day according to the choice of the user give in number. This program will print Sunday if the user enter 1, if it is 2 it will print Monday, if the choice of the user choice is not between 1 to 7 then it will print Illegal Choice.
Like this program we can create numerous other types programs like this. All the best.




Comment(s)

No response found. Be the first to respond to this post

Post Comment

Please Log In  to post comment

Related Resources



Usage of Java Programming and Concepts
Describes the salient features of Java and explains about the usage of java.

Starting Java Programming
Java is computer language. It help the user to develop softwares,programs and even games also. It is easy to study this computer language. Sometimes you may see certain title says java games, java apps when you try to download mobile apps from mobile store. That means this app is made with java language. This article is about how to write and run a java program.

About Java
Java is a purpose object oriented programming language

Enterprise Java Beans : An Overview
This article contains basic information about Enterprise Java Beans (EJBs)Technology .

Application Deployment Tool in java
Java Based Tools for testing and deployment tool

Latest Resources From The Same Category



Use of switch in java programming
Switch is a conditional statement which used to pick a statement if the condition and expression are equal. This article explains about the use and functioning switch along with example.

Addition of 2 numbers in java program
For starting with Java we need to know 2 things first is how to print a message and how to accept 2 numbers & print there sum. This article is about how to create a program which accept 2 numbers and print there sum.

Starting Java Programming
Java is computer language. It help the user to develop softwares,programs and even games also. It is easy to study this computer language. Sometimes you may see certain title says java games, java apps when you try to download mobile apps from mobile store. That means this app is made with java language. This article is about how to write and run a java program.

Usage of Java Programming and Concepts
Describes the salient features of Java and explains about the usage of java.

Threads in Java
Here is the most commonly used concept i.e. threads in Java.

 

Top Performers     Top Contributors     Share and Earn     Terms of Service    Privacy Policy     Contact Us