Conditions in Java
Java Master Course — Chapter 6 of 50
Conditions allow a Java program to make decisions. Instead of always executing the same code, a program can check a condition and choose what to do.
1. What Are Conditions?#
A program often needs to make decisions.
For example:
If marks are 40 or more
→ Student passes
Otherwise
→ Student fails
Or:
If age >= 18
→ Eligible to vote
Otherwise
→ Not eligible
This is called decision making or conditional execution.
The basic idea is:
Condition
│
┌──────┴──────┐
│ │
true false
│ │
▼ ▼
Action 1 Action 2
Java provides several ways to make decisions:
if
if-else
else-if
nested if
switch
switch expressions
ternary operator
You already learned the ternary operator in Chapter 4. Here we will understand conditional programming properly.
2. Boolean Values#
Conditions are based on boolean values.
A boolean can have only two values:
true
false
Example:
boolean isJavaEasy = true;
boolean isRainy = false;
A condition usually produces a boolean result.
For example:
10 > 5
produces:
true
And:
10 < 5
produces:
false
3. Relational Operators#
The most common operators used to create conditions are:
| Operator | Meaning |
|---|---|
> |
greater than |
< |
less than |
>= |
greater than or equal to |
<= |
less than or equal to |
== |
equal to |
!= |
not equal to |
Examples:
10 > 5
10 < 5
10 >= 10
10 <= 20
10 == 10
10 != 20
Their results are:
true
false
true
true
true
true
4. The if Statement#
The simplest decision-making statement is:
if
Syntax:
if (condition) {
// code
}
Example:
public class Main {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
System.out.println("You are an adult.");
}
}
}
Output:
You are an adult.
The code inside the if block executes only when the condition is true.
5. How if Works#
Consider:
int age = 20;
if (age >= 18) {
System.out.println("Adult");
}
Java evaluates:
age >= 18
20 >= 18
↓
true
Because the result is true, Java executes:
System.out.println("Adult");
Flow:
Start
↓
age = 20
↓
age >= 18 ?
↓
true
↓
Print "Adult"
↓
End
6. When the Condition Is False#
Example:
int age = 15;
if (age >= 18) {
System.out.println("Adult");
}
Condition:
15 >= 18
↓
false
Therefore the body is skipped.
Output:
There is no output.
This is an important property of if:
true → execute block
false → skip block
7. Basic if Example#
public class Main {
public static void main(String[] args) {
int marks = 80;
if (marks >= 40) {
System.out.println("Pass");
}
}
}
Output:
Pass
If:
int marks = 30;
there is no output because the condition is false.
8. if with Input#
Conditions become much more useful when combined with user input.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = sc.nextInt();
if (age >= 18) {
System.out.println("You are eligible.");
}
sc.close();
}
}
Input:
20
Output:
You are eligible.
Input:
15
Output:
9. if-else#
Usually, we want one action when a condition is true and another action when it is false.
For this we use:
if-else
Syntax:
if (condition) {
// true case
} else {
// false case
}
Example:
int age = 15;
if (age >= 18) {
System.out.println("Adult");
} else {
System.out.println("Minor");
}
Output:
Minor
10. How if-else Works#
Condition
│
┌───────┴───────┐
│ │
true false
│ │
▼ ▼
if block else block
│ │
└───────┬───────┘
↓
Continue
Exactly one of the two blocks executes.
11. Example — Even or Odd#
int number = 10;
if (number % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
Output:
Even
Why?
10 % 2
↓
0
Therefore:
number % 2 == 0
is true.
12. Even/Odd with User Input#
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = sc.nextInt();
if (number % 2 == 0) {
System.out.println("Even number");
} else {
System.out.println("Odd number");
}
sc.close();
}
}
Input:
25
Output:
Odd number
13. Positive, Negative, or Zero#
There are three possible cases:
positive
negative
zero
We need more than two branches.
Example:
int number = -10;
if (number > 0) {
System.out.println("Positive");
} else if (number < 0) {
System.out.println("Negative");
} else {
System.out.println("Zero");
}
Output:
Negative
14. else-if#
The general structure is:
if (condition1) {
// case 1
} else if (condition2) {
// case 2
} else if (condition3) {
// case 3
} else {
// default case
}
Java checks conditions from top to bottom.
The first true condition wins.
15. Example — Marks#
Suppose:
90–100 → A
80–89 → B
70–79 → C
60–69 → D
below 60 → F
Program:
int marks = 85;
if (marks >= 90) {
System.out.println("A");
} else if (marks >= 80) {
System.out.println("B");
} else if (marks >= 70) {
System.out.println("C");
} else if (marks >= 60) {
System.out.println("D");
} else {
System.out.println("F");
}
Output:
B
16. Why Order Matters#
Consider this:
int marks = 95;
if (marks >= 40) {
System.out.println("Pass");
} else if (marks >= 90) {
System.out.println("A");
}
Output:
Pass
Why didn't it print A?
Because Java checks:
marks >= 40
95 >= 40
↓
true
The first condition is already true.
Java executes that block and skips the remaining else-if conditions.
Therefore, more specific conditions often need to come before broader conditions.
Correct:
if (marks >= 90) {
System.out.println("A");
} else if (marks >= 40) {
System.out.println("Pass");
}
17. Important Rule: First Matching Branch#
In an if-else-if chain:
if (...)
else if (...)
else if (...)
else
Java evaluates from top to bottom.
condition 1
↓
true? ── yes → execute and stop
│
no
↓
condition 2
↓
true? ── yes → execute and stop
│
no
↓
condition 3
Only one branch from the chain executes.
18. Multiple Independent if Statements#
Compare:
if (condition1) {
}
if (condition2) {
}
with:
if (condition1) {
} else if (condition2) {
}
They are not the same.
Example:
int number = 10;
if (number > 0) {
System.out.println("Positive");
}
if (number % 2 == 0) {
System.out.println("Even");
}
Output:
Positive
Even
Both conditions are independently checked.
19. if-else-if Is Different#
int number = 10;
if (number > 0) {
System.out.println("Positive");
} else if (number % 2 == 0) {
System.out.println("Even");
}
Output:
Positive
The second condition is not checked because the first one was true.
Remember:
multiple if → multiple blocks can execute
if-else-if → at most one branch executes
20. Nested if#
An if statement can exist inside another if.
This is called a:
nested if
Example:
int age = 20;
boolean hasId = true;
if (age >= 18) {
if (hasId) {
System.out.println("Entry allowed");
}
}
Output:
Entry allowed
The inner condition is checked only if the outer condition is true.
21. Nested if Flow#
age >= 18?
│
┌────┴────┐
no yes
│ │
stop hasId?
│
┌────┴────┐
no yes
│ │
stop Entry allowed
Nested if is useful when one decision depends on another.
22. Nested if Example — Login#
String username = "admin";
String password = "1234";
if (username.equals("admin")) {
if (password.equals("1234")) {
System.out.println("Login successful");
}
}
Output:
Login successful
This is only a learning example. Real applications should not store passwords like this.
23. Adding else to Nested if#
int age = 20;
boolean hasId = false;
if (age >= 18) {
if (hasId) {
System.out.println("Entry allowed");
} else {
System.out.println("ID required");
}
} else {
System.out.println("Underage");
}
Output:
ID required
24. Logical Operators in Conditions#
You learned these in Chapter 4:
&&
||
!
They become extremely useful with if.
25. && — AND#
Both conditions must be true.
Example:
int age = 20;
boolean hasId = true;
if (age >= 18 && hasId) {
System.out.println("Allowed");
}
Both:
age >= 18 → true
hasId → true
Therefore:
true && true
↓
true
Output:
Allowed
26. Example — Login#
String username = "admin";
String password = "1234";
if (username.equals("admin") && password.equals("1234")) {
System.out.println("Login successful");
} else {
System.out.println("Invalid credentials");
}
The user must satisfy both conditions.
27. || — OR#
At least one condition must be true.
Example:
int age = 65;
if (age < 18 || age >= 60) {
System.out.println("Special category");
}
Here:
age < 18 → false
age >= 60 → true
false || true
↓
true
Output:
Special category
28. ! — NOT#
! reverses a boolean value.
boolean loggedIn = false;
if (!loggedIn) {
System.out.println("Please log in");
}
Since:
loggedIn = false
!loggedIn = true
Output:
Please log in
29. Combining Conditions#
Example:
int age = 25;
boolean citizen = true;
if (age >= 18 && citizen) {
System.out.println("Eligible");
}
Another example:
int marks = 85;
if (marks >= 40 && marks <= 100) {
System.out.println("Valid passing marks");
}
30. Parentheses Make Conditions Clear#
Consider:
if (age >= 18 && citizen || specialPermission)
This may be difficult to read.
Prefer:
if ((age >= 18 && citizen) || specialPermission)
Parentheses make the intended logic obvious.
31. Short-Circuit Evaluation#
Java's:
&&
||
operators use short-circuit evaluation.
For &&:
false && anything
is always false.
So Java may not evaluate the right side.
For ||:
true || anything
is always true.
So Java may not evaluate the right side.
32. Useful Short-Circuit Example#
Suppose:
String name = null;
This is dangerous:
if (name != null && name.length() > 0) {
System.out.println("Not empty");
}
Why is this safe?
Java checks:
name != null
first.
It is false.
Because the left side of && is false, Java does not evaluate:
name.length()
So there is no null dereference from that expression.
33. Dangerous Order#
This can fail:
if (name.length() > 0 && name != null) {
}
Java tries:
name.length()
before checking whether name is null.
So the order matters.
Good:
name != null && name.length() > 0
34. Conditions with Strings#
Do not compare Strings using:
==
when you mean content equality.
Use:
.equals()
Example:
String language = "Java";
if (language.equals("Java")) {
System.out.println("Correct");
}
Output:
Correct
For a potentially null String, a safer pattern can be:
if ("Java".equals(language)) {
System.out.println("Correct");
}
This avoids calling .equals() on a null variable.
35. == vs .equals() for Strings#
Consider:
String a = new String("Java");
String b = new String("Java");
Then:
a == b
checks whether the references refer to the same object.
It is not the correct general way to ask whether their contents are equal.
Use:
a.equals(b)
to compare String contents.
Output conceptually:
a == b → false
a.equals(b) → true
You will study references and object equality more deeply in the OOP chapters.
36. switch#
When you need to choose among discrete values, Java provides:
switch
Example:
int day = 2;
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("Invalid day");
}
Output:
Tuesday
37. How Traditional switch Works#
Suppose:
day = 2;
Java evaluates:
switch(day)
Then searches for:
case 2
It finds:
case 2:
System.out.println("Tuesday");
Then break exits the switch.
38. Why break Matters#
Consider:
int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
case 2:
System.out.println("Tuesday");
case 3:
System.out.println("Wednesday");
}
Because there are no break statements, execution can fall through after the matching case.
For day = 2, output can be:
Tuesday
Wednesday
This behavior is called:
fall-through
39. Traditional Switch with break#
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("Invalid");
}
Now only the matching branch executes.
40. default#
The default branch executes when no case matches.
Example:
int day = 10;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
default:
System.out.println("Invalid day");
}
Output:
Invalid day
Think of default as:
otherwise
41. Multiple Cases#
You can make multiple cases execute the same code.
Traditional syntax:
int day = 6;
switch (day) {
case 6:
case 7:
System.out.println("Weekend");
break;
default:
System.out.println("Weekday");
}
Output:
Weekend
Both 6 and 7 lead to the same block.
42. switch with String#
Modern Java allows switching on Strings.
String language = "Java";
switch (language) {
case "Java":
System.out.println("Java selected");
break;
case "Python":
System.out.println("Python selected");
break;
default:
System.out.println("Unknown language");
}
Output:
Java selected
String matching in a switch is based on String content semantics, not reference identity.
43. switch with char#
Example:
char grade = 'A';
switch (grade) {
case 'A':
System.out.println("Excellent");
break;
case 'B':
System.out.println("Good");
break;
case 'C':
System.out.println("Average");
break;
default:
System.out.println("Invalid grade");
}
Output:
Excellent
44. switch with enum#
Enums are covered in detail later, but Java can switch on enum values.
Example:
enum Day {
MONDAY,
TUESDAY,
WEDNESDAY
}
Then:
Day day = Day.MONDAY;
switch (day) {
case MONDAY:
System.out.println("Start of week");
break;
case TUESDAY:
System.out.println("Tuesday");
break;
case WEDNESDAY:
System.out.println("Wednesday");
break;
}
45. Modern Switch Expressions#
Modern Java provides a more concise switch form.
Example:
int day = 2;
String result = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
default -> "Invalid";
};
System.out.println(result);
Output:
Tuesday
This is called a:
switch expression
because it produces a value.
46. Switch Statement vs Switch Expression#
Traditional switch:
switch (day) {
case 1:
System.out.println("Monday");
break;
}
Modern switch expression:
String name = switch (day) {
case 1 -> "Monday";
default -> "Unknown";
};
The second form can directly produce a value.
47. Arrow Syntax#
Modern switch supports:
case value -> result;
Example:
int number = 1;
switch (number) {
case 1 -> System.out.println("One");
case 2 -> System.out.println("Two");
default -> System.out.println("Other");
}
The arrow form does not have the accidental fall-through behavior of traditional case statements.
48. Multiple Labels in Modern Switch#
You can group multiple values.
int day = 6;
String type = switch (day) {
case 6, 7 -> "Weekend";
default -> "Weekday";
};
System.out.println(type);
Output:
Weekend
This is much cleaner than repeating code.
49. Switch Expression with yield#
Sometimes a switch branch needs multiple statements.
Example:
int marks = 85;
String grade = switch (marks / 10) {
case 10, 9 -> "A";
case 8 -> "B";
case 7 -> "C";
case 6 -> "D";
default -> {
if (marks >= 40) {
yield "E";
} else {
yield "F";
}
}
};
System.out.println(grade);
yield provides the value produced by that switch-expression block.
50. if vs switch#
Use if when conditions involve ranges or complex boolean expressions.
Example:
if (marks >= 90) {
...
}
Use switch when you are selecting among specific discrete values.
Example:
switch (day) {
case 1 -> ...
case 2 -> ...
}
Simple mental model:
if
→ ranges, comparisons, complex logic
switch
→ specific choices/values
This is a guideline, not an absolute rule.
51. Ternary Operator#
You already learned this in Chapter 4.
Syntax:
condition ? valueIfTrue : valueIfFalse
Example:
int age = 20;
String result = age >= 18 ? "Adult" : "Minor";
System.out.println(result);
Output:
Adult
It is essentially a compact expression for choosing between two values.
52. if-else vs Ternary#
This:
if (age >= 18) {
result = "Adult";
} else {
result = "Minor";
}
can often become:
result = age >= 18 ? "Adult" : "Minor";
Use ternary for short, simple value selection.
Do not turn complicated logic into a giant nested ternary expression.
53. Nested Ternary#
Technically possible:
String result =
marks >= 90 ? "A" :
marks >= 80 ? "B" :
marks >= 70 ? "C" :
"F";
This works, but can become hard to read.
For complicated logic, prefer:
if
else-if
Readable code is usually better than clever code.
54. Conditions and Scope#
Variables declared inside an if block are local to that block.
Example:
if (true) {
int x = 10;
System.out.println(x);
}
This works.
But:
if (true) {
int x = 10;
}
System.out.println(x);
does not compile because x is outside its scope.
Think:
{
variable lives here
}
55. Declaring a Variable Before the Condition#
Suppose you want to use a value after the conditional.
You may write:
String result;
if (marks >= 40) {
result = "Pass";
} else {
result = "Fail";
}
System.out.println(result);
This works because both branches assign result.
The compiler performs definite-assignment analysis to make sure local variables are initialized before use.
56. Definite Assignment Example#
This is problematic:
String result;
if (marks >= 40) {
result = "Pass";
}
System.out.println(result);
Why?
If:
marks < 40
the assignment never happens.
Java therefore does not allow the compiler to assume result has a value.
A correct version:
String result;
if (marks >= 40) {
result = "Pass";
} else {
result = "Fail";
}
57. Conditions with final#
A final variable can still be used in conditions.
final int PASS_MARKS = 40;
int marks = 55;
if (marks >= PASS_MARKS) {
System.out.println("Pass");
}
Output:
Pass
final means the variable cannot be reassigned.
58. Comparing Characters#
Characters can be compared.
char ch = 'A';
if (ch == 'A') {
System.out.println("Character is A");
}
Output:
Character is A
You can also compare character values:
if (ch >= 'A' && ch <= 'Z') {
System.out.println("Uppercase letter");
}
This works because char participates in numeric comparisons according to Java's character/integer conversion rules.
59. Checking a Lowercase Character#
char ch = 'g';
if (ch >= 'a' && ch <= 'z') {
System.out.println("Lowercase");
}
Output:
Lowercase
For production code, Java also provides character utility methods such as:
Character.isUpperCase(ch)
Character.isLowerCase(ch)
Character.isDigit(ch)
Character.isLetter(ch)
60. Character Utility Example#
char ch = '7';
if (Character.isDigit(ch)) {
System.out.println("Digit");
} else {
System.out.println("Not a digit");
}
Output:
Digit
This is often clearer than manually checking ranges.
61. Conditions with Floating-Point Values#
Be careful when comparing floating-point values for exact equality.
Example:
double x = 0.1 + 0.2;
if (x == 0.3) {
System.out.println("Equal");
} else {
System.out.println("Not exactly equal");
}
The result may be:
Not exactly equal
because binary floating-point numbers cannot represent many decimal fractions exactly.
For numerical code, an acceptable tolerance is often used:
double epsilon = 0.000001;
if (Math.abs(x - 0.3) < epsilon) {
System.out.println("Approximately equal");
}
This is an important real-world consideration.
62. Range Checking#
A common task is checking whether a value lies inside a range.
Example:
int marks = 75;
if (marks >= 0 && marks <= 100) {
System.out.println("Valid marks");
} else {
System.out.println("Invalid marks");
}
The condition means:
marks is at least 0
AND
marks is at most 100
63. Range Checking with if-else-if#
int marks = 83;
if (marks < 0 || marks > 100) {
System.out.println("Invalid marks");
} else if (marks >= 90) {
System.out.println("A");
} else if (marks >= 80) {
System.out.println("B");
} else if (marks >= 70) {
System.out.println("C");
} else if (marks >= 60) {
System.out.println("D");
} else if (marks >= 40) {
System.out.println("E");
} else {
System.out.println("F");
}
Output:
B
Notice that validation comes before grading.
64. Practical Program — Largest of Two Numbers#
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = sc.nextInt();
System.out.print("Enter second number: ");
int b = sc.nextInt();
if (a > b) {
System.out.println("First number is larger");
} else if (b > a) {
System.out.println("Second number is larger");
} else {
System.out.println("Both numbers are equal");
}
sc.close();
}
}
65. Practical Program — Largest of Three Numbers#
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a: ");
int a = sc.nextInt();
System.out.print("Enter b: ");
int b = sc.nextInt();
System.out.print("Enter c: ");
int c = sc.nextInt();
if (a >= b && a >= c) {
System.out.println("Largest = " + a);
} else if (b >= a && b >= c) {
System.out.println("Largest = " + b);
} else {
System.out.println("Largest = " + c);
}
sc.close();
}
}
66. Practical Program — Leap Year#
A common leap-year rule is:
A year is a leap year if:
divisible by 400
OR
divisible by 4 AND not divisible by 100
Java:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter year: ");
int year = sc.nextInt();
if (year % 400 == 0 ||
(year % 4 == 0 && year % 100 != 0)) {
System.out.println("Leap year");
} else {
System.out.println("Not a leap year");
}
sc.close();
}
}
Examples:
2000 → Leap year
1900 → Not a leap year
2024 → Leap year
2023 → Not a leap year
This is a good example of combining:
%
&&
||
!=
inside one condition.
67. Practical Program — Vowel or Consonant#
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a character: ");
char ch = sc.next().charAt(0);
if (ch == 'a' || ch == 'e' ||
ch == 'i' || ch == 'o' ||
ch == 'u' ||
ch == 'A' || ch == 'E' ||
ch == 'I' || ch == 'O' ||
ch == 'U') {
System.out.println("Vowel");
} else {
System.out.println("Consonant");
}
sc.close();
}
}
For a production-quality program, you would also validate that the input is actually a letter.
68. Practical Program — Character Classification#
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a character: ");
char ch = sc.next().charAt(0);
if (Character.isLetter(ch)) {
System.out.println("Letter");
} else if (Character.isDigit(ch)) {
System.out.println("Digit");
} else {
System.out.println("Special character");
}
sc.close();
}
}
69. Practical Program — Calculator with switch#
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
double a = sc.nextDouble();
System.out.print("Enter operator (+, -, *, /): ");
char operator = sc.next().charAt(0);
System.out.print("Enter second number: ");
double b = sc.nextDouble();
switch (operator) {
case '+':
System.out.println("Result = " + (a + b));
break;
case '-':
System.out.println("Result = " + (a - b));
break;
case '*':
System.out.println("Result = " + (a * b));
break;
case '/':
if (b != 0) {
System.out.println("Result = " + (a / b));
} else {
System.out.println("Cannot divide by zero");
}
break;
default:
System.out.println("Invalid operator");
}
sc.close();
}
}
Example:
Enter first number: 20
Enter operator (+, -, *, /): *
Enter second number: 5
Result = 100.0
This demonstrates that conditions can exist inside switch branches.
70. Practical Program — Day of Week#
Using a modern switch expression:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter day number (1-7): ");
int day = sc.nextInt();
String dayName = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
case 4 -> "Thursday";
case 5 -> "Friday";
case 6 -> "Saturday";
case 7 -> "Sunday";
default -> "Invalid day";
};
System.out.println(dayName);
sc.close();
}
}
Input:
5
Output:
Friday
71. Practical Program — Electricity Bill#
For learning, suppose:
0–100 units → ₹5/unit
101–200 units → ₹7/unit
above 200 → ₹10/unit
A simple slab example:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter units: ");
int units = sc.nextInt();
double bill;
if (units <= 100) {
bill = units * 5;
} else if (units <= 200) {
bill = 100 * 5 + (units - 100) * 7;
} else {
bill = 100 * 5
+ 100 * 7
+ (units - 200) * 10;
}
System.out.printf("Bill = ₹%.2f%n", bill);
sc.close();
}
}
This is an excellent example of using ranges and else-if.
72. Practical Program — Admission Eligibility#
Suppose a student needs:
marks >= 60
AND
attendance >= 75
Program:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter marks: ");
int marks = sc.nextInt();
System.out.print("Enter attendance percentage: ");
double attendance = sc.nextDouble();
if (marks >= 60 && attendance >= 75) {
System.out.println("Eligible for admission");
} else {
System.out.println("Not eligible");
}
sc.close();
}
}
73. Practical Program — Login Decision#
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Username: ");
String username = sc.nextLine();
System.out.print("Password: ");
String password = sc.nextLine();
if ("admin".equals(username) && "1234".equals(password)) {
System.out.println("Login successful");
} else {
System.out.println("Invalid username or password");
}
sc.close();
}
}
Notice the style:
"admin".equals(username)
instead of:
username.equals("admin")
The first form remains safe if username is null.
74. Practical Program — Age Category#
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter age: ");
int age = sc.nextInt();
if (age < 0) {
System.out.println("Invalid age");
} else if (age <= 12) {
System.out.println("Child");
} else if (age <= 19) {
System.out.println("Teenager");
} else if (age <= 59) {
System.out.println("Adult");
} else {
System.out.println("Senior");
}
sc.close();
}
}
75. Practical Program — Discount Calculator#
Suppose:
Purchase >= 5000 → 20%
Purchase >= 2000 → 10%
Otherwise → 0%
Program:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter purchase amount: ");
double amount = sc.nextDouble();
double discountRate;
if (amount >= 5000) {
discountRate = 0.20;
} else if (amount >= 2000) {
discountRate = 0.10;
} else {
discountRate = 0.0;
}
double discount = amount * discountRate;
double finalAmount = amount - discount;
System.out.printf("Discount = %.2f%n", discount);
System.out.printf("Final amount = %.2f%n", finalAmount);
sc.close();
}
}
76. Practical Program — Simple ATM Decision#
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double balance = 10000;
System.out.print("Enter withdrawal amount: ");
double amount = sc.nextDouble();
if (amount <= 0) {
System.out.println("Invalid amount");
} else if (amount > balance) {
System.out.println("Insufficient balance");
} else {
balance -= amount;
System.out.printf("Withdrawal successful%n");
System.out.printf("Remaining balance = %.2f%n", balance);
}
sc.close();
}
}
This is still a simple educational example, not a real banking system.
77. Common Mistake — Assignment Instead of Comparison#
This is wrong for a condition:
if (age = 18) {
}
= means assignment.
Comparison uses:
==
Correct:
if (age == 18) {
}
Remember:
= → assign
== → compare
78. Common Mistake — Semicolon After if#
Do not write:
if (age >= 18);
{
System.out.println("Adult");
}
The semicolon terminates the if statement.
The block afterward is no longer controlled by the condition.
Correct:
if (age >= 18) {
System.out.println("Adult");
}
79. Common Mistake — Missing Braces#
Java technically allows:
if (age >= 18)
System.out.println("Adult");
But when there are multiple statements, braces are important.
Prefer:
if (age >= 18) {
System.out.println("Adult");
System.out.println("Eligible");
}
Braces make the scope obvious and reduce mistakes.
80. The Dangling else Problem#
Consider:
if (a > 0)
if (b > 0)
System.out.println("Both positive");
else
System.out.println("b is not positive");
Which if does the else belong to?
Java associates an else with the nearest unmatched if.
To make code clear, use braces:
if (a > 0) {
if (b > 0) {
System.out.println("Both positive");
} else {
System.out.println("b is not positive");
}
}
This is much easier to understand.
81. Common Mistake — Wrong else-if Order#
Bad:
if (marks >= 40) {
System.out.println("Pass");
} else if (marks >= 90) {
System.out.println("A");
}
The >= 90 branch is unreachable in practice within this chain because every mark of 90 or more already satisfies marks >= 40.
Better:
if (marks >= 90) {
System.out.println("A");
} else if (marks >= 40) {
System.out.println("Pass");
}
82. Common Mistake — Using == for String Content#
Avoid:
if (name == "Aman") {
}
Use:
if ("Aman".equals(name)) {
}
or:
if (name != null && name.equals("Aman")) {
}
The first version is often simpler and null-safe.
83. Common Mistake — Integer Division in Conditions#
Consider:
int a = 1;
int b = 2;
if (a / b == 0.5) {
System.out.println("Equal");
}
This does not behave as a floating-point division because both operands are integers.
1 / 2
↓
0
Use:
if ((double) a / b == 0.5) {
...
}
when floating-point division is intended.
84. Common Mistake — Floating-Point Exact Comparison#
Avoid relying on:
if (0.1 + 0.2 == 0.3)
for exact equality.
Floating-point representation can make the result slightly different.
Use an appropriate tolerance for numerical applications.
85. if Conditions Must Be Boolean#
Java does not allow C-style implicit integer-to-boolean conditions.
This is invalid:
int x = 10;
if (x) {
}
Java requires:
if (x > 0) {
}
or:
boolean positive = true;
if (positive) {
}
This is different from languages where non-zero numbers can be treated as true.
86. No Implicit Boolean Conversion#
These are not valid:
if (1) {
}
or:
if ("hello") {
}
Java conditions must have type:
boolean
or evaluate to a boolean expression.
87. Complex Condition Example#
Consider:
int age = 25;
boolean citizen = true;
boolean specialPermission = false;
if ((age >= 18 && citizen) || specialPermission) {
System.out.println("Eligible");
}
Break it down:
age >= 18
↓
true
citizen
↓
true
true && true
↓
true
specialPermission
↓
false
true || false
↓
true
Therefore:
Eligible
Learning to break complex conditions into small pieces is an important programming skill.
88. Boolean Variables Can Simplify Conditions#
Instead of:
if (isLoggedIn == true) {
}
write:
if (isLoggedIn) {
}
Similarly:
Instead of:
if (isLoggedIn == false) {
}
write:
if (!isLoggedIn) {
}
Example:
boolean isLoggedIn = true;
if (isLoggedIn) {
System.out.println("Welcome");
}
89. Avoid Unnecessary Boolean Comparisons#
Prefer:
if (hasPermission) {
}
instead of:
if (hasPermission == true) {
}
Prefer:
if (!hasPermission) {
}
instead of:
if (hasPermission == false) {
}
The shorter versions are easier to read.
90. Combining Conditions with Methods#
Conditions can use method calls.
Example:
String name = "Aman";
if (!name.isEmpty()) {
System.out.println("Name provided");
}
Another:
String email = "user@example.com";
if (email.contains("@")) {
System.out.println("Looks like an email");
}
These are simple examples; real validation should be more rigorous.
91. Conditions and Null#
Reference variables can contain:
null
You can check:
if (name == null) {
System.out.println("No name");
}
Or:
if (name != null) {
System.out.println(name);
}
Remember:
null means no object reference
You will study references and null much more deeply in the OOP chapters.
92. instanceof in Conditions#
Java provides:
instanceof
to test whether an object is compatible with a type.
Example:
Object value = "Java";
if (value instanceof String) {
System.out.println("It is a String");
}
Output:
It is a String
This becomes especially important when studying inheritance and polymorphism.
93. Pattern Matching with instanceof#
Modern Java can combine the type test and variable binding.
Example:
Object value = "Java";
if (value instanceof String text) {
System.out.println(text.length());
}
If the object is a String, Java makes the matched variable:
text
available in the appropriate scope.
You will study this more deeply in the modern Java chapter.
94. Conditions in Loops#
Conditions are not limited to if.
Loops also use conditions.
For example:
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
The condition:
i <= 5
determines whether the loop continues.
You will study loops in Chapter 7.
95. Conditions in Methods#
Methods can return boolean values.
Example:
public static boolean isEven(int number) {
return number % 2 == 0;
}
Then:
if (isEven(10)) {
System.out.println("Even");
}
Output:
Even
This is an important connection between conditions and methods.
96. Good Condition Design#
A good condition should be:
clear
correct
easy to read
easy to test
Instead of:
if (!(age < 18) && !(!citizen || banned)) {
}
sometimes it is better to introduce meaningful boolean variables:
boolean adult = age >= 18;
boolean allowed = citizen && !banned;
if (adult && allowed) {
...
}
Readable logic is easier to maintain.
97. Practical Example — Password Strength#
A very basic educational check:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter password: ");
String password = sc.nextLine();
if (password.length() >= 8) {
System.out.println("Password has at least 8 characters.");
} else {
System.out.println("Password is too short.");
}
sc.close();
}
}
This is not a complete password-security check. It simply demonstrates a condition.
98. Practical Example — Shipping Eligibility#
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter order amount: ");
double amount = sc.nextDouble();
System.out.print("Enter distance in km: ");
double distance = sc.nextDouble();
if (amount >= 1000 && distance <= 20) {
System.out.println("Free shipping");
} else {
System.out.println("Shipping charges apply");
}
sc.close();
}
}
99. Practical Example — Login with Account Status#
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Username: ");
String username = sc.nextLine();
System.out.print("Password: ");
String password = sc.nextLine();
boolean active = true;
if ("admin".equals(username) && "1234".equals(password)) {
if (active) {
System.out.println("Login successful");
} else {
System.out.println("Account is inactive");
}
} else {
System.out.println("Invalid credentials");
}
sc.close();
}
}
This example demonstrates nested decisions.
100. Decision-Making Cheat Sheet#
if
→ execute something only when a condition is true
if-else
→ choose between two paths
else-if
→ choose among multiple conditions
nested if
→ condition inside another condition
switch
→ choose among discrete values
switch expression
→ choose a value based on a case
ternary
→ compact two-way value selection
101. if Decision Tree#
Condition
│
┌─────┴─────┐
true false
│ │
▼ ▼
execute skip
block block
102. if-else Decision Tree#
Condition
│
┌─────┴─────┐
true false
│ │
▼ ▼
if block else block
│ │
└──────┬──────┘
▼
End
103. else-if Decision Tree#
Condition 1
/ \
true false
│ │
▼ ▼
Block 1 Condition 2
/ \
true false
│ │
▼ ▼
Block 2 Condition 3
Continue until one condition matches or else is reached.
104. switch Decision Tree#
value
│
┌──────────┼──────────┐
│ │ │
case 1 case 2 case 3
│ │ │
▼ ▼ ▼
Action Action Action
│ │ │
└──────────┴──────────┘
│
default
105. Practice Questions — Basic#
Write programs for:
- Check whether a number is positive.
- Check whether a number is negative.
- Check whether a number is zero.
- Check whether a number is even or odd.
- Check whether a person is an adult.
- Check whether marks are passing.
- Find the larger of two numbers.
- Check whether two numbers are equal.
- Check whether a character is uppercase.
- Check whether a character is a digit.
106. Practice Questions — if-else-if#
Write programs to:
- Find the grade from marks.
- Categorize age.
- Find the largest of three numbers.
- Find the smallest of three numbers.
- Determine whether a year is a leap year.
- Calculate a discount based on purchase amount.
- Calculate an electricity bill using slabs.
- Classify a number as positive, negative, or zero.
- Check whether a triangle angle value is valid.
- Determine a student's result category.
107. Practice Questions — Logical Operators#
Write programs that check:
- Age and citizenship eligibility.
- Username and password.
- Whether a number lies within a range.
- Whether a number is divisible by both 3 and 5.
- Whether a number is divisible by either 3 or 5.
- Whether a person is eligible based on marks and attendance.
- Whether a product gets free shipping.
- Whether a user is logged in and has permission.
- Whether an account is active and verified.
- Whether a number is outside a specified range.
108. Practice Questions — switch#
Write programs for:
- Day of week.
- Month number to month name.
- Simple calculator.
- Menu selection.
- Grade description.
- Traffic signal.
- Basic unit converter.
- Number to word for values 1–5.
- Season selection.
- Character-based menu.
109. Challenge — ATM Menu#
Create:
===== ATM =====
1. Check Balance
2. Deposit
3. Withdraw
4. Exit
Take the user's choice and perform the appropriate action.
Use:
switch
and conditions where needed.
110. Challenge — Student Result System#
Take:
student name
marks of 5 subjects
Calculate:
total
percentage
grade
pass/fail
Validation:
Each mark must be between 0 and 100.
Suggested logic:
invalid marks
↓
show error
otherwise
↓
calculate percentage
↓
determine grade
↓
display result
111. Challenge — Login System#
Create a simple program with:
username
password
account status
Rules:
wrong username/password
→ invalid credentials
correct credentials + inactive account
→ account inactive
correct credentials + active account
→ login successful
Use:
if
else-if
&&
112. Challenge — Shopping Discount#
Rules:
amount >= 10000 → 30%
amount >= 5000 → 20%
amount >= 2000 → 10%
otherwise → 0%
Print:
Original amount
Discount percentage
Discount amount
Final amount
Use printf() for money formatting.
113. Challenge — Number Analyzer#
Take an integer and determine:
positive/negative/zero
even/odd
divisible by 3?
divisible by 5?
For example:
Input: 30
Positive
Even
Divisible by 3
Divisible by 5
Notice that multiple independent if statements are useful here because multiple properties can be true at the same time.
114. Interview Questions#
Q1. What is an if statement?#
An if statement conditionally executes a block of code when a boolean condition is true.
Q2. What is if-else?#
It provides two alternative execution paths:
true → if
false → else
Q3. What is else-if?#
It allows multiple conditions to be checked in sequence.
The first true condition's block executes.
Q4. Can multiple if blocks execute?#
Yes.
With separate if statements, every condition is checked independently.
Q5. Can multiple else-if branches execute?#
No.
In a single if-else-if-else chain, at most one branch executes.
Q6. What is nested if?#
An if statement placed inside another conditional block.
Q7. What is switch used for?#
switch is useful when selecting among discrete values or cases.
Q8. What is break used for in traditional switch?#
It exits the switch statement and prevents execution from falling through to later cases.
Q9. What is fall-through?#
In traditional switch syntax, if a matching case does not end with a control transfer such as break, execution can continue into subsequent cases.
Q10. What is default?#
It is the fallback branch of a traditional switch when no case matches.
Q11. What is a switch expression?#
A modern switch form that produces a value.
Example:
String result = switch (day) {
case 1 -> "Monday";
default -> "Unknown";
};
Q12. What is the ternary operator?#
A conditional expression with three parts:
condition ? trueValue : falseValue
Q13. Can Java use an integer directly as an if condition?#
No.
Java requires a boolean expression.
This is invalid:
if (1) {
}
Q14. Difference between = and ==?#
= → assignment
== → equality comparison
Q15. How do you compare Strings?#
Use .equals() for content equality.
Example:
"Java".equals(name)
Q16. Why is "Java".equals(name) useful?#
It avoids calling a method on name when name is null.
Q17. What is short-circuit evaluation?#
With && and ||, Java may skip evaluation of the right-hand operand when the result is already determined by the left-hand operand.
Q18. Why is this safe?#
if (name != null && name.length() > 0)
Because if name != null is false, && does not evaluate name.length().
Q19. What is the difference between if and switch?#
if is more flexible for comparisons, ranges, and complex boolean expressions.
switch is convenient for choosing among discrete values.
Q20. What is a nested condition?#
A condition placed inside another condition.
115. Interview Output Questions#
Question 1#
What is the output?
int x = 10;
if (x > 5) {
System.out.println("A");
}
System.out.println("B");
Answer:
A
B
Question 2#
int x = 10;
if (x > 20) {
System.out.println("A");
} else {
System.out.println("B");
}
Output:
B
Question 3#
int x = 10;
if (x > 0) {
System.out.println("Positive");
} else if (x % 2 == 0) {
System.out.println("Even");
}
Output:
Positive
The second condition is not reached.
Question 4#
int x = 10;
if (x > 0) {
System.out.println("Positive");
}
if (x % 2 == 0) {
System.out.println("Even");
}
Output:
Positive
Even
Question 5#
int x = 5;
if (x > 0 && x < 10) {
System.out.println("A");
}
Output:
A
Question 6#
int x = 10;
if (x < 5 || x == 10) {
System.out.println("Yes");
}
Output:
Yes
116. Important Mental Model#
When reading a condition, ask:
What is being checked?
↓
What boolean value does it produce?
↓
Which branch executes?
For example:
if (marks >= 40) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
Think:
marks >= 40?
│
┌──┴──┐
true false
│ │
Pass Fail
This mental model makes conditional code much easier to understand.
117. Conditions in Real Programs#
Almost every serious application uses conditions.
Examples:
Login
↓
Is username correct?
↓
Is password correct?
↓
Is account active?
Shopping:
Cart
↓
Is coupon valid?
↓
Is minimum amount reached?
↓
Apply discount
Banking:
Withdrawal request
↓
Is amount valid?
↓
Is balance sufficient?
↓
Process withdrawal
Games:
Player action
↓
Is move valid?
↓
Is player alive?
↓
Update game
Web applications:
Request
↓
Is user authenticated?
↓
Is user authorized?
↓
Perform operation
Conditions are therefore not just beginner syntax. They are a fundamental part of software logic.
118. Connection to Previous Chapters#
You now have:
Chapter 3
Variables & Data Types
↓
Chapter 4
Operators
↓
Chapter 5
Input & Output
↓
Chapter 6
Conditions
Together, these allow you to build programs like:
User Input
↓
Store in Variables
↓
Perform Calculations
↓
Check Conditions
↓
Choose Action
↓
Display Output
This is the basic structure of many programs.
119. Connection to the Next Chapter#
The next major step is repetition.
Conditions answer:
Should I do this?
Loops answer:
Should I keep doing this?
For example:
if (age >= 18) {
System.out.println("Adult");
}
checks something once.
A loop can repeatedly check a condition:
while (age < 18) {
// repeat
}
You will learn this in:
Chapter 7 — Loops#
Topics will include:
while
do-while
for
nested loops
break
continue
enhanced for loop
loop patterns
common loop mistakes
practical problems
120. Final Revision#
Remember these structures.
Basic if:
if (condition) {
// code
}
if-else:
if (condition) {
// true
} else {
// false
}
else-if:
if (condition1) {
// ...
} else if (condition2) {
// ...
} else {
// ...
}
Nested if:
if (condition1) {
if (condition2) {
// ...
}
}
Traditional switch:
switch (value) {
case 1:
// ...
break;
case 2:
// ...
break;
default:
// ...
}
Modern switch:
switch (value) {
case 1 -> "One";
case 2 -> "Two";
default -> "Other";
}
Ternary:
condition ? value1 : value2
Logical operators:
&& → AND
|| → OR
! → NOT
Comparison operators:
> < >= <= == !=
Most importantly:
if
↓
decision
else
↓
alternative
else-if
↓
multiple alternatives
switch
↓
discrete choices
Once conditions become comfortable, you are ready to move from simple one-time decisions to repeated logic with loops.