Tutorialslot.com

youtube facebook twitter gmail pinterest

java Switch Statement


It is a multi way branch statement.It provides an easy way for the execution of different parts of the code based on the value of given expression.

Syntax:
switch (expression)
{
case value-1:
   statement-1;
break;
case value-2:
   statement-2
break;
.
.
.
case value-n:
   statement-n
break;
default:
   default statement;
}

Example:
class Switch-Statement
{
   public static void main(String[] args)
  {
   int day = 7;

  switch (day)
  {
   case 1:
     System.out.println("monday");
     break;
   case 2:
     System.out.println("tuesday");
     break;
   case 3:
     System.out.println("wednesday");
     break;
   case 4:
     System.out.println("thursday");
     break;
   case 5:
     System.out.println("friday");
     break;
   case 6:
     System.out.println("saturday");
     break;
   case 7:
     System.out.println("sunday");
     break;
   default:
     System.out.println("no days");
     break;
   }
  }
}

Output:
sunday



Advertisement Powered By Google Adsense






IF YOU LIKE THIS TUTORIALS PLEASE SHARE

Share on whatsapp

POPULAR TAGS

Java java tutorials learn java java programming tutorial learn java online learning java professionals tutorials Multithreading Generics Object Oriented Language Methods Polymorphism Inheritance