INTRODUCTION:Welcome back learners! It is indeed true that monkeys love bananas? But do they drink bananas? Nope. Depending on situations they consume different products. Our program that we code should be able to take different paths based on situations. For that purpose we write if-else statements in our code. These statements make our programs able to take decisions.
IF ELSE STATEMENTS
They check a condition, if it is true, they execute a specific part of code. If the condition is not true, an alternative part of code is executed.
if → Runs a code inside it when a condition is true
else → Runs an alternative code when condition is false
Let's write a simple code for it...
int bananas = 12;
if (bananas > 10) {
System.out.println("We have enough bananas!");
} else {
System.out.println("We are short on bananas");
This will print We have enough bananas! because the condition we set is true (12>10)
With "else if"
Most of the time there are more than 2 possible outcomes. Therefore we have else if in these statements which add more conditions to block of code e.g.
if(Condition 1){
// If condition 1 is true execute this block of code
}
else if(Condition 2){
// If condition 2 is true execute this block of code
}
else if(Condition 3){
// If condition 3 is true execute this block of code
}
else{
// If none of conditions is true execute this default block
}
POINTS TO REMEMBER:
⦿ Only one of these statements will be executed
if(Condition 1) // either this
else if(Condition 2) // or this
else if(Condition 3) // or this
else // or this
⦿ If one condition is executed the remaining are automatically ignored by java
if( false ) // ignored
else if( true ) // executed
else if( true ) // ignored
else // ignored
⦿ If multiple conditions are true only first one is executed
if( true ) // executed
else if( true ) // ignored
else if( true ) // ignored
else // ignored
⦿ If none are true then else will always execute
if( false ) // ignored
else if( false ) // ignored
else if( false ) // ignored
else // executed
⦿ Condition does the operations and return boolean value (true or false)
int age = 10;
if(age>5) As 10 is greater then 5 thus it act as: if( true )
⦿ Use comparison operators to form conditions (==,!=,>,<,>=,<=
)
⦿ Use logical operators to combine multiple conditions (&&,||,!
)
if(age>17 && age<40)
// This will do operation on both condition, if both are true then will return true
if(age>39 || age<18)
// This will do operation on both condition, if any one is true then will return true
if(age!=18)
// If age is NOT equal to 10, then it will return true
⦿ The else block is optional, you can write just if on returning false nothing will happen.
⦿ The else does not take condition but else if takes condition
Write the most specific condition on top. And most common or overlapped on bottom. This way your program will have clean and neat logic. Here is demonstration:
int marks = 85;
if (marks > 90) { // Most specific, highest grade
System.out.println("A");
}
else if (marks > 80) { // Next level
System.out.println("B");
}
else if (marks > 70) {
System.out.println("C");
}
else {
System.out.println("Fail");
}
The output will be: B
Here how it works...
1. 85 is not greater than 90 so first one is ignored
2. 85 is greater than 80 so second one is executed
3. Even though 85 is greater than 70, the program ignores the rest
⦿ Conditions are checked from top to bottom, so write most specific on top
⦿ Most specific means the one that overlaps the least with others (90 in this case)
⦿ else is not compulsory, but use it as safety net. If all conditions fail else guarantees on final outcome
"NESTED" IF ELSE STATEMENTS
Sometimes one question leads to more question, like one decision leads to taking more decisions. In that case we use more
if-else statements inside one.
int age = 20;
boolean hasID = true;
if (age >= 18) {
if (hasID) {
System.out.println("You can enter the club.");
}
else {
System.out.println("You need an ID to enter.");
}
} else {
System.out.println("You are too young to enter.");
}
The output will be: You can enter the club
Because the first condition returns true. And inside nested if, the other condition comes true as well therefore we get that specific output.
CLOSING
That’s it for if-else statements in Java! They’re the foundation of decision-making in programming. Once you understand how to set conditions and control the flow, you’ll see how powerful even the simplest programs can become.
Before jumping into the next blog (Switch Statements), it’s time to practice. I’ve prepared an exercise and a mini-project that focus on everything you’ve learned in this post.
📂 Conditional Statements Exercises Folder (For Post #07)
🌐 Full Java Fundamentals GitHub Repo
Download the exercise, break it down line by line, and then try the project challenge. This will help you apply conditions in real programs and strengthen your understanding before moving forward.
0 Comments