🧱 1. Normal Statements
A normal statement is any line of code that executes in a straightforward, sequential manner. These are the basic building blocks of a Java program.
✍️ Example:
int a = 10;
System.out.println("The value of a is: " + a);
The first line stores 10 in variable a.
The second line prints the value.
These lines run one after the other—no decisions, no loops—just straight execution.
🔀 2. Conditional Statements
Conditional statements allow your program to make decisions. They help the program choose between different paths based on certain conditions.
✅ if Statement
int number = 5;
if (number > 0) {
System.out.println("The number is positive.");
}
This checks if the number is greater than 0. If true, it prints a message.
🔄 if-else Statement
if (number % 2 == 0) {
System.out.println("Even number.");
} else {
System.out.println("Odd number.");
}
Either the if or the else block will execute, not both.
📚 else if Ladder
if (number < 0) {
System.out.println("Negative number.");
} else if (number == 0) {
System.out.println("Zero.");
} else {
System.out.println("Positive number.");
}
Multiple conditions, one result.
🔘 switch Statement
Used when you have multiple specific values to check.
`int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Other day");
}`
🔁 3. Control (Looping) Statements
Control statements are used to repeat a block of code as long as a certain condition is true.
🔂 for Loop
for (int i = 1; i <= 5; i++) {
System.out.println("i = " + i);
}
Runs the loop 5 times, printing values from 1 to 5.
♻️ while Loop
int i = 1;
while (i <= 5) {
System.out.println("i = " + i);
i++;
}
Similar to for, but separates initialization and update.
🔁 do-while Loop
int i = 1;
do {
System.out.println("i = " + i);
i++;
} while (i <= 5);
This loop runs at least once, even if the condition is false initially.
🧭 Bonus: Jump Statements (break, continue)
break – exits a loop or switch.
continue – skips the current iteration and moves to the next one.
for (int i = 1; i <= 5; i++) {
if (i == 3) continue;
System.out.println("i = " + i);
}
Output: 1, 2, 4, 5 (skips 3)
🧠 Final Thoughts
- Normal statements execute in order.
- Conditional statements let you make choices.
- Control statements let you repeat actions.