You are on page 1of 10

AREA: Computer Science DATE:

TOPIC: Conditions and Logical Operators in Java GRADE: 10th A B C D


TEACHER: Mr. Diego Tovar
STUDENT NAME:

Grade Motto: “I recognize


myself through my capabilities and difficulties within a decision-making
process and also assume the beginning of a new stage of my life.”

Performance standard # 1
Identifies structures, logical operators and other programming elements necessary in the
development of solutions to proposed situations.

Performance standard # 2
Proposes alternative solutions to problems, using simple logical structures and programming tools.

Introduction:

With this guide you will learn to evaluate and write conditional statements, a structure used across coding
languages. Conditional statements tell a program to carry out different actions depending on whether a
condition is true or false. You will first practice determining whether conditions are true and carrying out
actions accordingly. Then you will write your own conditional statements and evaluate statements to
predict the outcome of programs. Finally, you would create programs that would solve small situations
using conditions.

Activities: Take a minute to analyses the picture below.


If you where the person in the picture what road would you take? Based on what criteria would
you take this decision?

This
Thisschool
image year
is anwe will be working
illustration with a cycle
of the different or process
decisions called can
a program Software
take. Development Life Cycle
But base on what are
(SDLC).
these decisions taken. Conditions…. This is the answer. But what are conditions?
Conditional
• statements are part of our
Software Development everyday
Life Cycle?lives. As people, we can think about situations
and make decisions based on what we observe or know to be true. For example, “If it is raining
out, then we will have recess inside.” Or “If it is my birthday, then I can have a birthday cake.”
Conditions are cause and effect: “If this, then that.”
In a programming environment, conditions allow us to control what the program does and
perform different actions based on these “if, then” logic statements. What makes computer
programs great is the ability to interact with a user- this is only possible with conditions that direct
this type of interaction.

Using conditions in Java

Very often when you write code, you want to perform different actions for
different decisions.
You can use conditional statements in your code to do this.
In Java we have the following conditional statements:

• Use if to specify a block of code to be executed, if a specified condition is true


• Use else to specify a block of code to be executed, if the same condition is false
• Use else if to specify a new condition to test, if the first condition is false
• Use switch to specify many alternative blocks of code to be executed

The if Statement

Use the if statement to specify a block of Java code to be executed if a condition is true.

Syntax Example

if (condition) { if (hour < 18) {


block of code to be executed if greeting = "Good day";
the condition is true }
}
What you see in this image is called a flow
diagram or flowchart which let’s have a
diagrammatic representation of an algorithm.
In this case, we can see that, if the condition is
true it executes a block of code but if its false,
it doesn’t do anything.

The else Statement

Use the else statement to specify a block of code to be executed if the condition is false.

Syntax Example

if (condition) {
block of code to be executed if the if (hour < 18) {
condition is true greeting = "Good day";
} else { } else {
block of code to be executed if the greeting = "Good evening";
condition is false }
}

Note: If the hour is less than 18, create a "Good day" greeting, otherwise "Good evening":
In this case, we can see that, if the condition is
true it executes a block of code but different
from the example above, this flowchart has a
block of code if its false. Meaning that, in either
condition, it would execute something.

The else if Statement


Use the else if statement to specify a new condition if the first condition is false.

Syntax Example

if (condition1) {
block of code to be executed if condition1 if (time < 10) {
is true greeting = "Good morning";
} else if (condition2) { } else if (time < 20) {
block of code to be executed if the greeting = "Good day";
condition1 is false and condition2 is true } else {
} else { greeting = "Good evening";
block of code to be executed if the }
condition1 is false and condition2 is false
}

Note: If time is less than 10:00, create a "Good morning" greeting, if not, but time is less than 20:00,
create a "Good day" greeting, otherwise a "Good evening":
In this case, we can
see that, this
flowchart has
multiple conditions
and statements but
only one condition
can be true, letting
only one statement
execute.
For example, if
condition 1 is true
the other x
conditions must be
false.

Equality and Relational Operators

The equality and relational operators determine the relationship between two operands. It checks if an
operand is greater than, less than, equal to, not equal to and so on. Depending on the relationship, it
results to either true or false.
Java Example 1: A student wants to know the grade of his exam, but he only has the score. Create an
application which the user enters the exam’s score and the program shows the grade.

public static void main(String[] args) {


String grade;
double score, d;
score=Double.parseDouble(JOptionPane.showInputDialog("Please enter the score"));
if (score == 100) {
grade = “A”;
} else if (score >= 90) {
grade = “A”;
} else if (score >= 80) {
grade = “B”;
} else if (score >= 70) {
grade = “C”;
} else if (score >= 60) {
grade = “D”;
} else {
grade = “F”;
}
JOptionPane.showMessageDialog(null, "Your note is: " + grade);
}

Java Example 2: Create an application in which the user enters a number and the programs shows if is
positive, negative or 0.
public static void main(String[] args) {
String grade;
double score, d;
score=Double.parseDouble(JOptionPane.showInputDialog("Please enter the number"));
if (score >0) {
grade = “positive”;
} else if (score<0) {
grade = “negative”;
} else {
grade = “0”;
}
JOptionPane.showMessageDialog(null, "The number is: " + grade);
}

Activity 1: In your teams, create a java project in NetBeans and using java conditions, create a program
to solve each situation. Remember that each exercise must be in a different class. Your team will be
evaluate with a Rubric.

