Professional Documents
Culture Documents
LABORATORY ACTIVITY #2
DECISION STRUCTURES
NAME: Zapanta, Angelo Miguel DR. JOAN P. LAZARO
SECTION: 1CPE DATE: 09/10/21 GRADE:
The if Statement
The if statement is used to create a decision structure, which allows a program to have
more than one path of execution. The if statement causes one or more statements to execute only
when a boolean expression is true.
In a decision structure’s simplest form, a specific action is taken only when a condition
exists. If the condition does not exist, the action is not performed. The flowchart in the figure
below shows the logic of a decision structure. The diamond symbol represents a yes/no question
or a true/false condition. If the answer to the question is yes (or if the condition is true), the
program flow follows one path, which leads to an action being performed. If the answer to the
question is no (or the condition is false), the program flow follows another path, which skips the
action.
One way to code a decision structure in Java is with the if statement. Here is the general
format of the if statement:
if (BooleanExpression) {
statement;
}
The if statement is simple in the way it works: The BooleanExpression that appears
inside the parentheses must be a boolean expression. A boolean expression is one that is either
true or false. If the boolean expression is true, the very next statement is executed. Otherwise, it
is skipped. The statement is conditionally executed because it executes only under the condition
that the expression in the parentheses is true.
Relational Operators
1|Page
NCP 3106 (Software Design Laboratory)
2|Page
NCP 3106 (Software Design Laboratory)
Result:
Observation:
For the first program in our activity, we are dealing with the If statement. The program
lets the user to input 3 scores that will be averaged by the program then if the result is greater
than 95, then a message will appear by the program that indicates a great score.
3|Page
NCP 3106 (Software Design Laboratory)
Example Program #2
package example;
/**
* @author Joan Lazaro
*/
import java.util.Scanner; // Needed for the Scanner class
4|Page
NCP 3106 (Software Design Laboratory)
Result:
Observation:
This program lets the user to enter 2 numbers and then the program divides the first
entered number by the second number. If the second number is “0”, a message will appear that it
requires to be other than zero to be able to divide it. Otherwise, the quotient will be displayed
along with its divisor and dividend.
5|Page
NCP 3106 (Software Design Laboratory)
Nested if Statements
To test more than one condition, an if statement can be nested inside another if statement.
Example Program #3
package example;
/**
* @author Joan Lazaro
*/
import javax.swing.JOptionPane; // Needed for JOptionPane class
public class Example
{
public static void main(String[] args)
{
double salary;
double yearsOnJob;
String input;
input = JOptionPane.showInputDialog("Enter your annual salary.");
salary = Double.parseDouble(input);
input = JOptionPane.showInputDialog("Enter the number of years at your
current job.");
yearsOnJob = Double.parseDouble(input);
if (salary >= 30000) {
if (yearsOnJob >= 2) {
6|Page
NCP 3106 (Software Design Laboratory)
Result:
Observation:
For this item, the program asks the user to input his/her annual salary along with the
number of years at his/her current job. If the salary and number of years currently on the job is at
least 30000 and 2, respectively. A message will show that the user is qualified for the loan. Else,
a message will show that the user needs to earn at least 30000 in his/her annual salary and at
least 2 years at his/her current job.
7|Page
NCP 3106 (Software Design Laboratory)
8|Page
NCP 3106 (Software Design Laboratory)
Result:
Observation:
For this program, the program asks a user to input a score that varies the result depending
on the value that was given by the user. We also used the “if-else-if” statement to be able to have
different output depending on the user’s input.
The trailing else clause, which appears at the end of the if-else-if statement, is optional,
but in many situations, you will use it to catch errors. Example Program #4 will assign the grade
'A' to any test score that is 90 or greater. What if the highest possible test score is 100? We can
modify the code as shown in Example Program #5 so the trailing else clause catches any value
greater than 100 and displays an error message.
Example Program #5
package example;
/*
* @author Joan Lazaro
*/
import javax.swing.JOptionPane; // Needed for JOptionPane
public class Example
{
public static void main(String[] args)
{
int testScore;
String input;
input = JOptionPane.showInputDialog("Enter your numeric test score and
I will tell you the grade: ");
testScore = Integer.parseInt(input);
if (testScore < 60)
JOptionPane.showMessageDialog(null, "Your grade is F.");
else if (testScore < 70)
JOptionPane.showMessageDialog(null, "Your grade is D.");
9|Page
NCP 3106 (Software Design Laboratory)
Result:
Observation:
This program is somehow similar from the previous one but the difference is that any
value greater than 100 will still have a grade of A. In addition to that, an error message appears if
ever the value was not covered by the program.
10 | P a g e
NCP 3106 (Software Design Laboratory)
Logical operators
Logical operators connect two or more relational expressions into one or reverse the logic
of an expression.
Java provides two binary logical operators, AND (&&) and OR (||), which are used to
combine two boolean expressions into a single expression. It also provides the unary operator
NOT (!), which reverses the truth of a boolean expression.
Operato Meaning Effect
r
Connects two boolean expressions into one. Both
&& AND
expressions must be true for the overall expression to be true.
Connects two boolean expressions into one. One or both
expressions must be true for the overall expression to be true.
|| OR
It is only necessary for one to be true, and it does not matter
which one.
The NOT (!) operator reverses the truth of a boolean
expression. If it is applied to an expression that is true, the
! NOT
operator returns false. If it is applied to an expression that is
false, the operator returns true.
Example Program #6
package example;
/*
* @author Joan Lazaro
*/
import javax.swing.JOptionPane; // Needed for JOptionPane class
public class Example
{
public static void main(String[] args)
{
double salary;
double yearsOnJob;
String input;
input = JOptionPane.showInputDialog("Enter your annual salary.");
salary = Double.parseDouble(input);
11 | P a g e
NCP 3106 (Software Design Laboratory)
}
}
Result:
Observation:
For this program, we can tell that it is somehow similar from the third program due to the
inputs and outputs. But as I observe the program, it was shorter and simpler due to the use of
logical operators which can be symbolized as “&&”. The program connects two Boolean
expressions into one. Both expressions must be true to produce the desired output by the
program.
12 | P a g e
NCP 3106 (Software Design Laboratory)
Example Program #7
package example;
/*
* @author Joan Lazaro
*/
import javax.swing.JOptionPane; // Needed for JOptionPane class
public class Example
{
public static void main(String[] args)
{
double salary;
double yearsOnJob;
String input;
Result:
13 | P a g e
NCP 3106 (Software Design Laboratory)
Observation:
This program is somehow similar from the previous activity. They both use the logical
operators that connects two Boolean expressions into one. The program asks the user to input
his/her annual salary along with the years at the current job. If the annual salary is at least 30000
and the number of years is at least 2, then the user is qualified for the loan. Else the program tells
the user that he/she does not qualify for the loan.
Example Program #8
package example;
/*
* @author Joan Lazaro
*/
public class Example
{
public static void main (String[] args)
{
String name1 = "Mark",
name2 = "Mark",
name3 = "Mary";
if (name1.equals(name2)) {
System.out.println(name1 + " and " + name2 + " are the same.");
}
else {
System.out.println(name1 + " and " + name2 + " are NOT the
same.");
}
if (name1.equals(name3)) {
System.out.println(name1 + " and " + name3 + " are the same.");
}
else {
System.out.println(name1 + " and " + name3 + " are NOT the
same.");
}
}
}
14 | P a g e
NCP 3106 (Software Design Laboratory)
Result:
Observation:
This program is somehow different due to the type of input that requires by the program.
Commonly, we are asked to input numbers or at least one character. For this case, the objects are
already declared which are strings. The program compares the strings if the characters are the
same, considering the case sensitive of the strings. It uses if-else statements to satisfy the
program.
The String class also provides the compareTo method, which can be used to determine
whether one string is greater than, equal to, or less than another string. The general form of the
method is as follows:
StringReference.compareTo(OtherString)
StringReference is a variable that references a String object, and OtherString is either
another variable that references a String object or a string literal. The method returns an integer
value that can be used in the following manner:
If the method’s return value is negative, the string referenced by StringReference
(the calling object) is less than the OtherString argument.
If the method’s return value is 0, the two strings are equal.
If the method’s return value is positive, the string referenced by StringReference
(the calling object) is greater than the OtherString argument.
Example Program #9
package example;
/*
* @author Joan Lazaro
*/
public class Example
{
public static void main(String[] args)
15 | P a g e
NCP 3106 (Software Design Laboratory)
{
String name1 = "Mary",
name2 = "Mark";
if (name1.compareTo(name2) < 0) {
System.out.println(name1 + " is less than " + name2);
}
else if (name1.compareTo(name2) == 0) {
System.out.println(name1 + " is equal to " + name2);
}
else if (name1.compareTo(name2) > 0) {
System.out.println(name1 + " is greater than " + name2);
}
}
}
Result:
Observation:
This program uses a compareTo function to compare strings from one another. In this
case, the string Mary is greater than Mark due to the Unicode value that the string “Mary”
contains is greater than the string “Mark”. The string “Mark” contains a value of 395, while the
string "Mary" contains a value of 409.
16 | P a g e
NCP 3106 (Software Design Laboratory)
Result:
Observation:
For this program we are dealing with case sensitivity. There is a specific string declared
in the program that the user needs to input during the simulation. In this case, the ignoreCase
function ignores the case sensitivity that was declared in the program. With this function, even if
the case sensitivity are different but the characters contains the same length and characters in the
same position, the program output proceeds to be true.
17 | P a g e
NCP 3106 (Software Design Laboratory)
The first line of the statement starts with the word switch, followed by a testExpression,
which is enclosed in parentheses. The testExpression is a variable or an expression that gives a
char, byte, short, int, or string value. (If you are using a version of Java prior to Java 7, the
testExpression cannot be a string.)
Beginning at the next line is a block of code enclosed in curly braces. Inside this block of
code is one or more case sections. A case section begins with the word case, followed by a value,
followed by a colon. Each case section contains one or more statements, followed by a break
statement. After all of the case sections, an optional default section appears. When the switch
18 | P a g e
NCP 3106 (Software Design Laboratory)
statement executes, it compares the value of the testExpression with the values that follow each
of the case statements (from top to bottom). When it finds a case value that matches the
testExpression’s value, the program branches to the case statement.
The statements that follow the case statement are executed until a break statement is
encountered. At that point, the program jumps out of the switch statement. If the testExpression
does not match any of the case values, the program branches to the default statement and
executes the statements that immediately follow it.
19 | P a g e
NCP 3106 (Software Design Laboratory)
Result:
Observation:
For the last program in our experiment, we are dealing with the switch function. Unlike
to the “if-else” function which specifies an action, this focuses on the change control flow of
program depending on the entered value. The program asks the user to enter any number among
1, 2, or 3. If the entered number is either 1, 2, or 3, the output simply tells which number was
entered. Otherwise, the output tells that it is not 1, 2, or 3.
20 | P a g e