Java Switch Statement

Java Switch Statement just like if-else-if ladder statement executes on a statement from multiple conditions. Java Switch Statement Syntax...

Java Switch Statement just like if-else-if ladder statement executes on a statement from multiple conditions.

Java Switch Statement Syntax

[switch(expression ){
        case value1:
        //code to be executed;     
   break;  //optional       
 case value2:
        //code to be executed;     
   break;  //optional        ......

default:
        code to be executed if all cases fail to match;
        }
]

Example:
[
public class JavaSwitchStatementExample {
    public static void main(String args[])
    {
        int score=30;
        switch(score)
        {
            case 30:
                System.out.println("Number is 30");
                break;
            case 40:
                System.out.println("Number is 40");
                break;
            case 50:
                System.out.println("Number is 50");
                break;
            case 60:
                System.out.println("Number is 60");
                break;
            default:
                System.out.println("No match for the number");


        }
    }

}
]

Java Switch Statement can be a fall-through

If break keyword is not used,Java Switch Statement becomes a fall-through.
Run below code and compare the result with above.

[
public class JavaSwitchStatementExample {
    public static void main(String args[])
    {
        int score=40;
        switch(score)
        {
            case 30:
                System.out.println("Number is 30");

            case 40:
                System.out.println("Number is 40");

            case 50:
                System.out.println("Number is 50");

            case 60:
                System.out.println("Number is 60");

            default:
                System.out.println("No match for the number");


        }
    }

}
]

You should note the Different between the two outputs.


COMMENTS

Name

android arrays cnna1 graphics java java control statements java.swing JButton JFrame JLabel JTextField laravel node.js OPP questions swing technology
false
ltr
item
Code WHIZZ: Java Switch Statement
Java Switch Statement
Code WHIZZ
https://code-whizz.blogspot.com/2017/03/java-switch-statement.html
https://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/
http://code-whizz.blogspot.com/2017/03/java-switch-statement.html
true
5534598864497432585
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