a) Write Java program to allow the user to input his/her age. Then the program will show if the person
is eligible to vote. A person who is eligible to vote must be older than or equal to 18 years old.

Enter your age: 18


You are eligible to vote.
b) Write a Java program that keeps a number from the user and generates an integer between 1
and 7 and displays the name of the weekday.

c) A shop will give discount of 10% if the cost of purchased quantity is more than 1000.
The program must show the value of the discount and the total cost that the user must pay.

Logical operators

If we’re talking about conditions, we must mention logical operators. Logical operators are used when
we want to check multiple conditions together.

Logical “AND” Operator

We can combine many relational expressions using “Logical And” operators. This operator is
represented by the symbol “&&”.

Consider the operation “operand1 && operand2”

Here operand1 and operand2 can be relational expressions or Boolean types. The result of the
operation “operand1 && operand2” will be

1. “true” only if both operand1 and operand2 are “true”

2. “false” if any of the operands (operand1 or operand2) is “false” or both the operands (operand1 or
operand2) are “false”.

Java Example: A school has following rules for grading system:

a. Below 25 - F
b. 25 to 45 - E
c. 45 to 50 - D
d. 50 to 60 - C
e. 60 to 80 - B
f. Above 80 – A
Ask user to enter marks and print the corresponding grade.

Solution

public static void main(String[] args){


int x;
x=Integer.parseInt(JOptionPane.showInputDialog("Please enter the score"));
if(x<25) {
JOptionPane.showMessageDialog (null, "F");
}
else if((x>=25) && (x<45)) {
JOptionPane.showMessageDialog (null, "E");
}
else if((x>=45) && (x<50)) {
JOptionPane.showMessageDialog (null, "D");
}
else if((x>=50) && (x<60)) {
JOptionPane.showMessageDialog (null, "C");
}
else if((x>=60) && (x<80)) {
JOptionPane.showMessageDialog (null, "B");
}
else if((x>=80) && (x<=100)) {
JOptionPane.showMessageDialog (null, "A");
}
else {
JOptionPane.showMessageDialog (null, "Not correct marks");
}
}

Logical “OR” Operator

We can combine many relational expressions using “Logical OR” operators. This operator is
represented by the symbol “||”.

Consider the operation “operand1 || operand2”

Here operand1 and operand2 can be relational expressions or boolean types. The result of the
operation “operand1 || operand2” will be

1. “true” if any of the operands (operand1 or operand2) are “true” or both of the operands (operand1 or
operand2) are “true”.

2. “false” only if both operand1 and operand2 are “false”


Java Example

public static void main(String[] args) {

int number1 = 1, number2 = 2, number3 = 9;


boolean result;

// At least one expression needs to be true for result to be true


result = (number1 > number2) || (number3 > number1);
// result will be true because (number3 > number1) is true
JOptionPane.showMessageDialog (null, result);

// All expression must be true from result to be true


result = (number1 > number2) && (number3 > number1);
// result will be false because (number1 > number2) is false
JOptionPane.showMessageDialog (null, result);
}
}

Activity 2:

a) Write a program to determine the cost of an automobile insurance premium, based on driver's
age and the number of accidents that the driver has had.

• The basic insurance charge is $500. There is a surcharge of $100 if the driver is under 25
and an additional surcharge for accidents:
# of accidents Accident Surcharge
1 50
2 125
3 225
4 375
5 575
6 or more No insurance

b) Prompt the user for a number and print good if the number is less than 5 but more than 0,
between 8 & 10 or greater than 33. Otherwise, print bad.
c) A worker needs to calculate his weekly salary, which is obtained as follows:
If you work 40 hours or less, you will be paid $ 16 per hour. If you work more than 40 hours you
will be paid $ 16 for each of the first 40 hours and $ 20 for each extra hour.
d) Ask the user for three numbers and show them ordered from highest to lowest.

Activity 3:

a) A pharmacy wants a program in which the user enters the value of the purchase and it
calculates the following: if the payment is made in "cash" and the total amount of the purchase
is greater than 500,000, the user has a discount of 5%; but if the payment is with "card" a
surcharge of 3% is added to the purchase value. Calculate and visualize the discount or
surcharge depending on the case and the total to be paid for the purchase.

b) A store gives his clients discounts base on the following conditions:

Value of the purchase Discount

$0 - $100 0%
$101 - $200 5%
$201 - $300 10%
More or equal to 301 15%

c) Create a program which ask the user for a number between 0 - 9999 and tells him/her how
many digits it has.
d) A worker is deducted from his salary a 15% if his salary is less than or equal to 1000. If his
salary is above 1000 and up to 2000 a 25% is deducted, and above 2000 a 30%. Calculate the
discount and the salary the worker will received.

WEBLIOGRAPHY:
http://homepages.uc.edu/~thomam/Intro_OOP_Text/Intro_Prog_Lang.html
https://www.britannica.com/technology/computer-programming-language
https://www.youtube.com/watch?v=YV6ykfG1nQY
https://www.youtube.com/watch?v=i-QyW8D3ei0
https://www.khanacademy.org/computing/computer-science/how-computers-work2/v/khan- academy-and-codeorg-
what-makes-a-computer-a-computer
https://www.khanacademy.org/computing/computer-science/how-computers-work2/v/khan- academy-and-codeorg-
hardware-and-software
https://www.khanacademy.org/computing/computer-programming/programming/intro-to-
programming/v/programming-intro

You might also like