Page 1 of 1

Switch control in Java Rate Topic: ****- 1 Votes

#1 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Posted 26 January 2010 - 10:05 AM

maybe switch case is the best to use in decision making statements
we will now learn mostly everything about the switch statements in Java

Switch statements in java are used instead of the nested if else ladders
for example if there is a situation where you need to use the statements repeatedly then switch case can be useful there

see following code

				if (choice ==1)
		{
			System.out.println("A");
		}
		else if (choice ==2)
		{
			System.out.println("B");
		}
		else if (choice ==3)
		{
			System.out.println("C");
		}
		else if (choice ==4)
		{
			System.out.println("D");
		}
		else if (choice ==5)
		{
			System.out.println("E");
		}




this is really a boring job hence we will use the switch statement here

but let us take a look at the switch first

switch(choice)
		{
			case 1:
					// case 1 statements here
					break;
			case 2:
					// case 2 statements here
					break;
			case 3:
					// case 3 statements here
					break;
			case 4:
					// case 4 statements here
					break;
						default:
									   // default statements here
		}


now look at the code line by line
first line is the switch word containing the choice as argument
this selects the statements to be executed in the cases those are written in the statements

for example if choice = 1
then case 1 statements are executed

know that the

Quote

break;

statement is required where we want to leave the switch block when the case statements are executed
for example when we want to leave the code block of switch statements then put the break keyword after every case

Now what will happen if we do not provide the break statements??

let us take it with the example

consider the following small program

class SwitchDemo
{
	public static void main(String args[])
	
	{int choice = 1;	
	
	switch(choice)
		{
			case 1:
					System.out.println("A");
					
			case 2:
					System.out.println("B");
					
			case 3:
					System.out.println("C");
					
			case 4:
					System.out.println("D");
					break;
			case 5:
					System.out.println("E");
					
			case 6:
					System.out.println("F");
					break;
			default:
					System.out.println("Default");
					break;
		}
	}
}


Now let us see how this program runs

compiler comes to the first case after it takes the argument i.e choice = 1
it checks whether the argument is present in the case or not
in our case it is present so the statements in case 1 are executed, thus it prints

Quote

A



but wait a minute why did B, C and D also printed
because we have not put the

Quote

break
statement
thus the compiler continues to execute the switch block and continues until it encounters the first break statement.
The first break statement encounters at case 4 hence it jumps out of the switch block and executes further statements


Now what is the default case??
If no condition is satisfied then default case runs
i.e. if we give choice = 9 then
output will be

Quote

Default

but it is not compulsory to put the default statement in last
we can put it anywhere in the switch block

see following code
class SwitchDemo
{
	public static void main(String args[])
	
	{int choice = 9;	
	
	switch(choice)
		{
			case 1:
					System.out.println("A");
					
			default:
					System.out.println("Default");
					break;

			case 2:
					System.out.println("B");
					
			case 3:
					System.out.println("C");
					
			case 4:
					System.out.println("D");
					break;
			case 5:
					System.out.println("E");
					
			case 6:
					System.out.println("F");
					break;
			
		}
	}
}



this also prints out the same result


Now follow some rules that are to be followed


1. Switch case cannot accept the float values as cases
2. Only data types lower than Int are allowed i.e int , short, byte and char
3. If an implicit conversion is done in the program then switch block automatically does the conversions in the block


Valid examples of switch statements are as follows
	char choice = 'A';	
	
	switch(choice)
		{
			case 'A':
					System.out.println("A");
					break;
			case 'B':
					System.out.println("B");
					break;
			case 'C':
					System.out.println("C");
					break;
			
		}


If you use character values for cases then you have to put them in the single quotes as shown above
byte choice = 128;	
	
	switch(choice)
		{
			case 1:
					System.out.println("A");
					break;
			case 2:
					System.out.println("B");
					break;
			case 128:							// this throws error as byte range is only upto 127
					System.out.println("C");
					break;
			
		}



also we cannot use any type of array in switch statements



This is most of the switch statement

Is This A Good Question/Topic? 4
  • +

Replies To: Switch control in Java

#2 thosarsupriya  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-January 10

Posted 19 February 2010 - 06:40 AM

Good,u almost covered every thing of switch in java
Was This Post Helpful? 0
  • +
  • -

#3 saket_iknowlife  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-February 10

Posted 24 February 2010 - 12:52 AM

thnks! this was very helpful! :)
Was This Post Helpful? 0
  • +
  • -

#4 XRaizeX  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 20
  • Joined: 20-March 10

Posted 23 March 2010 - 09:35 PM

thanks for the tutorial, it helped a lot.
And could case read letters as well like
switch(choice)....
case A: //code
case B: //code
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1