NCP2210 LabAct2
NCP2210 LabAct2
LABORATORY ACTIVITY #2
DECISION STRUCTURES
NAME: CRUZ, JOSH CLARENCE F. DR. JOAN P. LAZARO
SECTION: BSME2 DATE: 3/23/22 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 2210 (Computer Fundamentals and Programming)
2|Page
NCP 2210 (Computer Fundamentals and Programming)
Observation:
The system has a pop-up prompt asking for average and it has a system that calculates the
average and has a following response about the grade you have and it is an automated response if
your grade is higher or greater than 95.
The if-else Statement
The if-else statement will execute one group of statements if its boolean expression is
true, or another group if its boolean expression is false.
The if-else statement is an expansion of the if statement. Here is its format:
if (BooleanExpression)
statement or block;
else
statement or block;
3|Page
NCP 2210 (Computer Fundamentals and Programming)
Example Program #2
package example;
/**
* @author Joan Lazaro
*/
import java.util.Scanner; // Needed for the Scanner class
4|Page
NCP 2210 (Computer Fundamentals and Programming)
Observation:
My observation on this experiment is that if one number is “0” the prompt will
automatically ask for another number. And, the formula is simple and it just finds the quotient of
two numbers.
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) {
5|Page
NCP 2210 (Computer Fundamentals and Programming)
Observation:
I’ve noticed that if your annual salary is not equal or higher than 30,000 you will not
qualify for the loan.
The if-else-if Statement
The if-else-if statement tests a series of conditions. It is often simpler to test a series of
conditions with the if-else-if statement than with a set of nested if-else statements.
The if-else-if statement makes certain types of nested decision logic simpler to write.
Here is the general format of the if-else-if statement:
if (expression_1) {
statement
statement
etc.
}
else if (expression_2) {
statement
statement
etc.
}
Insert as many else if clauses as necessary
else {
statement
statement
6|Page
NCP 2210 (Computer Fundamentals and Programming)
etc.
}
When the statement executes, expression_1 is tested. If expression_1 is true, the block of
statements that immediately follows is executed, and the rest of the structure is ignored. If
expression_1 is false, however, the program jumps to the next else if clause and tests
expression_2. If it is true, the block of statements that immediately follows is executed, and then
the rest of the structure is ignored. This process continues, from the top of the structure to the
bottom, until one of the expressions is found to be true. If none of the expressions are true, the
last else clause takes over and the block of statements immediately following it is executed.
The last else clause, which does not have an if statement following it, is referred to as the
trailing else. The trailing else is optional, but in most cases, you will use it.
Example Program #4
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.");
else if (testScore < 80)
JOptionPane.showMessageDialog(null, "Your grade is C.");
else if (testScore < 90)
JOptionPane.showMessageDialog(null, "Your grade is B.");
else
JOptionPane.showMessageDialog(null, "Your grade is A.");
System.exit(0);
}
}
Result: Result: (2 test case)
7|Page
NCP 2210 (Computer Fundamentals and Programming)
Observation:
The numerical grade that you input in the prompt has a specific answer and that is your
grade in alphabet. The system has a formula that has a prompt based on what is greater to the
grade you gave.
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.");
else if (testScore < 80)
JOptionPane.showMessageDialog(null, "Your grade is C.");
else if (testScore < 90)
JOptionPane.showMessageDialog(null, "Your grade is B.");
else if (testScore <= 100)
JOptionPane.showMessageDialog(null, "Your grade is A.");
else
JOptionPane.showMessageDialog(null, "Invalid score.");
System.exit(0);
}
}
8|Page
NCP 2210 (Computer Fundamentals and Programming)
Observation:
This one differs from lab2_4 only on having a grade of A you need to hav a grade that is
equal or less than 100 but not less than 90.
The if-else-if Statement vs nested Decision Structure
You never have to use the if-else-if statement because its logic can be coded with nested
if-else statements. However, a long series of nested if-else statements has two particular
disadvantages when you are debugging code:
The code can grow complex and become difficult to understand.
Because indenting is important in nested statements, a long series of nested if-else
statements can become too long to be displayed on the computer screen without
horizontal scrolling. Also, long statements tend to “wrap around” when printed on paper,
making the code even more difficult to read.
The logic of an if-else-if statement is usually easier to follow than that of a long series of
nested if-else statements. And, because all of the clauses are aligned in an if-else-if statement, the
lengths of the lines in the statement tend to be shorter.
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.
Operator Meaning Effect
9|Page
NCP 2210 (Computer Fundamentals and Programming)
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);
Observation:
The prompt simply states that if your salary is greater or equal to 30k and the years of
you working should be equal or greater than 2 years. If you do not obtain neither of the two
requirements you are most likely unqualified.
Example Program #7
10 | P a g e
NCP 2210 (Computer Fundamentals and Programming)
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;
Observation:
EXAMPLE PROGRAM 7 IS JUST THE SAME AS 6. BUT IF YOU JUST GET ONE
(the salary or the year on the job) you can still qualify.
11 | P a g e
NCP 2210 (Computer Fundamentals and Programming)
Remember that a String object is referenced by a variable that contains the object’s
memory address. When you use a relational operator with the reference variable, the operator
works on the memory address that the variable contains, not the contents of the String object.
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.");
}
}
}
Observation:
I think that this program simply compares 2 variables and compares it if they have a
similarity.
12 | P a g e
NCP 2210 (Computer Fundamentals and Programming)
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)
{
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);
}
}
}
Observation:
13 | P a g e
NCP 2210 (Computer Fundamentals and Programming)
This program is specific when it is giving its data, since both names have a corresponding
numerical value because of the coding. It will answer that name 1 is greater than name 2.
Ignoring Case in String Comparisons
The equals and compareTo methods perform case-sensitive comparisons, which means
that uppercase letters are not considered the same as their lowercase counterparts. In other words,
“A” is not the same as “a”. This can obviously lead to problems when you want to perform case-
insensitive comparisons.
The String class provides the equalsIgnoreCase and compareToIgnoreCase methods.
These methods work like the equals and compareTo methods, except the case of the characters in
the strings is ignored.
14 | P a g e
NCP 2210 (Computer Fundamentals and Programming)
Observation:
There are two prompts in this program, one is, the rejection and one is when you answer
the correct answer that the system is looking for.
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
15 | P a g e
NCP 2210 (Computer Fundamentals and Programming)
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.
16 | P a g e
NCP 2210 (Computer Fundamentals and Programming)
17 | P a g e
NCP 2210 (Computer Fundamentals and Programming)
Observation:
In this program the system will prompt you an answer that you did not answer it right
when you don’t have the answer they are looking for. If your answer is not 1,2 or 3. The system
will not accept your answer.
18 | P a g e