0% found this document useful (0 votes)
4 views8 pages

Ps

Uploaded by

hlemorvan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

Ps

Uploaded by

hlemorvan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 8
2arii2023 11:18 Java Swit 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools Java Switch [crv] Looe | Java Switch Statements Instead of writing many if..else statements, you can use the switch statement. The switch statement selects one of many code blocks to be executed Syntax switch(expresston) { case x: // code block break; case y: // code block break; default: // code block This is how it works: * The switch expression is evaluated once. * The value of the expression is compared with the values of each case * If there is a match, the associated block of code is executed hitpssivawwschools.comfavaljava_switch.asp ve 2arive023 11:98 Java Switeh 3 w Tutorials» —Exercisese Servicese§ QU O SignUp Login schools CSS JAVASCRIPT SQL. PYTHON. «JAVA. PHP, «SHOW TO Example int day = 4; switch (day) { case 1: system. out .print1n("Monday"); break; case 2: system.out.print1n("Tuesday"); break; case 3: system. out.print1n("Wednesday") ; break; case 4: system.out.print1n("Thursday"); break; case 5: System. out .print1n("Friday"); break; case 6: system.out.print1n("Saturday"); break; case 7: System.out.println("sunday") ; break; + // Outputs “Thursday” (day 4) Try it Yourself » The break Keyword When Java reaches a break keyword, it breaks out of the switch block, This will stop the execution of more code and case testing inside the block. htipsitwnww.w3schools convavaljava_switch asp 28 2arii2023 11:18 Java Swit 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools A break can save a lot of execution time because it "ignores" the execution of all the rest of the code in the switch block. The default Keyword The default keyword specifies some code to run if there is no case match: Example int day = 4; switch (day) { case 6: System.out.printIn("Today is Saturday"); break; case 7: System.out.printIn("Today is Sunday"); break; system.out.print1n("Looking forward to the Weekend"); + // Outputs “Looking forward to the Weekend" hitpssvawawschools.comfavaljava_switch.asp 38 2arii2023 11:18 Java Swit 3 w Tutorialsy —Exercisesw Servicese§ QO Log in schools Note that if the default statement is used as the last statement in a switch block, it does not need a break. Exercise: Insert the missing parts to complete the following switch statement. int day = switch ( ) { 1: system. out.print1n("Saturday") ; break; 2: system. out.print1n("Sunday") ; Start the Exercise hitpssvawawschools.comfavaljava_switch.asp 48 2arii2023 11:18 Java Swit Tutorialse Exercisesw — Servicese§ QO schools W3Schools Spaces Build Your Web, Mobile, and Desktop Solutions Get Premium! COLOR PICKER © Hone hitpssvawawschools.comfavaljava_switch.asp Log in se 2arii2023 11:18 Java Swit v’ Tutorials Exercisesw Servicesey§ QO Login ad Get Certified Get Noticed Cera TW ett} ay reat ody SPACES UPGRADE NEWSLETTER GET CERTIFIED REPORT ERROR hitpssvawawschools.comfavaljava_switch.asp oe 2arive023 14:18 w schools Tutorials css Exercises» Services JAVASCRIPT SQL. PYTHON W3.cSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial Top References HTML Reference SS Reference JavaScript Reference SQL Reference Python Reference W3.CSS Reference Bootstrap Reference PHP Reference HTML Colors Java Reference ‘Angular Reference Query Reference Top Examples HTML Examples SS Examples JavaScript Examples How To Examples SQL Examples Python Examples Wa.CSS Examples Bootstrap Examples PHP Examples Java Examples XML Examples JQuery Examples G@ @ ® © Forum asour W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning Java Switeh Q 0 JAVA PHP. Get Certified HTML Certificate css certificate JavaScript Certificate Front End Certificate SQL Certificate Python Certificate PHP Certificate Query Certificate Java Certificate c++ Certificate i Certificate XML Certificate Sign Up How TO Login W3.css Tutorials, references, and examples are constantly reviewed to avold errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 1999-2023 by Refsnes Data. All Rights Reserved. W3Schools is Powered by htipsilweww.w3schools convavaljava_switch asp c 718 2arive023 14:18 Java Switeh 3 w Tutorials~ Exercisesw Serviesey§ QO SignUp Login = CSS _JAVASCR SQL PYTHON JAVA. PHP, «= HOWTO.-—W3.CSS_—C ipsum w28chools.comfaval ae

You might also like