Java Switch Statement Example

Java Switch Statement Example

Java Switch Statement in one way or the other is similar to Java If-else Statement .One condition is executed from several statements.

Java Switch Statement Syntax :


 switch(expresssion statement){  
 case 1: value   
 //code that will be executed if true value is returned:  
 break;  
 case 2: value  
 //code that will be executed if true value is returned:  
 break;  
 .....................  
 default:  
 //this code is executed if all the conditions fails:  
 }  

Java Switch Statement Example:

 /**  
  *  
  * @author acer  
  */  
 public class SwitchStatement {  
   public static void main(String[] args) {  
     int score=70;  
     switch(score)  
     {  
         case 90:  
         System.out.println("Student score grade A in maths");  
         break;  
         case 80:  
         System.out.println("Student score grade B in maths");  
         break;  
         case 70:  
         System.out.println("Student score grade C in maths");  
         break;  
         case 60:  
         System.out.println("Student score grade D in maths");  
         break;  
         default:  
         System.out.println("Student score grade E in maths");  
     }  
If  break keyword is not used,Java Switch Statement becomes a fall through.

What it means by Java Switch Statement fall through?

Java fall through statements means that if the first conditions returns true,other statements that follow are executed.

Execute below code and see the results:
 /**  
  *  
  * @author acer  
  */  
 public class SwitchStatement {  
   public static void main(String[] args) {  
     int score=80;  
     switch(score)  
     {  
         case 90:  
         System.out.println("Student score grade A in maths");  
         case 80:  
         System.out.println("Student score grade B in maths");  
         case 70:  
         System.out.println("Student score grade C in maths");  
         case 60:  
         System.out.println("Student score grade D in maths");  
         default:  
         System.out.println("Student score grade E in maths");  
     }  
   }  
 }  

Output 

 /**  
 
Student score grade B in maths
Student score grade C in maths
Student score grade D in maths
Student score grade E in maths

See also: Java If-else Statement

COMMENTS

Name

android android Apps android emulators antivirus blogger bluestack Break Statement in Java constructors download downloader drivers emulators java java control statements java do-while loop java for loop java infinitive do-while loop java system methods java tutorials Java While Loop java.awt java.swing javalookandfeel javascript JButton JCheckBox JColorChooser JComboBox JDialog JFileChooser JFrame JLayeredPane JList JMenuBar joomla JOptionPane JPanel JPasswordField JPopupMenu JProgressBar JRadioButton JScrollBar JSlider JSpinner JTabbedPane JTable JTextArea JTextField JToolBar mysql php plugins Polymorphism setToolTiptext skype for windows software swing class themes Visual Studio Code WeChat whatsapp wordpress
false
ltr
item
Java Codes: Java Switch Statement Example
Java Switch Statement Example
Java Switch Statement Example
Java Codes
http://www.download-all.net/2017/01/java-switch-statement-example.html
http://www.download-all.net/
http://www.download-all.net/
http://www.download-all.net/2017/01/java-switch-statement-example.html
true
7826648292785071009
UTF-8
Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS CONTENT IS PREMIUM Please share to unlock Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy