Switch Statements in Java

Posted on

Switch statements: Test a variable’s value with a list of values defined by us, where an appropriate match is found. The corresponding block of statements is executed and the structure is terminated.

Syntax: –

switch(expression)
{

case value-1: statements;
break;
case value-2: statements;
break;
case value-3: statements;
break;
[default : statement;]
}

 

/*A small demo on switch control structures*/

class App6{

public static void main(String yck[])
{

int x;
x=3;
switch(x)
{

case 1 : System.out.println(“One”);
break;
case 2 : System.out.println(“Two”);
break;
case 3 : System.out.println(“Three”);
break;
default : System.out.println(“Out of range”);
}
}

}

 

output:
\>javac App6.java
\>java App6
Three

Leave a Reply

Your email address will not be published. Required fields are marked *