Source: CS 101, Intro to Computer Science, UIUC
Tags: Java comparisons, == vs =, if else, conditional statements, boolean logic, && AND, || OR, compound comparisons, Java control flow
Difficulty: Beginner | Prerequisites: Java Variables, Types, and Literals (Lectures 1 & 2 notes). You need to understand variable declaration, assignment, and the boolean type.
Once you can store values in variables, the next question is: how does your program make decisions? This is where comparisons and conditional statements come in. They let a program choose different paths depending on the data. Nearly every useful program contains at least one conditional, so this is the mechanism that turns a flat sequence of instructions into something that can actually respond to input. If you are comfortable with variables and types, you are ready for this.
Java uses == to test equality (not =, which is assignment). You can combine comparisons with && (AND) and || (OR) to build compound conditions. The if/else structure lets your program execute different blocks of code depending on whether a condition is true or false.
Comparison operator
A symbol that compares two values and returns a boolean result (true or false). The basic set: <, >, <=, >=, ==, !=.
In simple terms, these are the questions you ask about your data: is this bigger, smaller, or the same?
== (equality operator)
Tests whether two values are equal. Returns true if they are, false if they are not.
Think of it as asking "are these the same?" This is different from =, which means "set this to that."
= (assignment operator)
Sets a variable to a value. A = 10 stores 10 in A. It does not compare anything.
In simple terms, = is a command ("store this"), while == is a question ("is this equal?").
&& (logical AND)
A compound operator that returns true only if both sides are true.
Think of it as "this AND that must both be true."
|| (logical OR)
A compound operator that returns true if at least one side is true.
Think of it as "this OR that (or both) must be true."
if statement
A control structure that executes a block of code only when its condition evaluates to true.
In simple terms, it is the program saying "only do this part if the condition holds."
else clause
The fallback block that executes when the if condition is false.
Think of it as "otherwise, do this instead."
else if
An additional condition checked when the preceding if was false. Allows chaining multiple conditions in sequence.
In simple terms, it is a second (or third, or fourth) question the program asks if the first answer was no.
This is the single most important distinction in this lecture:
A = 10 assigns the value 10 to A
A == 10 tests whether A is currently equal to 10, and returns true or false
Confusing the two is one of the most common beginner errors and will produce bugs that are hard to spot.
Java provides six comparison operators, all of which return a boolean:
< less than
> greater than
<= less than or equal to
>= greater than or equal to
== equal to
!= not equal to
You can combine multiple conditions into a single expression:
&& (AND): both conditions must be true for the whole expression to be true
|| (OR): at least one condition must be true for the whole expression to be true
Example:
int first = 10;
System.out.println((first > 0 && first < 10) || (first == 10));
Step-by-step:
first > 0 evaluates to true (10 is greater than 0)
first < 10 evaluates to false (10 is not less than 10)
true && false evaluates to false
first == 10 evaluates to true
false || true evaluates to true
Output: true
The if/else structure lets your program branch:
int first = 10;
if (first > 20) {
System.out.println("First is greater than 20");
} else {
System.out.println("First is not greater than 20");
}
Because first is 10, the condition first > 20 is false
The else block runs, printing: First is not greater than 20
When you need to test more than two possibilities, chain else if blocks:
int first = 10;
int second = 15;
int third = 4;
if (first >= second && first >= third) {
System.out.println(first);
} else if (second >= first && second >= third) {
System.out.println(second);
} else if (third >= first && third >= second) {
System.out.println(third);
}
This finds and prints the maximum of three values
Only one block executes: the first whose condition is true
Output: 15
No mathematical formulas here, but remember the evaluation order for compound expressions:
(condition1 && condition2) || condition3
Java evaluates && before || (AND binds more tightly than OR). Use parentheses to make your intended grouping explicit and avoid mistakes.
Conditional logic is the backbone of every interactive system. When your bank app checks whether your balance is sufficient before approving a transaction, that is an if statement. When a login page checks whether the username AND password are both correct, that is &&. Understanding how to compose and evaluate these conditions is a skill you will use in every program you write.
Students often write if (x = 10) when they mean if (x == 10). The first is assignment, not comparison. In some contexts Java will catch this as a compile error, but the habit needs to be right from the start.
Students sometimes assume && and || have the same precedence. They do not: && is evaluated before ||. When in doubt, use parentheses.
Students occasionally forget the curly braces {} around the body of an if or else. If the body is more than one statement, missing braces means only the first statement is conditional and the rest always run.
Students sometimes think else if is optional between if and else. It is optional, but when present, it provides additional conditions to test before falling through to else.
⚠️ The = vs == distinction is a near-certainty on any introductory Java exam. Be able to explain the difference and identify incorrect usage in code.
⚠️ Be able to trace through a compound boolean expression step by step and determine the final result.
⚠️ Know how to write and read an if / else if / else chain, and predict which block executes for a given input.
⚠️ Understand that && has higher precedence than ||.
True or False: x = 5 and x == 5 do the same thing in Java. False. x = 5 assigns; x == 5 compares.
Fill in the blank: The && operator returns true only when _____ sides are true. Answer: both
True or False: (false || true) evaluates to false. False. It evaluates to true.
True or False: An if statement can exist without an else block. True.
Fill in the blank: In the expression (A && B) || C, Java evaluates the ___ operator first. Answer: &&
Q: What does the following expression evaluate to?
int x = 5;
System.out.println((x > 0 && x < 5) || (x == 5));
A: true. x > 0 is true, x < 5 is false, so the AND is false. But x == 5 is true, so false || true gives true.
Q: What is printed by this code?
int first = 25;
if (first > 20) {
System.out.println("First is greater than 20");
} else {
System.out.println("First is not greater than 20");
}
A: First is greater than 20. The condition 25 > 20 is true, so the if block executes.
Q: Write an if/else statement that prints "Even" if a variable n is divisible by 2, and "Odd" otherwise.
A:
if (n % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
Q: Explain why && having higher precedence than || matters in the expression A || B && C.
A: Because && binds first, Java reads this as A || (B && C), not (A || B) && C. The two groupings can produce different results for the same values of A, B, and C.
Comparisons and conditionals build directly on the variable and type material from Lectures 1 and 2 (you need to know what types your values are before comparing them). They also feed into loops (Lecture 3, continued), where a conditional expression controls how many times a block of code repeats. The if/else if pattern reappears in array algorithms (Lecture 4), such as finding the maximum value in a collection.
Java == vs =, assignment vs equality, if else Java, else if chain, compound boolean expression, && AND operator, || OR operator, comparison operators Java, control flow, conditional statements, Java branching, CS 101 UIUC, boolean evaluation, operator precedence