AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

Java Switch


Java Tutorial » Java Switch

Java switch statement executes one statement from multiple conditions and this it is like if .. else .. if sequence statement.
The switch statement works with: int, byte, short, enum types, long, string and some wrapper types like Int, Byte, Short and Long.

Syntax of Switch-case ::
switch(expression){
case val1:
//code to be executed;
break; //optional
case val2:
//code to be executed;
break; //optional
......

default:
code to be executed if all cases are not matched;
}



Switch statement executes one statement from multiple conditions, example


Example below uses the weekday number to calculate the weekday name.

Example
public class Main {
public static void main(String[] args) {
int day = 5;

switch (day) {

case 1:
System.out.println("1 is for Monday");
break;

case 2:
System.out.println("2 is for Tuesday");
break;

case 3:
System.out.println("3 is for Wednesday");
break;

case 4:
System.out.println("4 is for Thursday");
break;

case 5:
System.out.println("5 is for Friday");
break;

case 6:
System.out.println("6 is for Saturday");
break;

case 7:
System.out.println("7 is for Sunday");
break;

default: System.out.println("Our number not match with any day");
}
}
}
Output:
5 is for Friday


Nested Switch Case statements


We can use a switch as part of the statement sequence of an outer switch. This is called a nested switch.

Example:
public class Main {
public static void main(String args[])
{
// nested switch case statement

char branch = 'C';
int collegeYear = 4;
switch( collegeYear )
{

case 1:
System.out.println("mathematics, physics, chemistry");
break;

case 2:
switch( branch )
{

case 'C':
System.out.println("Structure, Operating System, Java,");
break;

case 'E':
System.out.println("Processor, Memory, HDD");
break;

case 'M': System.out.println("Computer Operator, Assistant, Worker");
break;
}
break;

case 3:
switch( branch )
{

case 'C':
System.out.println("Machines, Calculations, Equals");
break;

case 'E':
System.out.println("Google design, Facebook, Instagram");
break;

case 'M':
System.out.println("Engine Internal Combustion, Mechanical Aerofoil");
break;
}
break;

case 4:
switch( branch )
{

case 'C':
System.out.println("turbojet, vane engine, propeller");
break;

case 'E':
System.out.println("carburetor, gasoline, compression");
break;

case 'M':
System.out.println("high pressure, compressor, turbine");
break;
}
break;
}
}
}
Output:
turbojet, vane engine, propeller




java switch case, string, enum, case or, expression, break, constant expression required, multiple, case, instanceof, case enum
Java Switch - java

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


Upload Image, display, edit and delete i...
Login Form with Image background...
How to Create an Image with Transparent ...
Portfolio Gallery Website with filtering...
Simple pagination script PHP MySQLi...
Center Image in div...
Image Hover Overlay Fade...
Sticky image / element / div on scroll...
Responsive images...
Create rounded image in HTML CSS...
Add border around image...
Position Text Over an Image HTML CSS res...
Create a Slideshow Images Gallery...
Create a Sticky Sidebar...
Search bar using CSS HTML...
Shrink Navigation Menu on Scroll...
How to Create Accordion HTML Templates...
Dropdown menu in the navigation bar...
Responsive Top Navigation Bar with mobil...
Split horizontal navigation bar...