KEMBAR78
Java Programming - Conditional Statements (Switch).pdf
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

Java Programming - Conditional Statements (Switch).pdf

  • 1.
    1 | 8 USINGCONDITIONAL 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 USINGCONDITIONAL 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 USINGCONDITIONAL 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 USINGCONDITIONAL 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 USINGCONDITIONAL 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 USINGCONDITIONAL 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 USINGCONDITIONAL 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 USINGCONDITIONAL STATEMENTS: SWITCH STATEMENTS Java Programming – Using Conditional Statements: Switch Statements