SlideShare a Scribd company logo
Now, let us
Getting familiar with Java: Switch Case, Break and Default statement
40.08 STRING IN SWITCH CASE BEFORE ARRAY
So far in this series of Java tutorial under “Getting familiar with Java” we have brought
upon so many facets of Java from where we could find many instances of how code could run
and in the mean time we also find some short cuts in order to make the code precise and
response time of code should work intuitively and nice. In this tutorial we will love to study
about switch case statement of Java and how it works. As usual let us create the class with
Eclipse. So after opening Eclipse then go to FILE and then to NEW and then to CLASS and it
will open another window namely, NEW JAVA CLASS and as with previous article I have told
you, in the name of the class write down ‘ExampleSwitchCase’ and then move to tick, PUBLI
STATIC VOID MAIN inside WHICH METHOD STUBS WOULD YOU LIKE TO CREATE. So, here we
have created the class namely, ‘ExampleSwitchCase’ and the method namely PUBLIC STATIC
VOID MAIN.
Then click on FINISH to complete the auto creation of ‘ExampleSwitchCase.java’. Why the
name of the java file is exactly the same as that of the name of the class. This is the rule
with Java the name of the java file should be same as that of the name of class file. So now
the auto generated java file is as follows.
package day1.examples;
public class ExampleSwitchCase {
public static void main(String[] args) {
}
}
This means, the entire java file is inside day1.examples folder of java installation. The
package in java means folder. Then the class which is public and it is ExampleSwitchCase.
The name of the class should be in bold letters of each of the word inside it so here the class
name has three words such as Example and then Switch and then Class and all of these three
words are in capital letters and there should not be any space between these words.
Now let us write the Switch Case Statement. It is as follows and keep a look at it and then I
will explain it bit by bit.
package day1.examples;
public class ExampleSwitchCase {
public static void main(String[] args) {
Now, let us
int j = 2;
switch(j) {
case 0:
System.out.println("Value is 0");
break;
case 1:
System.out.println("Value is 1");
break;
case 2:
System.out.println("Value is 2");
break;
case 3:
System.out.println("Value is 3");
break;
default:
System.out.println("No Value");
break;
}
}
}
So, in the previous paragraph I explain in detail about package, class method which we create
automatically through
h Eclipse. Now, first declare the integer variable as:
int j =2;
This is self explanatory as we have seen, that it is a statement so presence of semicolon at
the end of it. Then the next line of code is:
switch(j) {
switch statement of integer j and beginning of opening curlybrace. Then begins various cases
of this switch J. Here, it is.
case 0:
System.out.println("Value is 0");
break;
case 1:
System.out.println("Value is 1");
break;
case 2:
Now, let us
System.out.println("Value is 2");
break;
case 3:
System.out.println("Value is 3");
break;
We do provide three case of switch statement. In the case 0, it asks the compiler if
the value of integer j is 0 then print it as value is 0 and if that value is 0 then
it will not shift to the following other cases. That is why the presence of break
statement is there and it is a statement and that is why the presence of semicolon. I
hope still this phase you have now cleared idea of case 0 and then move to case 1 and
if the value of j is 1 then compiler should print it other wise move to case 2 and
here if the value of integer j is 2 then compiler please print this as value is 2.
So, we have declared the value of integer j as 2 and this means that, it is true and
then comes the break statement so it will not go to check out the case 3. In the end
we have default statement which is as follows after writing one switch and three
cases.
default:
System.out.println("No Value");
break;
Here, the default statement begins and if no value of case 0, 1,2 and 3 satisfies the integer
then switch to default which says no value. Then put the closing curly brace.
Now, save this java file and then right click anywhere at the code window and then run as
and then run as Java application and the compiler will run and will show the result as follows.
Value is 2
After reaching to case 2 it finds that value is 2 so it prints the value is 2 and
then comes the break statement so it will not go to check again case 3 and then
default statements. Now we will see when we stop the break statement or commented it
and see what will be the compiler output. So just put comment mark of the break
statement after case 2. The result will be
Value is 2
Value is 3
It comes because, it checks case 0 and then 1 and then finds out that both of these
statements are not correct and then it reaches to case 2 and here it is correct and
then it prints and there is no break statement after case 2 so it automatically print
out the case 3.That is why presence of both case 2 and case 3 is there.
Now the question rises what is the role of default statement after switch cases. Let
us now look at it. Suppose our code is like as below.
package day1.examples;
public class ExampleSwitchCase {
Now, let us
public static void main(String[] args) {
int j = 5;
switch(j) {
case 0:
System.out.println("Value is 0");
break;
case 1:
System.out.println("Value is 1");
break;
case 2:
System.out.println("Value is 2");
break;
case 3:
System.out.println("Value is 3");
break;
default:
System.out.println("No Value");
break;
}
}
}
Here, is the new code with slight modification here. Here, we do find that, we
changed the declaration of value of integer from 2 to now 5 and this means in each of
the four switch cases we do not have the values satisfies the integer as it is upto 3
and this means that, it will print out the last default statement which is of no
values as all the switch and cases does not satisfy the value of integer and so ours
compiler output will be as follows.
No Value

More Related Content

PPTX
What is loops? What is For loop?
PPTX
02 - Prepcode
PPT
Decision making and looping
PPTX
Java loops for, while and do...while
PPTX
Module singleton
PPTX
Decision control structures
PPTX
Chapter 5 java
PDF
Programming in Java: Control Flow
What is loops? What is For loop?
02 - Prepcode
Decision making and looping
Java loops for, while and do...while
Module singleton
Decision control structures
Chapter 5 java
Programming in Java: Control Flow

Similar to Getting familair with java (20)

PPTX
SWITCH-CASE, Lesson Computer Programming.pptx
PPTX
Switch Case Statement
PPTX
Java Decision Control
PPTX
Java learning
PPT
Final requirement
PPTX
Lecture - 5 Control Statement
PPTX
Final project powerpoint template (fndprg) (1)
PDF
Lecture 7 Control Statements.pdf
PDF
java notes.pdf
PPTX
Switch Case in C Program
PDF
9-java language basics part3
PPTX
Presentation on C Switch Case Statements
PPTX
130706266060138191
PPTX
controlStatement.pptx, CONTROL STATEMENTS IN JAVA
PPTX
control statements
PPTX
Powerpoint presentation final requirement in fnd prg
PPTX
Object oriented programming_Unit1_contro statements.pptx
PPT
Switch statements in Java
PPTX
The Switch Statement in java
PDF
Control flow statements in java web applications
SWITCH-CASE, Lesson Computer Programming.pptx
Switch Case Statement
Java Decision Control
Java learning
Final requirement
Lecture - 5 Control Statement
Final project powerpoint template (fndprg) (1)
Lecture 7 Control Statements.pdf
java notes.pdf
Switch Case in C Program
9-java language basics part3
Presentation on C Switch Case Statements
130706266060138191
controlStatement.pptx, CONTROL STATEMENTS IN JAVA
control statements
Powerpoint presentation final requirement in fnd prg
Object oriented programming_Unit1_contro statements.pptx
Switch statements in Java
The Switch Statement in java
Control flow statements in java web applications
Ad

Recently uploaded (20)

PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Spectroscopy.pptx food analysis technology
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Big Data Technologies - Introduction.pptx
PPTX
Machine Learning_overview_presentation.pptx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Approach and Philosophy of On baking technology
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Encapsulation theory and applications.pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Spectroscopy.pptx food analysis technology
Digital-Transformation-Roadmap-for-Companies.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
A comparative analysis of optical character recognition models for extracting...
Building Integrated photovoltaic BIPV_UPV.pdf
Big Data Technologies - Introduction.pptx
Machine Learning_overview_presentation.pptx
Reach Out and Touch Someone: Haptics and Empathic Computing
Diabetes mellitus diagnosis method based random forest with bat algorithm
Spectral efficient network and resource selection model in 5G networks
Approach and Philosophy of On baking technology
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Group 1 Presentation -Planning and Decision Making .pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Encapsulation theory and applications.pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Ad

Getting familair with java

  • 1. Now, let us Getting familiar with Java: Switch Case, Break and Default statement 40.08 STRING IN SWITCH CASE BEFORE ARRAY So far in this series of Java tutorial under “Getting familiar with Java” we have brought upon so many facets of Java from where we could find many instances of how code could run and in the mean time we also find some short cuts in order to make the code precise and response time of code should work intuitively and nice. In this tutorial we will love to study about switch case statement of Java and how it works. As usual let us create the class with Eclipse. So after opening Eclipse then go to FILE and then to NEW and then to CLASS and it will open another window namely, NEW JAVA CLASS and as with previous article I have told you, in the name of the class write down ‘ExampleSwitchCase’ and then move to tick, PUBLI STATIC VOID MAIN inside WHICH METHOD STUBS WOULD YOU LIKE TO CREATE. So, here we have created the class namely, ‘ExampleSwitchCase’ and the method namely PUBLIC STATIC VOID MAIN. Then click on FINISH to complete the auto creation of ‘ExampleSwitchCase.java’. Why the name of the java file is exactly the same as that of the name of the class. This is the rule with Java the name of the java file should be same as that of the name of class file. So now the auto generated java file is as follows. package day1.examples; public class ExampleSwitchCase { public static void main(String[] args) { } } This means, the entire java file is inside day1.examples folder of java installation. The package in java means folder. Then the class which is public and it is ExampleSwitchCase. The name of the class should be in bold letters of each of the word inside it so here the class name has three words such as Example and then Switch and then Class and all of these three words are in capital letters and there should not be any space between these words. Now let us write the Switch Case Statement. It is as follows and keep a look at it and then I will explain it bit by bit. package day1.examples; public class ExampleSwitchCase { public static void main(String[] args) {
  • 2. Now, let us int j = 2; switch(j) { case 0: System.out.println("Value is 0"); break; case 1: System.out.println("Value is 1"); break; case 2: System.out.println("Value is 2"); break; case 3: System.out.println("Value is 3"); break; default: System.out.println("No Value"); break; } } } So, in the previous paragraph I explain in detail about package, class method which we create automatically through h Eclipse. Now, first declare the integer variable as: int j =2; This is self explanatory as we have seen, that it is a statement so presence of semicolon at the end of it. Then the next line of code is: switch(j) { switch statement of integer j and beginning of opening curlybrace. Then begins various cases of this switch J. Here, it is. case 0: System.out.println("Value is 0"); break; case 1: System.out.println("Value is 1"); break; case 2:
  • 3. Now, let us System.out.println("Value is 2"); break; case 3: System.out.println("Value is 3"); break; We do provide three case of switch statement. In the case 0, it asks the compiler if the value of integer j is 0 then print it as value is 0 and if that value is 0 then it will not shift to the following other cases. That is why the presence of break statement is there and it is a statement and that is why the presence of semicolon. I hope still this phase you have now cleared idea of case 0 and then move to case 1 and if the value of j is 1 then compiler should print it other wise move to case 2 and here if the value of integer j is 2 then compiler please print this as value is 2. So, we have declared the value of integer j as 2 and this means that, it is true and then comes the break statement so it will not go to check out the case 3. In the end we have default statement which is as follows after writing one switch and three cases. default: System.out.println("No Value"); break; Here, the default statement begins and if no value of case 0, 1,2 and 3 satisfies the integer then switch to default which says no value. Then put the closing curly brace. Now, save this java file and then right click anywhere at the code window and then run as and then run as Java application and the compiler will run and will show the result as follows. Value is 2 After reaching to case 2 it finds that value is 2 so it prints the value is 2 and then comes the break statement so it will not go to check again case 3 and then default statements. Now we will see when we stop the break statement or commented it and see what will be the compiler output. So just put comment mark of the break statement after case 2. The result will be Value is 2 Value is 3 It comes because, it checks case 0 and then 1 and then finds out that both of these statements are not correct and then it reaches to case 2 and here it is correct and then it prints and there is no break statement after case 2 so it automatically print out the case 3.That is why presence of both case 2 and case 3 is there. Now the question rises what is the role of default statement after switch cases. Let us now look at it. Suppose our code is like as below. package day1.examples; public class ExampleSwitchCase {
  • 4. Now, let us public static void main(String[] args) { int j = 5; switch(j) { case 0: System.out.println("Value is 0"); break; case 1: System.out.println("Value is 1"); break; case 2: System.out.println("Value is 2"); break; case 3: System.out.println("Value is 3"); break; default: System.out.println("No Value"); break; } } } Here, is the new code with slight modification here. Here, we do find that, we changed the declaration of value of integer from 2 to now 5 and this means in each of the four switch cases we do not have the values satisfies the integer as it is upto 3 and this means that, it will print out the last default statement which is of no values as all the switch and cases does not satisfy the value of integer and so ours compiler output will be as follows. No Value