SlideShare a Scribd company logo
3
Most read
1 | 8
USING CONDITIONAL STATEMENTS:
SWITCH STATEMENTS
Java Programming – Using Conditional Statements: Switch Statements
WHAT IS A CONDITIONAL STATEMENT?
A conditional statement in Python programming is a type of statement that compares the values of two or
more operands.
An operand refers to the data that is used by the program either for comparison, manipulation, or
mathematical operation. An operand can be:
▪ a value inputted by the user
▪ a value set by the programmer inside the program
The results of the comparison using a conditional statement can result to either a TRUE or FALSE response.
▪ The TRUE response signifies that the values of operands are equal to one another or it is within the
range provided by one of the operands.
▪ The FALSE response indicates that the values of operands are not equal to one another or it is outside
the range provided by one of the operands.
Programmers commonly use truth tables to determine whether the comparison between the data and the
values set in the program will yield either a TRUE or FALSE response. The truth table is a visual
representation that displays the inputted data, the set value, the argument, and the response.
TRUTH TABLE
Inputted Data Set Value Argument Response
8 5 8 > 5 True
20 20 20 != 20 False
Yes Yes Yes == Yes True
F f F == f False
25 15 25 >= 15 True
In this lecture, we have to operands labelled as inputted data and set value. The “inputted data” refers to the
data that was entered by the user in the program. The “set value” is the data that was specifically assigned
in the program. The set value will serve as our basis for comparison that will check whether inputted data
is equal to it or within its range.
The “argument” is a statement that indicates how the operands are being compared to one another.
Arguments in Python are expressed by using comparison operators. The comparison operators available in
Python programming were as follows:
COMPARISON
OPERATOR
DEFINITION
SAMPLE
STATEMENT
== A comparison operator that checks if the operands are EQUAL to one another. a == b
!=
A comparison operator that checks if the operands are NOT EQUAL to one
another.
a != b
>
A comparison operator that checks if value on the left side of the argument is
GREATER THAN the value on the right side of the argument.
a > b
<
A comparison operator that checks if value on the left side of the argument is
LESS THAN the value on the right side of the argument.
a < b
>=
A comparison operator that checks if value on the left side of the argument is
GREATER THAN OR EQUAL TO the value on the right side of the argument.
a >= b
<=
A comparison operator that checks if value on the left side of the argument is
LESS THAN OR EQUAL TO the value on the right side of the argument.
a <= b
The “response” serves as the result of comparing the operands. The response can either be TRUE or FALSE
based on the conditions that will determine whether the operands matches or does not match with one
another.
2 | 8
USING CONDITIONAL STATEMENTS:
SWITCH STATEMENTS
Java Programming – Using Conditional Statements: Switch Statements
PRACTICE WITH TRUTH TABLES
Complete the table by filling up the Response Column with either TRUE or FALSE:
TRUTH TABLE
No. Inputted Data Set Value Argument Response
1 Rowell rowell Rowell == rowell
2 15 15 15 != 15
3 male Male male == Male
4 25 15 25 >= 15
5 1.3 4 1.3 <= 4
6 15 18 15 == 18
7 giraffe rhino giraffe == rhino
8 90 91 90 != 91
9 48 45 48 >= 45
10 107 92 107 < 92
USING SWITCH STATEMENT
Now that we know how conditional statements work it is time for us to learn how to use one of the most
basic form of conditional statements called switch statement.
The switch statement is a type of conditional statement that is used to execute a block of code by comparing
an operand to a specific value that was set in the program.
The switch statement uses conditions that are labeled as cases. Each case is composed of the case,
executable block of code and a break statement.
Consider this example of a case:
Case
It refers to the statement that contains the condition.
In the source code above, the line case “January”: details the condition where it asks if the operand
presented to it is equal to January.
Executable Block of Code
The executable block of codes contains the instructions that the program will have to perform once the
condition on the case matches the value of the operand.
In our source code sample, the messages "The month is named after the Roman God, Janus." and "The
presider of doors and beginnings." will be displayed on the screen if the operand is equal to January.
If the operand fails to match the condition, the program will skip reading the executable block of code
and jump directly to the next case.
Break Statement
The break statement indicates the end of the case. It signals the program to stop reading the next line
of codes within the switch statement.
If the program reached the line that contains the break; statement, the program will skip all other
conditions and jump to the line of code that is outside the switch statement.
3 | 8
USING CONDITIONAL STATEMENTS:
SWITCH STATEMENTS
Java Programming – Using Conditional Statements: Switch Statements
Sample Program 1:
Create a Java program that will require the user to input any number from 1-3. The program will display an
output statement based on these given conditions:
▪ If the user inputted number 1, it will display the sentence “You entered the number ONE.”.
▪ If the user inputted number 2, it will display the sentence “You entered the number TWO.”.
▪ If the user inputted number 3, it will display the sentence “You entered the number THREE.”.
▪ If the user inputted any number that is outside the range, it will display the sentence “The number
you have entered is outside of the range given.”.
Simulation of Program 1:
USER’S INPUT PROGRAM’S OUTPUT
1
2
3
8
Source Code of Sample Program 1:
4 | 8
USING CONDITIONAL STATEMENTS:
SWITCH STATEMENTS
Java Programming – Using Conditional Statements: Switch Statements
Let us examine the code:
▪ Line 1 contains the package name of our Java program named “switch_statement”.
▪ Line 2 is where we import the scanner in our program. In our previous sessions, we have learned
that the scanner is a class in Java program that enables the program to get input from the user
▪ Line 4 contains the class name of our Java program named “Switch_Statement” while line 5 contains
an open bracket ( { ) which indicates the beginning of the class.
▪ Line 7 contains the header of our main program while line 8 contains an open bracket ( { ) which
indicates the beginning of the program body.
▪ On Line 9 we introduced our scanner to the program and assigned the name “mitis” to it. The new
Scanner statement declares mitis as a new scanner in the program that takes in inputs from the
user by using the System.in function.
▪ Line 10 is used to display message on the screen using the print ( ) function. The line explicitly
instructed the program to show the message “Enter any number from 1-3” on the display screen.
▪ Line 11 displays the procedure that prepares the program to accept input from the user. The int num
part of the statement declares the incoming input from the user as an integer (int) with the assigned
name “num”. The mitis.nextInt( ) tells the program that the input will be captured using the scanner
named mitis and that the input should be treated as an integer.
▪ Lines 12-26 details the procedure of using the switch statement.
5 | 8
USING CONDITIONAL STATEMENTS:
SWITCH STATEMENTS
Java Programming – Using Conditional Statements: Switch Statements
• Line 12 displays the use of the function switch ( ). The identifier num that was enclosed in
parentheses instructs the program that the value that was contained in num will be the value that
will be used for the switch statement. The line 13 contains an open bracket ( { ) which indicates
the beginning of the switch statement.
• Line 14 contains the first condition of the program which checks if the value contained in num is
equal to 1. If num is equal to one then the display message on line 15 will be shown on the screen.
The line 16 contains the break statement which indicates the end of the first case.
Note:
If the value of num is not equal to 1, the program will skip reading lines 15 and 16 and
automatically jumps to line 17.
• Line 17 contains the second condition which checks if the value contained in num is equal to 2. If
num is equal to two then the display message on line 18 will be shown on the screen. The line 19
contains the break statement which indicates the end of the second case.
Note:
If the value of num is not equal to 2, the program will skip reading lines 18 and 19 and
automatically jumps to line 20.
• Line 20 contains the third condition which checks if the value contained in num is equal to 3. If
num is equal to three then the display message on line 21 will be shown on the screen. The line
22 contains the break statement which indicates the end of the third case.
Note:
If the value of num is not equal to 3, the program will skip reading lines 21 and 22 and
automatically jumps to line 23.
• Line 23 contains the default condition which accepts the input if it fails to match with any of the
conditions written above it. If the program has reached this line of code it will automatically
display the message on line 24. The line 22 contains the break statement which indicates the end
of the default case. The line 26 contains an close bracket ( } ) which indicates the end of the switch
statement.
6 | 8
USING CONDITIONAL STATEMENTS:
SWITCH STATEMENTS
Java Programming – Using Conditional Statements: Switch Statements
Sample Program 2:
Create a Java program that will require the user to choose an animal among the list of choices. The list of
animals include dog, cat, bird, and fish. After the user have entered their choice of pet, the program will tell
what personality do they have based on their chosen pet.
▪ If the user inputted dog, it will display the sentence “You have an outgoing personality.”.
▪ If the user inputted cat, it will display the sentence “You have a conservative personality.”.
▪ If the user inputted bird, it will display the sentence “You have caring personality.”.
▪ If the user inputted fish, it will display the sentence “You have a calm and stable personality.”.
▪ If the user inputted a name of an animal that is not included in the list, it will display the sentence
“The animal you have entered is not in the list.”.
Simulation of Program 2:
USER’S INPUT PROGRAM’S OUTPUT
dog
cat
bird
fish
chicken
Source Code of Sample Program 2:
7 | 8
USING CONDITIONAL STATEMENTS:
SWITCH STATEMENTS
Java Programming – Using Conditional Statements: Switch Statements
Programming Challenge 1:
Create a Java program that will require the user to input a name of a country based on list of choices. The
list includes Philippines, Japan, Korea, and China. After the user have entered their preferred country, the
program will display the equivalent of “I love you” in the chosen language.
▪ If the user inputted Philippines, it will display the sentence “Mahal kita.”.
▪ If the user inputted Japan, it will display the sentence “Aishiteru”.
▪ If the user inputted Korea, it will display the sentence “Saranghaeyo.”.
▪ If the user inputted China, it will display the sentence “Wo ai ni”.
▪ If the user inputted a name of a country that is not included in the list, it will display the sentence
“The country you have entered is not in the list.”.
Simulation of Program 2:
USER’S INPUT PROGRAM’S OUTPUT
Philippines
Japan
Korea
China
Malaysia
Programming Challenge 2:
Create a Java program that will require the user to input any number from 1-12. The program will display
the name of the month that corresponds to the user’s input.
▪ If the user inputted number 1, it will display the sentence “January is the 1st month of the year”.
▪ If the user inputted number 2, it will display the sentence “February is the 2nd month of the year”.
▪ If the user inputted number 3, it will display the sentence “March is the 3rd month of the year”.
▪ If the user inputted number 4, it will display the sentence “April is the 4th month of the year”.
▪ If the user inputted number 5, it will display the sentence “May is the 5th month of the year”.
▪ If the user inputted number 6, it will display the sentence “June is the 6th month of the year”.
▪ If the user inputted number 7, it will display the sentence “July is the 7th month of the year”.
▪ If the user inputted number 8, it will display the sentence “August is the 8th month of the year”.
▪ If the user inputted number 9, it will display the sentence “September is the 9th month of the year”.
▪ If the user inputted number 10, it will display the sentence “October is the 10th month of the year”.
▪ If the user inputted number 11, it will display the sentence “November is the 11th month of the year”.
▪ If the user inputted number 12, it will display the sentence “December is the 12th month of the year”.
▪ If the user inputted any number that is outside the range, it will display the sentence “Our calendar
only has 12 months”.
Simulation of Program 2:
USER’S INPUT PROGRAM’S OUTPUT
1
2
45
8 | 8
USING CONDITIONAL STATEMENTS:
SWITCH STATEMENTS
Java Programming – Using Conditional Statements: Switch Statements

More Related Content

What's hot (20)

PPT
14. Query Optimization in DBMS
koolkampus
 
PPTX
Pointers in c++
Vineeta Garg
 
PPTX
Operators and expression in c#
Dr.Neeraj Kumar Pandey
 
PPT
Pointers C programming
Appili Vamsi Krishna
 
PPTX
Managing input and output operations in c
niyamathShariff
 
PPTX
Polymorphism and its types
Suraj Bora
 
PPTX
Relational operators
Graphic Era Hill University,Bhimtal
 
PDF
Chapter3 the relational data model and the relation database constraints part2
eidah20
 
PPTX
Exception Handling in object oriented programming using C++
Janki Shah
 
PPTX
Delegates and events in C#
Dr.Neeraj Kumar Pandey
 
PPT
C program
AJAL A J
 
PPT
Entity Relationship Diagram
Shakila Mahjabin
 
PPT
2. Entity Relationship Model in DBMS
koolkampus
 
PPTX
Dynamic memory allocation in c
lavanya marichamy
 
PPT
C Structures & Unions
Ram Sagar Mourya
 
PDF
Modular Programming in C
bhawna kol
 
PDF
Java I/O
Jussi Pohjolainen
 
PPTX
Deadlock and memory management -- Operating System
EktaVaswani2
 
PPT
Swing and AWT in java
Adil Mehmoood
 
PPTX
File in C language
Manash Kumar Mondal
 
14. Query Optimization in DBMS
koolkampus
 
Pointers in c++
Vineeta Garg
 
Operators and expression in c#
Dr.Neeraj Kumar Pandey
 
Pointers C programming
Appili Vamsi Krishna
 
Managing input and output operations in c
niyamathShariff
 
Polymorphism and its types
Suraj Bora
 
Chapter3 the relational data model and the relation database constraints part2
eidah20
 
Exception Handling in object oriented programming using C++
Janki Shah
 
Delegates and events in C#
Dr.Neeraj Kumar Pandey
 
C program
AJAL A J
 
Entity Relationship Diagram
Shakila Mahjabin
 
2. Entity Relationship Model in DBMS
koolkampus
 
Dynamic memory allocation in c
lavanya marichamy
 
C Structures & Unions
Ram Sagar Mourya
 
Modular Programming in C
bhawna kol
 
Deadlock and memory management -- Operating System
EktaVaswani2
 
Swing and AWT in java
Adil Mehmoood
 
File in C language
Manash Kumar Mondal
 

Similar to Java Programming - Conditional Statements (Switch).pdf (20)

PDF
CIS 1403 lab 4 selection
Hamad Odhabi
 
PDF
itft-Decision making and branching in java
Atul Sehdev
 
PPTX
Conditional Statement - Switch Case.pptx
DheromeIngenious1
 
PDF
Java input Scanner
Huda Alameen
 
PPTX
Switch statement
Patrick John McGee
 
PPT
Switch statements in Java
Jin Castor
 
PPTX
Introduction to Java
Ashita Agrawal
 
PPTX
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
DevaKumari Vijay
 
PPT
Final requirement
Faye Salosagcol
 
PPTX
MODULE_2_Operators.pptx
VeerannaKotagi1
 
PDF
Lecture 7 Control Statements.pdf
SalmanKhurshid25
 
PPTX
Javascript conditional statements 2
Jesus Obenita Jr.
 
PPTX
Java Decision Control
Jayfee Ramos
 
PPTX
SWITCH-CASE, Lesson Computer Programming.pptx
AlwinJamesPuracan
 
PPT
slides03.ppt
Anjali127411
 
PDF
201707 CSE110 Lecture 10
Javier Gonzalez-Sanchez
 
PDF
java notes.pdf
RajkumarHarishchandr1
 
PPTX
Java chapter 3
Abdii Rashid
 
PPTX
3. Java Installations ppt for engineering
vyshukodumuri
 
PPTX
Java learning
moew3
 
CIS 1403 lab 4 selection
Hamad Odhabi
 
itft-Decision making and branching in java
Atul Sehdev
 
Conditional Statement - Switch Case.pptx
DheromeIngenious1
 
Java input Scanner
Huda Alameen
 
Switch statement
Patrick John McGee
 
Switch statements in Java
Jin Castor
 
Introduction to Java
Ashita Agrawal
 
Unit 2-data types,Variables,Operators,Conitionals,loops and arrays
DevaKumari Vijay
 
Final requirement
Faye Salosagcol
 
MODULE_2_Operators.pptx
VeerannaKotagi1
 
Lecture 7 Control Statements.pdf
SalmanKhurshid25
 
Javascript conditional statements 2
Jesus Obenita Jr.
 
Java Decision Control
Jayfee Ramos
 
SWITCH-CASE, Lesson Computer Programming.pptx
AlwinJamesPuracan
 
slides03.ppt
Anjali127411
 
201707 CSE110 Lecture 10
Javier Gonzalez-Sanchez
 
java notes.pdf
RajkumarHarishchandr1
 
Java chapter 3
Abdii Rashid
 
3. Java Installations ppt for engineering
vyshukodumuri
 
Java learning
moew3
 
Ad

More from ROWELL MARQUINA (20)

PDF
WSAT Lesson 3 - Introduction to HTML.pdf
ROWELL MARQUINA
 
PDF
PLD Lesson 5 - Displaying Text and Comment on the Screen.pdf
ROWELL MARQUINA
 
PDF
COM Lesson 1 - Introduction to Computer Organization and Management.pdf
ROWELL MARQUINA
 
PDF
PROG3 Lesson 4 - Setting Up the VS Code Environment.pdf
ROWELL MARQUINA
 
PDF
PLD Lesson 4 - Setting Up the VS Code Environment.pdf
ROWELL MARQUINA
 
PDF
PLD Lesson 3 - Flowcharting Conditional Statements.pdf
ROWELL MARQUINA
 
PDF
FoR Lesson 1 - Introduction to Research.pdf
ROWELL MARQUINA
 
PDF
Lesson 3 - Understanding Professional Ethics.pdf
ROWELL MARQUINA
 
PDF
WSAT Lesson 2 - Introduction to World Wide Web.pdf
ROWELL MARQUINA
 
PDF
WSAT Lesson 1 - Introduction to Web Systems and Technologies.pdf
ROWELL MARQUINA
 
PDF
ITSP Lesson 2 - Ethical Principles and Theories.pdf
ROWELL MARQUINA
 
PDF
ITSP Lesson 1 - Ethics and Its History.pdf
ROWELL MARQUINA
 
PDF
WSAT Lesson 1 - Introduction to Web Systems and Technologies.pdf
ROWELL MARQUINA
 
PDF
Lesson 2 - Algorithm and Flowcharting.pdf
ROWELL MARQUINA
 
PDF
Introduction to Programming and Languages.pdf
ROWELL MARQUINA
 
PDF
QMMS Lesson 2 - Using MS Excel Formula.pdf
ROWELL MARQUINA
 
PPTX
QMMS Lesson 2 - Using Excel Formula.pptx
ROWELL MARQUINA
 
PDF
HCI Lesson 2 - Components of Human-Computer Interaction (HCI).pdf
ROWELL MARQUINA
 
PDF
HCI Lesson 1 - Introduction to Human-Computer Interaction.pdf
ROWELL MARQUINA
 
PDF
DS Lesson 2 - Subsets, Supersets and Power Set.pdf
ROWELL MARQUINA
 
WSAT Lesson 3 - Introduction to HTML.pdf
ROWELL MARQUINA
 
PLD Lesson 5 - Displaying Text and Comment on the Screen.pdf
ROWELL MARQUINA
 
COM Lesson 1 - Introduction to Computer Organization and Management.pdf
ROWELL MARQUINA
 
PROG3 Lesson 4 - Setting Up the VS Code Environment.pdf
ROWELL MARQUINA
 
PLD Lesson 4 - Setting Up the VS Code Environment.pdf
ROWELL MARQUINA
 
PLD Lesson 3 - Flowcharting Conditional Statements.pdf
ROWELL MARQUINA
 
FoR Lesson 1 - Introduction to Research.pdf
ROWELL MARQUINA
 
Lesson 3 - Understanding Professional Ethics.pdf
ROWELL MARQUINA
 
WSAT Lesson 2 - Introduction to World Wide Web.pdf
ROWELL MARQUINA
 
WSAT Lesson 1 - Introduction to Web Systems and Technologies.pdf
ROWELL MARQUINA
 
ITSP Lesson 2 - Ethical Principles and Theories.pdf
ROWELL MARQUINA
 
ITSP Lesson 1 - Ethics and Its History.pdf
ROWELL MARQUINA
 
WSAT Lesson 1 - Introduction to Web Systems and Technologies.pdf
ROWELL MARQUINA
 
Lesson 2 - Algorithm and Flowcharting.pdf
ROWELL MARQUINA
 
Introduction to Programming and Languages.pdf
ROWELL MARQUINA
 
QMMS Lesson 2 - Using MS Excel Formula.pdf
ROWELL MARQUINA
 
QMMS Lesson 2 - Using Excel Formula.pptx
ROWELL MARQUINA
 
HCI Lesson 2 - Components of Human-Computer Interaction (HCI).pdf
ROWELL MARQUINA
 
HCI Lesson 1 - Introduction to Human-Computer Interaction.pdf
ROWELL MARQUINA
 
DS Lesson 2 - Subsets, Supersets and Power Set.pdf
ROWELL MARQUINA
 
Ad

Recently uploaded (20)

PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PPTX
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
PDF
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
What’s my job again? Slides from Mark Simos talk at 2025 Tampa BSides
Mark Simos
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Mastering ODC + Okta Configuration - Chennai OSUG
HathiMaryA
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Automating Feature Enrichment and Station Creation in Natural Gas Utility Net...
Safe Software
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Peak of Data & AI Encore AI-Enhanced Workflows for the Real World
Safe Software
 
[Newgen] NewgenONE Marvin Brochure 1.pdf
darshakparmar
 

Java Programming - Conditional Statements (Switch).pdf

  • 1. 1 | 8 USING CONDITIONAL STATEMENTS: SWITCH STATEMENTS Java Programming – Using Conditional Statements: Switch Statements WHAT IS A CONDITIONAL STATEMENT? A conditional statement in Python programming is a type of statement that compares the values of two or more operands. An operand refers to the data that is used by the program either for comparison, manipulation, or mathematical operation. An operand can be: ▪ a value inputted by the user ▪ a value set by the programmer inside the program The results of the comparison using a conditional statement can result to either a TRUE or FALSE response. ▪ The TRUE response signifies that the values of operands are equal to one another or it is within the range provided by one of the operands. ▪ The FALSE response indicates that the values of operands are not equal to one another or it is outside the range provided by one of the operands. Programmers commonly use truth tables to determine whether the comparison between the data and the values set in the program will yield either a TRUE or FALSE response. The truth table is a visual representation that displays the inputted data, the set value, the argument, and the response. TRUTH TABLE Inputted Data Set Value Argument Response 8 5 8 > 5 True 20 20 20 != 20 False Yes Yes Yes == Yes True F f F == f False 25 15 25 >= 15 True In this lecture, we have to operands labelled as inputted data and set value. The “inputted data” refers to the data that was entered by the user in the program. The “set value” is the data that was specifically assigned in the program. The set value will serve as our basis for comparison that will check whether inputted data is equal to it or within its range. The “argument” is a statement that indicates how the operands are being compared to one another. Arguments in Python are expressed by using comparison operators. The comparison operators available in Python programming were as follows: COMPARISON OPERATOR DEFINITION SAMPLE STATEMENT == A comparison operator that checks if the operands are EQUAL to one another. a == b != A comparison operator that checks if the operands are NOT EQUAL to one another. a != b > A comparison operator that checks if value on the left side of the argument is GREATER THAN the value on the right side of the argument. a > b < A comparison operator that checks if value on the left side of the argument is LESS THAN the value on the right side of the argument. a < b >= A comparison operator that checks if value on the left side of the argument is GREATER THAN OR EQUAL TO the value on the right side of the argument. a >= b <= A comparison operator that checks if value on the left side of the argument is LESS THAN OR EQUAL TO the value on the right side of the argument. a <= b The “response” serves as the result of comparing the operands. The response can either be TRUE or FALSE based on the conditions that will determine whether the operands matches or does not match with one another.
  • 2. 2 | 8 USING CONDITIONAL STATEMENTS: SWITCH STATEMENTS Java Programming – Using Conditional Statements: Switch Statements PRACTICE WITH TRUTH TABLES Complete the table by filling up the Response Column with either TRUE or FALSE: TRUTH TABLE No. Inputted Data Set Value Argument Response 1 Rowell rowell Rowell == rowell 2 15 15 15 != 15 3 male Male male == Male 4 25 15 25 >= 15 5 1.3 4 1.3 <= 4 6 15 18 15 == 18 7 giraffe rhino giraffe == rhino 8 90 91 90 != 91 9 48 45 48 >= 45 10 107 92 107 < 92 USING SWITCH STATEMENT Now that we know how conditional statements work it is time for us to learn how to use one of the most basic form of conditional statements called switch statement. The switch statement is a type of conditional statement that is used to execute a block of code by comparing an operand to a specific value that was set in the program. The switch statement uses conditions that are labeled as cases. Each case is composed of the case, executable block of code and a break statement. Consider this example of a case: Case It refers to the statement that contains the condition. In the source code above, the line case “January”: details the condition where it asks if the operand presented to it is equal to January. Executable Block of Code The executable block of codes contains the instructions that the program will have to perform once the condition on the case matches the value of the operand. In our source code sample, the messages "The month is named after the Roman God, Janus." and "The presider of doors and beginnings." will be displayed on the screen if the operand is equal to January. If the operand fails to match the condition, the program will skip reading the executable block of code and jump directly to the next case. Break Statement The break statement indicates the end of the case. It signals the program to stop reading the next line of codes within the switch statement. If the program reached the line that contains the break; statement, the program will skip all other conditions and jump to the line of code that is outside the switch statement.
  • 3. 3 | 8 USING CONDITIONAL STATEMENTS: SWITCH STATEMENTS Java Programming – Using Conditional Statements: Switch Statements Sample Program 1: Create a Java program that will require the user to input any number from 1-3. The program will display an output statement based on these given conditions: ▪ If the user inputted number 1, it will display the sentence “You entered the number ONE.”. ▪ If the user inputted number 2, it will display the sentence “You entered the number TWO.”. ▪ If the user inputted number 3, it will display the sentence “You entered the number THREE.”. ▪ If the user inputted any number that is outside the range, it will display the sentence “The number you have entered is outside of the range given.”. Simulation of Program 1: USER’S INPUT PROGRAM’S OUTPUT 1 2 3 8 Source Code of Sample Program 1:
  • 4. 4 | 8 USING CONDITIONAL STATEMENTS: SWITCH STATEMENTS Java Programming – Using Conditional Statements: Switch Statements Let us examine the code: ▪ Line 1 contains the package name of our Java program named “switch_statement”. ▪ Line 2 is where we import the scanner in our program. In our previous sessions, we have learned that the scanner is a class in Java program that enables the program to get input from the user ▪ Line 4 contains the class name of our Java program named “Switch_Statement” while line 5 contains an open bracket ( { ) which indicates the beginning of the class. ▪ Line 7 contains the header of our main program while line 8 contains an open bracket ( { ) which indicates the beginning of the program body. ▪ On Line 9 we introduced our scanner to the program and assigned the name “mitis” to it. The new Scanner statement declares mitis as a new scanner in the program that takes in inputs from the user by using the System.in function. ▪ Line 10 is used to display message on the screen using the print ( ) function. The line explicitly instructed the program to show the message “Enter any number from 1-3” on the display screen. ▪ Line 11 displays the procedure that prepares the program to accept input from the user. The int num part of the statement declares the incoming input from the user as an integer (int) with the assigned name “num”. The mitis.nextInt( ) tells the program that the input will be captured using the scanner named mitis and that the input should be treated as an integer. ▪ Lines 12-26 details the procedure of using the switch statement.
  • 5. 5 | 8 USING CONDITIONAL STATEMENTS: SWITCH STATEMENTS Java Programming – Using Conditional Statements: Switch Statements • Line 12 displays the use of the function switch ( ). The identifier num that was enclosed in parentheses instructs the program that the value that was contained in num will be the value that will be used for the switch statement. The line 13 contains an open bracket ( { ) which indicates the beginning of the switch statement. • Line 14 contains the first condition of the program which checks if the value contained in num is equal to 1. If num is equal to one then the display message on line 15 will be shown on the screen. The line 16 contains the break statement which indicates the end of the first case. Note: If the value of num is not equal to 1, the program will skip reading lines 15 and 16 and automatically jumps to line 17. • Line 17 contains the second condition which checks if the value contained in num is equal to 2. If num is equal to two then the display message on line 18 will be shown on the screen. The line 19 contains the break statement which indicates the end of the second case. Note: If the value of num is not equal to 2, the program will skip reading lines 18 and 19 and automatically jumps to line 20. • Line 20 contains the third condition which checks if the value contained in num is equal to 3. If num is equal to three then the display message on line 21 will be shown on the screen. The line 22 contains the break statement which indicates the end of the third case. Note: If the value of num is not equal to 3, the program will skip reading lines 21 and 22 and automatically jumps to line 23. • Line 23 contains the default condition which accepts the input if it fails to match with any of the conditions written above it. If the program has reached this line of code it will automatically display the message on line 24. The line 22 contains the break statement which indicates the end of the default case. The line 26 contains an close bracket ( } ) which indicates the end of the switch statement.
  • 6. 6 | 8 USING CONDITIONAL STATEMENTS: SWITCH STATEMENTS Java Programming – Using Conditional Statements: Switch Statements Sample Program 2: Create a Java program that will require the user to choose an animal among the list of choices. The list of animals include dog, cat, bird, and fish. After the user have entered their choice of pet, the program will tell what personality do they have based on their chosen pet. ▪ If the user inputted dog, it will display the sentence “You have an outgoing personality.”. ▪ If the user inputted cat, it will display the sentence “You have a conservative personality.”. ▪ If the user inputted bird, it will display the sentence “You have caring personality.”. ▪ If the user inputted fish, it will display the sentence “You have a calm and stable personality.”. ▪ If the user inputted a name of an animal that is not included in the list, it will display the sentence “The animal you have entered is not in the list.”. Simulation of Program 2: USER’S INPUT PROGRAM’S OUTPUT dog cat bird fish chicken Source Code of Sample Program 2:
  • 7. 7 | 8 USING CONDITIONAL STATEMENTS: SWITCH STATEMENTS Java Programming – Using Conditional Statements: Switch Statements Programming Challenge 1: Create a Java program that will require the user to input a name of a country based on list of choices. The list includes Philippines, Japan, Korea, and China. After the user have entered their preferred country, the program will display the equivalent of “I love you” in the chosen language. ▪ If the user inputted Philippines, it will display the sentence “Mahal kita.”. ▪ If the user inputted Japan, it will display the sentence “Aishiteru”. ▪ If the user inputted Korea, it will display the sentence “Saranghaeyo.”. ▪ If the user inputted China, it will display the sentence “Wo ai ni”. ▪ If the user inputted a name of a country that is not included in the list, it will display the sentence “The country you have entered is not in the list.”. Simulation of Program 2: USER’S INPUT PROGRAM’S OUTPUT Philippines Japan Korea China Malaysia Programming Challenge 2: Create a Java program that will require the user to input any number from 1-12. The program will display the name of the month that corresponds to the user’s input. ▪ If the user inputted number 1, it will display the sentence “January is the 1st month of the year”. ▪ If the user inputted number 2, it will display the sentence “February is the 2nd month of the year”. ▪ If the user inputted number 3, it will display the sentence “March is the 3rd month of the year”. ▪ If the user inputted number 4, it will display the sentence “April is the 4th month of the year”. ▪ If the user inputted number 5, it will display the sentence “May is the 5th month of the year”. ▪ If the user inputted number 6, it will display the sentence “June is the 6th month of the year”. ▪ If the user inputted number 7, it will display the sentence “July is the 7th month of the year”. ▪ If the user inputted number 8, it will display the sentence “August is the 8th month of the year”. ▪ If the user inputted number 9, it will display the sentence “September is the 9th month of the year”. ▪ If the user inputted number 10, it will display the sentence “October is the 10th month of the year”. ▪ If the user inputted number 11, it will display the sentence “November is the 11th month of the year”. ▪ If the user inputted number 12, it will display the sentence “December is the 12th month of the year”. ▪ If the user inputted any number that is outside the range, it will display the sentence “Our calendar only has 12 months”. Simulation of Program 2: USER’S INPUT PROGRAM’S OUTPUT 1 2 45
  • 8. 8 | 8 USING CONDITIONAL STATEMENTS: SWITCH STATEMENTS Java Programming – Using Conditional Statements: Switch Statements