0/30 Days Completed 0 Learning Days
Day 3 of 30 · Week 1 — Java Foundations
Operators
1 Learn
Operators do work on values. Arithmetic: + - * / %. The % operator gives the remainder.
Relational operators compare values and give true or false: == != > < >= <=.
Logical operators combine conditions: && (and), || (or), ! (not). Assignment operators store values: =, +=, -=.
Careful: dividing two ints gives an int. 7 / 2 is 3, not 3.5.
2 See the Code
Java
public class Ops {
public static void main(String[] args) {
int a = 7, b = 2;
System.out.println(a / b);
System.out.println(a % b);
System.out.println(a > b && b > 0);
int score = 10;
score += 5;
System.out.println(score);
}
}- 7 / 2 is integer division, so the result is 3.
- 7 % 2 is the remainder, 1.
- a > b && b > 0 — both are true, so the result is true.
- score += 5 means score = score + 5.
Output
3 1 true 15
3 Practice
- Write a program that checks whether a number is even using % and prints true or false.
Try it in your own Java setup or write it in the Practice Notebook.
4 Quick Check
1. What is 10 % 3?
2. Which operator means AND?
3. What does == do?
Step 5
Answer and check the Quick Check above to unlock this.