Powered by Blogger.

The Switch Statement & Using Program - Java Program

The Switch Statement Syntax:

switch(<expression>){
case<constant>:<statement_or_block>*
[break;]
case<constant>:<statement_ or_block>*
          [break;]
case<constant>:<statement_or_block>*
          [break;]
default:<statement_or_block>*
}

Switch Statement Program:-

public class SwitchTest{
public void mySwitch(intx){
  switch(x){
  case 1 : System.out.println(”RED”);
          break;
  case 2 : System.out.println(”GREEN”);
          break ;
  case 3 : System.out.println(”BLUE”);
          break;
  default: System.out.println(”WHITE”);
  }
  }
  public static void main(String[]args){
  SwitchTest st = new SwitchTest();
  st.mySwitch(2);
  }
 }

0 comments:

Post a Comment