This program will show to you how switch statement execute by the java compiler.
import java.util.Scanner;
public class SampleSwitch
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
//declaration
int num;
System.out.print("Enter a number:");
num=input.nextInt();
Switch(num)
{
case 1:
case 3:
case 5:
case 7:
case 9:
System.out.print("You enter odd number:");
break;
case 2:
case 4:
case 6:
case 8:
case 10:
System.out.print("You enter even number:");
break;
default:
System.out.print("Your number you enter is not found in the case");
}
}
}
understand this program and practice to create your own program using switch statement,,
Showing posts with label switch statement. Switch Case Statement. Show all posts
Showing posts with label switch statement. Switch Case Statement. Show all posts
Monday, August 2, 2010
Switch Case Statement
Switch Case Statement is a type of selection control statement that exists in most modern imperative programming languages (e.g., Pascal, C, C++, C#, and Java). Its purpose is to allow the value of a variable or expression to control the flow of program execution via a multiway branch (or "goto", one of several labels). The main reasons for using a switch include improving clarity, by reducing otherwise repetitive coding, and (if the heuristics permit), offering the potential of faster execution through compiler optimization. In some programming languages, a statement that is syntactically different but conceptually the same as the switch statement is known as a case statement, select statement or inspect instruction.
Subscribe to:
Posts (Atom)