How to Write Conditional Statements in Java?
🎯 What Will You Learn in This Guide?
In this guide, you will learn how to use conditional statements that control program flow in Java.
You will see step by step the differences between if, else if, else and switch structures, their correct usage areas and how to avoid common mistakes.
Additionally, you will get hands-on learning experience by performing live tests in the JShell environment through examples.
🧠 Technical Summary
Main topic: Correct use of conditional statements (if, else if, else, switch) in Java.
Purpose: To branch the program flow according to different conditions.
Solves the problem: Execution of certain code when a certain condition is met, alternative paths are followed when not met.
Steps followed:
- Java environment (JDK) and JShell are installed.
- The difference between "statement" and "block" is learned.
- Single
ifconditions are written. - The error occurring in non-Boolean expressions is examined.
- The effects of using
ifwithout blocks are seen. - Nested
ifstructure is applied. - The extended
if-else if-elsestructure is created. - The
switchstructure is introduced and tested with examples.
⚙️ 1. Necessary Prerequisites
To follow this guide, your system must have:
- Java Development Kit (JDK 11 or above)
- JShell (REPL) – to run examples directly from the terminal.
- Basic Java knowledge: Basic knowledge about data types, operators and class structure.
🧩 2. Statement and Code Block
When a condition is met, Java executes the corresponding block of code.
This block can be either a single statement or a group of multiple lines.
System.out.println("genixnode sunucusu");
➡️ This is an example of a one-line “statement”.
Block groups multiple expressions in curly brackets:
{
System.out.println("genixnode sunucusu");
}
➡️ For readability of the code, it is recommended to use four-space indentation.
💡 3. Using Simple if Condition
The most common conditional structure is the if statement. If the condition is true, the code inside the curly brackets works:
int x = 5;
int y = 2;
if (x > y) {
System.out.println(x + " sayısı " + y + " sayısından büyüktür.");
}
Output:
x ==> 5
y ==> 2
5 sayısı 2 sayısından büyüktür.
🚨 Common Error: Using non-Boolean expression
int x = 1;
if (x) {
System.out.println("Bu çalışacak mı?");
}
Error:
Error: int cannot be converted to boolean
➡️ Conditions should only return true or false.
⚠️ 4. Using if Without a Code Block (Not Recommended)
It is possible to write if without using curly brackets, but it is dangerous:
int x = 5;
int y = 2;
if (x > y)
System.out.println(x + " > " + y);
If you add one more line it works regardless of that condition:
if (x > y)
System.out.println("x büyük");
System.out.println("Bu satır her zaman çalışır.");
Output:
Bu satır her zaman çalışır.
➡️ Always use for clean code.
🔁 5. Nested If
You can place another if inside an if structure:
int x = 10;
int y = 5;
if (x > y) {
if (y == 5) {
System.out.println("x, y'den büyük ve y, 5'e eşit.");
}
}
➡️ It reduces readability, can be simplified with logical operators (&&, ||).
🌿 6. Expanded if – else if – else
Used to test multiple conditions sequentially:
int x = 10;
int y = 10;
if (x > y) {
System.out.println("x, y'den büyüktür.");
} else if (x < y) {
System.out.println("y, x'ten büyüktür.");
} else if (x == y) {
System.out.println("x, y'ye eşittir.");
} else {
System.out.println("Karşılaştırılamıyor.");
}
Output:
x ==> 10
y ==> 10
x, y'ye eşittir.
➡️ Only the first true condition works.
🔄 7. Switch Usage
switch compares a single variable with multiple literals.
int ayKodu = 1;
switch (ayKodu) {
case 1:
System.out.println("Ocak");
break;
case 2:
System.out.println("Şubat");
break;
default:
System.out.println("Geçersiz Ay");
}
Output:
ayKodu ==> 1
Ocak
Important:
break → Exits after correct match.
default → Fired when there is no match.
💬 Frequently Asked Questions (FAQ)
- Why should I use switch instead of if?
switch provides a cleaner structure for comparing a single variable with multiple literals.
- Can I write more than one condition in if?
Yes, && (and), || (or), ! You can combine it with (not) operators.
- What happens if I don't use code blocks?
Only one line becomes conditional, other lines always run.
- Is the else if order important?
Yes, conditions are tested from top to bottom. The first correct one works.
- Can I use boolean in switch?
No, switch does not support boolean variables. Use if for this.
✅ Result
In this guide:
You learned the differences between if, else if, else and switch structures in Java.
You have seen which one is suitable in which situation.
You've learned how to make your code cleaner, readable, and error-free.

