Today you will learn:
By the end, you will be able to perform calculations and make decisions in Java.
Arithmetic operators are used to perform calculations.
+ → addition- → subtraction* → multiplication/ → division% → remainder (modulo)Example:
int a = 10;
int b = 3;
System.out.println("Sum: " + (a + b));
System.out.println("Remainder: " + (a % b));
Assignment operators are used to assign values to variables.
= → assign value+= → add and assign-= → subtract and assign*= → multiply and assign/= → divide and assignExample:
int x = 5;
x += 3; // x is now 8
These operators compare values.
== → equal!= → not equal> → greater than< → less than>= → greater or equal<= → less or equalExample:
int age = 18;
if (age >= 18) {
System.out.println("You are an adult");
}
Logical operators combine conditions.
Both conditions must be true.
if (age >= 18 && age < 65) {
System.out.println("Working age");
}
At least one condition must be true.
if (age < 18 || age > 65) {
System.out.println("Not typical working age");
}
Reverses a condition.
boolean isStudent = false;
if (!isStudent) {
System.out.println("Not a student");
}
int a = 10;
int b = 3;
System.out.println("Sum: " + (a + b));
System.out.println("Remainder: " + (a % b));
Write a program that checks if a number is even or odd.
Steps:
%Example:
public class EvenOdd {
public static void main(String[] args) {
int number = 7;
if (number % 2 == 0) {
System.out.println("The number is even");
} else {
System.out.println("The number is odd");
}
}
}