Java/C2/Switch-Case/English

From Script | Spoken-Tutorial
Revision as of 10:16, 9 May 2013 by Sneha (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Title of script: Switch Case in Java

Author: TalentSprint

Keywords: case, switch, conditions, video tutorial


Visual Cue Description
Slide 1

Welcome

Welcome to the spoken tutorial on Switch case in Java.
Slide 2

Learning Outcomes

In this tutorial, you will learn how to use the switch case construct in Java
Slide 3

Tools Used

For this tutorial we are using
  • Ubuntu v 11.10
  • JDK 1.6 and
  • Eclipse 3.7.0


Slide 4

Prerequisites

For this tutorial, you should have knowledge of if else statement in Java.


If not, please go through the tutorials on these topics available at our website which is as shown http://spoken-tutorial.org

Slide 5

switch case

A switch case is used to perform actions based on the value of a variable.


Here is the syntax for a switch case statement.


Let us use it now.

Minimize Slides and open Eclipse

Eclipse should contain the following code

public class SwitchCaseDemo{

public static void main(String[] args){

}

}

I already have Eclipse opened.


I have created a class named SwitchCaseDemo.


Now Let us add some variables.

Inside the main, type

int day = 3;

String dName = “”;


Highlight day and dName when you explain.

Inside the main method, we will create a variable day of type int.


So type inside the main method int day and we can give it a value equal to 3 semi-colon.


Now, let us create a variable dName of type String.

String ane we shall linitialize it to null.


Here dName is a variable to hold the names of the days of a week.


day stores the day number.

Now, we will type the switch case statement.So next line type


 switch within brackets day , then open curly brackets... Press enter



Highlight switch(day) This statement defines which variable is under consideration for the cases.
Type


switch(day) {

case 0:

dName = “Sunday”;

break;

}

Next line type


case 0 colon

dName equal to within double quotes Sunday semicolon

Then type break semicolon.


This statement says that if the value of day is 0, then dName must be set to Sunday.


Note that a break statement must be used at the end of each case.


without the break statement, the switch-case functions in a complex fashion.


It will be explained in subsequent part of the tutorial.

switch(day) {

case 0:

dName = “Sunday”;

break;

case 1:

dName = “Monday”;

break;

case 2:

dName = “Tuesday”;

break;

case 3:

dName = “Wednesday”;

break;

case 4:

dName = “Thursday”;

break;

case 5:

dName = “Friday”;

break;

case 6:

dName = “Saturday”;

break;

}

Similarly, let us type the remaining cases.


case 1 colon

dName equal to within double quotes Monday semicolon

break

case 2 colon

dName equal to within double quotes Tuesday semicolon

break

case 3 colon

dName equal to within double quotes Wednesday semicolon

break '

case 4 colon

dName equal to within double quotes Thursday semicolon

break

case 5 colon

dName equal to within double quotes Friday semicolon

break case 5 colon

dName equal to within double quotes Saturday semicolon

break Then case 6 Colon dnamewihin double quotesSaturaday breakSemicolon Then close the brackets.

now Let us add a print statement and see the code in action.
Type System.out.println(dName);


save and run.


Point to output

So next line Type System dot out dot println within brackets dName semicolon.


Now Save and runfile.

Now press Ctrl S and Ctrl F11 keys


we get the output as Wednesdaywhich is corresponding to the case 3.


Now Let us change the value of day and see the result

Change day = 3 to day = 0


save and run.


Point to output

SoChange 3 to 0


NowSave and Run


As we can see, the output is Sunday corresponding to the case 0


Now what if there is no case corresponding to the value. Let us see that

Change day = 0 to day = -1

save and run. Point to output

So change day equal to -1 Save and run the file

As we can see, there is no output.


But it would be better if we could have a case for all other values.


That is done by using the default keyword.

After the last case, type this

default:

dName = “Wrong Choice”

break;


save and run.


Point to output

So After the last case, type


default colon

dName equal to within double quotes Wrong Choicesemicolon

break semicolon We do not say case default;

Note that we simply use the keyword default .

Now let us run the code. So save and run the file


As we can see, the default case is executed and the required message Wrong choice is printed.


Let us try with another random value.

Change day = -1 to day = 15

save and run. Point to output

Change -1 to 15

As we can see, again the default case is executed.

Now let us see what happens if we remove the break statement.
Change day = 15 to day = 4

Remove the break in case 4 statement there

So Let us change day = 15 to day = 4


 remove the corresponding break statement for day=4


Now Save and run
Point to output Although the case is 4, we get the output as Friday and not Thursday.
Highlight case 4 and case 5 This is because of the way switch case works.


First the value of day is compared with 0.


Then with 1 and then with 2 and so on with all the possible cases.

When a match is found, it executes all the case from the match onwards.


In our case, it executed case 5 after case 4 .

Then it stops because of the break statement in case 5.

To avoid that, we need to add a break statement in each case.
Type in case 4

break;

Let us now add the break statement we have removed.


So type Type break semicolon.


Now let us run the code.

Save and run. Point to output As we can see, now only case 4 is executed.


As a rule, remember to use a break statement in every case to avoid errors.

Minimize the Eclipse window and switch to slides.


Slide 6

Summary

We have come to the end of this tutorial.

In this tutorial we have learnt how to use switch case construct and how to use break statement.

Slide 7Assignment As an assignemetWrite a program that has a name and gender as a variable Use a switch case statement that prints “Hello Mr....” for males and “Hello Ms...” for females.
Slide 8About the Spoken Tutorial Project
  • It summarises the Spoken Tutorial project
  • If you do not have good bandwidth, you can download and watch it


To know more about the Spoken Tutorial project, watch the video available at the following link, that summarises the project.Alternatively, you can download and watch it.
Slide 9Spoken Tutorial WorkshopsThe Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test


The Spoken Tutorial Project Team. Conducts workshops using spoken tutorials and gives certificates for those who pass an online test. For more details, please write to contact AT spoken HYPHEN tutorial DOT org.
Slide 10Acknowledgement
  • Spoken Tutorial Project is a part of the Talk to a Teacher project
  • It is supported by the National Mission on Education through ICT, MHRD, Government of India
  • More information on this Mission is available at


Spoken Tutorial Project is a part of the Talk to a Teacher project and is supported by the National Mission on Education through ICT, MHRD, Government of India. More information on this Mission is available at spoken HYPHEN tutorial DOT org SLASH NMEICT HYPHEN Intro
Slide 11About the contributor
  • This tutorial has been contributed by TalentSprint
  • www.talentsprint.com
  • Thanks for joining


This tutorial has been contributed by TalentSprint. Thanks for joining.



Contributors and Content Editors

Arya, Sneha