0% found this document useful (0 votes)
11 views

Operation and Condition in Java

Uploaded by

Axel Mendoza
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Operation and Condition in Java

Uploaded by

Axel Mendoza
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Operators and COndition

in Java
• In Java, operators are symbols that perform
operations on variables and values. They are
essential for carrying out arithmetic, logical,
and relational tasks within programs.
In the example below, we use the + operator to
add together two values:

int x = 100 + 20;

Although the + operator is often used to add together two


values, like in the example above, it can also be used to
add together a variable and a value, or a variable and
another variable:
In the example below, we use the + operator to
add together two values:

int sum1 = 100 + 50;


int sum2 = sum1 + 250; //400
int sum3 = sum2 + sum1; //550
Java divides the operators into the following
groups:

·Arithmetic operators
·Assignment operators
·Comparison operators
·Logical operators
·Bitwise operators
JAVA COMPARISON OPERATORS
Comparison operators are used to compare two
values (or variables). This is important in
programming, because it helps us to find
answers and make decisions. The return value of
a comparison is either true or false. These values
are known as Boolean values.
JAVA COMPARISON OPERATORS
Conditional Statements in Java

Everyday in our life we make decisions whether


our decisions are good or bad, it all depends on
the conditions that we make. The same is also
true in programming. We use conditions to make
our programs intelligent enough to give us the
right results when we needed them most.
Conditional Statements
This is a programming language statement that selects an execution path based on
whether some condition is true or false. It also describes some action or operation
that takes place based on whether or not a certain condition is true.
In Java, there are mainly three types of conditional statements:
1. if statement: Executes a block of code if a specified condition is true.
2. if-else statement: Executes one block of code if the condition is true and
another block if the condition is false.
3. if-else-if ladder: Executes different blocks of code for multiple conditions.
USING THE IF STATEMENT

if (condition) {
// Code block to execute if condition is
true
}
Here is an example:

int num = 10;


if (num > 0) {
System.out.println("The number is positive.");
}

In this example, if the value of num is greater than 0, the message "The number is
positive." will be printed.
USING THE IF-ELSE STATEMENT
The if-else statement allows you to execute one block of code if the condition is true,
and another block if the condition is false. Its syntax is:

if (condition) {
// Code block to execute if condition is true
} else {
// Code block to execute if condition is false
}
Here is an example:
int num = -5;
if (num > 0) {
System.out.println("The number is positive.");
} else {
System.out.println("The number is non-positive.");
}

In this example, if the value of num is greater than 0, the message "The number is positive."
will be printed; otherwise, "The number is non-positive." will be printed.
USING THE IF-ELSE-IF LADDER
When you have multiple conditions to check, you can use an if-else-if ladder. It allows you to
check multiple conditions and execute different code blocks accordingly. Here's the syntax:

if (condition1) {
// Code block to execute if condition1 is true
} else if (condition2) {
// Code block to execute if condition2 is true
} else {
// Code block to execute if none of the conditions are true
}
Example:
int num = 0;
if (num > 0) {
System.out.println("The number is positive.");
} else if (num < 0) {
System.out.println("The number is negative.");
} else {
System.out.println("The number is zero.");
}

In this example, if the value of num is greater than 0, "The number is positive." will be printed; if it's less
than 0, "The number is negative." will be printed; otherwise, "The number is zero." will be printed.
Skill Building Activity 2

Design a program which determines if the given age of


the user is qualified to vote or not. The qualifying age is
18 years old and above.
Skill Building Activity 2

Design a Java program that takes


an entered age and determines
which generation it falls under.
Skill Building Activity 3
Activity: Guess the Number Game
Description: Create a Java program that generates a random number between 1 and 100 and asks the user to guess the
number. After each guess, the program should provide feedback to the user whether their guess was too high, too low, or
correct. The game should continue until the user correctly guesses the number.
Requirements:
1.Generate a random number between 1 and 100.
2.Prompt the user to guess the number.
3.Compare the user's guess with the generated number and provide appropriate feedback.
4.Continue prompting the user for guesses until they correctly guess the number.
5.Display the number of attempts it took the user to guess correctly.
Intermediate Concepts Covered:
•User input (using Scanner class)
•Conditional statements (if, else if, else)
•Random number generation
Skill Building Activity 3
Activity: Guess the Number Game
Description: Create a Java program that generates a random number between 1 and 100 and asks the user to guess the
number. After each guess, the program should provide feedback to the user whether their guess was too high, too low, or
correct. The game should continue until the user correctly guesses the number.
Requirements:
1.Generate a random number between 1 and 100.
2.Prompt the user to guess the number.
3.Compare the user's guess with the generated number and provide appropriate feedback.
4.Continue prompting the user for guesses until they correctly guess the number.
5.Display the number of attempts it took the user to guess correctly.
Intermediate Concepts Covered:
•User input (using Scanner class)
•Conditional statements (if, else if, else)
•Random number generation
Skill Building Activity 3
import java.util.Scanner; import java.util.Random; public class
GuessTheNumber { public static void main(String[] args) { Scanner
scanner = new Scanner(System.in); Random random = new Random(); int
secretNumber = random.nextInt(100) + 1; int attempts = 0; int guess;
System.out.println("Welcome to Guess the Number Game!"); do
{ System.out.print("Enter your guess (between 1 and 100): "); guess =
scanner.nextInt(); attempts++; if (guess < secretNumber)
{ System.out.println("Too low! Try again."); } else if (guess >
secretNumber) { System.out.println("Too high! Try again."); } else
{ System.out.println("Congratulations! You guessed the number " +
secretNumber + " correctly!"); System.out.println("Number of attempts: "
+ attempts); } } while (guess != secretNumber); scanner.close(); } }
Skill Building Activity 4
Activity: ATM Transaction Simulator
Description: Develop a Java program that simulates basic ATM transactions. The program should provide options for users to perform actions
such as checking their account balance, depositing funds, and withdrawing funds. It should continue to prompt the user for actions until they
choose to exit.
Requirements:
1.Implement a simple menu system where users can choose from the following options:
•Check balance
•Deposit funds
•Withdraw funds
•Exit
2.Use conditional statements (if, else if, else) to handle each option selected by the user.
3.For the deposit and withdrawal options, prompt the user to enter the amount and update the account balance accordingly.
4.Ensure appropriate error handling for invalid input, such as non-numeric values or attempting to withdraw more funds than available.
5.Display the account balance after each transaction.
Intermediate Concepts Covered:
•User input (using Scanner class)
•Conditional statements (if, else if, else)
•Arithmetic operations
•Error handling
Nested If Statement

You can have if statements inside other


if statements.
Switch Statement
This statement allows you to execute
different blocks of code based on the
value of an expression.

You might also like