Androidberry logo Androidberry text logo
AndroidBerry Home
  • Learn Java
  • Java Programs
  • Interview Questions
  • Androidberry Facebook connect
  • Androidberry twitter connect
  • Androidberry Google Plus connect

Java Tutorials


Basic Introduction

What is Java?
Java Features
Java vs Other Languages
Java on Internet
Java Components
Java First Program
Java Commandline argument
Java Constants
Java Variables
Java Types of Variables
Java Datatypes
Java Getting Input from User
Java Typecasting
Java Operators
Java Math Functions

Decision Making

Java if statement
Java if-else statement
Java nested if-else
Java else-if ladder
Java Switch case statement
Java Nested Switch Case
Java Ternary/Conditional operator

Looping

Java while loop
Java do-while loop
Java for loop
Java Enhanced loop/for each loop
Java Labelled for loop
Java Nesting of loops
Java Break and Continue

Classes and Objects

Java Class
Java Object
Java Object Array
Java Nesting of Classes
Java Methods
Java Method Overloading
Java Constructor
Java Constructor Overloading
Java This Keyword
Java Garbage Collection
Java Static Keyword
Java Arrays
Java String
Java StringBuffer
Java String vs StringBuffer
Java Vector Class
Java Vector vs Array
Java Wrapper Classes

Inheritance

Interfaces

Packages

Exception Handling

Multithreading

Java Applets

Graphics Programming

More on Java

Java Programs
Java Keywords Dictionary
Java Interview Questions

Java Switch case statement


Switch Case Statement

Java has a built-in multi way decision statement known as switch statement.

When it is used?

When in our program number of alternatives increases then program becomes difficult to read(Like if else statement). It may confuse to the developer a lot. This is overcome using the switch statement.

Switch case is similar to the if else statement, But the reason for switch case use is that switch knows which case need to be executed. This is not possible in if else statement. If else has to go through all conditions

Syntax

switch ( test-expression )
{
   case const_value1:
      // case 1 statements;
   break;

   case const_value2:
      // case 2 statements;
   break;
   .
   .
   .

   case const_value N:
      // case N statements;
   break;

   default:	// same as else 
      // default block statements
   break;
}	

Flowchart

Switch Case Statement Flowchat

How switch statement works?

  • The switch statement tests the value of a given expression against a list of case values.
  • When a match is found, a block of statements associated with that case is executed.
  • If the expression doesn’t match any case value in the switch statement, then compiler enters into the default block statement.

Example - Check entered character is Vowel or Consonant using switch case.

Vowel - it is speech sound produced when breath flows out through the mouth without being blocked by the teeth, tongue, or lips. Ex - a,e,i,o,u

Consonant - it is speech sound produced by completely or partly stopping the air being breathed out through the mouth. Ex- except Vowels all others are Consonants.

public class SwitchDemo
{
    public static void main(String[] args) 
    {
	char mychar = 'i';
	String result;
	switch(mychar)
	{
	    case 'a':
		result = "Vowel";
	    break;
		
	    case 'e':
		result = "Vowel";
	    break;
		
	    case 'i':
		result = "Vowel";
	    break;
		
	    case 'o':
		result = "Vowel";
	    break;
		
	    case 'u':
		result = "Vowel";
	    break;
		
	    default:
		result = "Consonant";
	    break;
	 }
	 System.out.println("Given character is "+result);
    }
}

Output

Given character is Vowel

In above example first of all condition is checked using mychar variable, the compiler searched for the matching case statement in the block of code. if case is matched then result variable is assigned.


Different ways of switch case

We can write the switch case statement in different ways.

1. Without using break statement

We can write above program for checking vowels and consonant as.

public class SwitchNoBreak
{
    public static void main(String[] args) 
    {
	char mychar = 'u';
	String result;
	switch(mychar)
	{
	    case 'a':
			
	    case 'e':
		
	    case 'i':
			
	    case 'o':
			
	    case 'u':
		result = "Vowel";
	    break;
		
	    default:
		result = "Consonant";
			
	 }
	 System.out.println("Given character is "+result);
    }
}

Output

Given character is Vowel

Best Examples for Practice
  • Check character is Vowel or Consonent program
  • Calculator program using else-if ladder
  • Check Number is Even or Odd program
  • Check number is Positive or Negative program
  • Find largest number from two/three numbers
  • Check year is Leap year or not Program
  • Swap numbers using temp and without temp variable program
  • Convert days into Days, Months, Weeks, Years program
  • Find ASCII value of any character program
<< Previous Next >>

See Also

All Java Programs
Java Keywords
Java Interview Questions

AndroidBerry Support

About us
Contact us
Suggest us
Questions?

Follow us

Androidberry FacebookFacebook
Androidberry TwitterTwitter
Androidberry GooglePlusGoogle+
Back to top
Androidberry Name Logo