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

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.


HTML Image tag, code...
How to code in HTML...
HTML text tag...
HTML text, how to display...
Container responsive styles...
HTML container tag...
HTML container...
Send forgot password by mail or message ...
PHP MySQL simple login system...
Check for duplicate username live or ema...
How to create simple search engine using...
Diseño de sitio web CSS Layout...
Diseño de caja flexible CSS3...
Botones en CSS...
ID y Clase, Diferencias entre Class e Id...
Esquina redondeada CSS...
Tamaño de caja css box-sizing: border-b...
Alineación CSS...
Altura y Anchura CSS...
Bordes CSS...