Conditional Statements
Conditional Statements
1) If –else
2) if and only if (Multiple if )
3) if –else if – else
4) nested if else
Note
AND(&&)
The && operator evaluates to true if both conditions it connects are true. If either condition is false, the entire
expression evaluates to false
AND (&&)
Condition 1 Condition 2 Output
True True True
True False False
False True False
False False False
OR(||)
The || operator evaluates to true if at least one of the conditions it connects is true. The entire expression
evaluates to false only if all conditions are false
if (condition1 || condition2) {
// Code executes if either condition1 or condition2 (or both) are true
}
OR (||)
Condition 1 Condition 2 Output
True True True
True False True
False True True
False False False
if - else
The if statement starts by evaluating the condition inside the parentheses ().
The condition is a boolean expression (an expression that evaluates to true or false).
If the condition evaluates to true, the code inside the if block (the first set of curly braces {}) is executed.
If the condition evaluates to false, the code inside the else block (the second set of curly braces {}) is
executed.
The else block is optional. If you omit it and the condition is false, nothing happens, and the program
continues executing the code after the if-else statement.
1)
System.out.println("Allowed to enter");
else
System.out.println("Condition is true");
else
System.out.println("Condition is false");
if (x < 10)
{
System.out.println("X is less than 10");
}
if (y > 5)
{
System.out.println("Y is greater than 5");
}
if (x + y == 15)
{
System.out.println("The sum of X and Y is 15");
}
if (x > 10)
{
System.out.println("X is greater than 10");
}
OUTPUT
X is less than 10
Y is greater than 5
The sum of X and Y is 15
First if Statement: The condition x < 10 is evaluated. Since x is 5, which is less than 10, the statement
inside the if block is executed, printing "X is less than 10".
Second if Statement: Regardless of whether the first if was true or false, this condition y > 5 is still
evaluated. Since y is 10, which is greater than 5, it prints "Y is greater than 5".
Fourth if Statement: This condition x > 10 is evaluated, even though previous conditions might have
been true or false. Since x is 5, which is not greater than 10, the block is not executed, and nothing is
printed.
1)
int temperature = 40;
if (temperature > 30)
{
System.out.println("It's hot outside.");
}
if (x % 2 == 0 && y % 2 == 0)
{
System.out.println("Both x and y are even numbers.");
}
if (x % 2 != 0 && y % 2 != 0)
{
System.out.println("Both x and y are odd numbers.");
}
4) int x = 7, y = 8;
if (x < 10 && y < 10)
{
System.out.println("Both x and y are single-digit numbers.");
}
if (x % 2 == 0 && y % 2 == 0)
{
System.out.println("Both x and y are even numbers.");
}
if (x % 2 == 0 && y % 2 != 0)
{
System.out.println("x is even, but y is odd.");
}
if (x % 2 != 0 && y % 2 == 0) {
System.out.println("x is odd, but y is even.");
}
if (x % 2 != 0 && y % 2 != 0) {
System.out.println("Both x and y are odd numbers.");
}
}
}
Example
if (temperature < 0)
System.out.println("It's freezing!");
System.out.println("It's cold");
System.out.println("It's warm");
else
System.out.println("It's hot!");
1)
int x = 7;
int y = 15;
System.out.println("Condition 1 is true");
System.out.println("Condition 2 is true");
else
3)
int age = 16;
boolean hasPermit = true;
else
{
System.out.println("You cannot drive.");
}
4)
int a = 4,b = 9;
if (a < 5 || b > 10)
{
System.out.println("Condition 1 is true");
}
else if (a > 5 || b > 5)
{
System.out.println("Condition 2 is true");
}
else if (a < 5 || b < 5)
{
System.out.println("Condition 3 is true");
}
else
{
System.out.println("None of the conditions are true");
}
OUTPUT : Condition 1 is true
5) int x = 8,y = 12;
if (x > 5 && y < 10)
{
System.out.println("Condition 1 is true");
}
else if (x > 5 && y >= 10)
{
System.out.println("Condition 2 is true");
}
else
{
System.out.println("Neither condition is true");
}
OUTPUT : Condition 2 is true
NESTED if – else
A nested if-else statement is when you place one if-else, multiple if and if –else if – else
statements inside another if or else block.
If the outer if condition is false, the program skips the entire if block and moves to the else block (if
present).
The else block may contain its own nested if-else, multiple if and if –else if – else
structure, which will then be evaluated in the same manner.
Nested if-else statements where both the if and else blocks contain their own if-else structures:
1) int a = 5, b = 10;
if (a < 10)
{
if (b > 5)
{
System.out.println("Block 1");
}
else
{
System.out.println("Block 2");
}
}
else
{
if (b == 10)
{
System.out.println("Block 3");
}
else
{
System.out.println("Block 4");
}
}
Output : Block 1
2)
int n = 6,m = 7;
if (n * m > 40)
{
if ((n + m) % 2 == 0)
{
System.out.println("Product large, sum even");
}
else
{
System.out.println("Product large, sum odd");
}
}
else
{
if ((n * m) % 3 == 0)
{
System.out.println("Product small, divisible by 3");
}
else
{
System.out.println("Product small, not divisible by 3");
}
}
Output : Product large, sum odd
3)
int p = 4, q = 8;
if (p == q)
{
if (p % 2 == 0)
{
System.out.println("Equal and even");
}
else
{
System.out.println("Equal and odd");
}
}
else
{
if (p > q)
{
System.out.println("p is greater");
}
else
{
System.out.println("q is greater");
}
}
Output: q is greater
int n = 6,m = 7;
if (n * m > 40)
{
if ((n + m) % 2 == 0)
{
System.out.println("Product large, sum even");
}
else
{
System.out.println("Product large, sum odd");
}
}
else
{
if ((n * m) % 3 == 0)
{
System.out.println("Product small, divisible by 3");
}
else
{
System.out.println("Product small, not divisible by 3");
}
}
Output : Product large, sum odd
Nested if-else statements where both the if and else blocks contain their own multiple if structures:
Q2)
String a= ” Karnataka”;
If(a.equals(“Karnataka”))
{
If(a.equals(“Maharashtra”)
System.out.println(“Language”);
If(a.equals(“Karnataka”)
System.out.println(“State “);
}
else
{
If(2==2)
System.out.println(“India”);
If(3==30)
System.out.println(“ Bharat”);
}
Nested if-else statements where both the if and else blocks contain their own if-else if – else structures:
1) intp=10,q=5;
if (q % 3 == 0)
{
if (p % 3 == 0)
System.out.println("p is less or equal, and both are divisible by 3");
else if (p * q > 50)
System.out.println("p is less or equal, q is not divisible by 3, and their product is greater than 50");
else
System.out.println("p is less or equal, q is not divisible by 3, and their product is not greater than 50");
}
else
{
System.out.println(“Not eligible”);
}
2)
int a = 8;
int b = 12;
if (a > 5)
{
if (b < 15)
System.out.println(“Check Further”);
else if (a + b > 20)
{
System.out.println("a + b is greater than 20");
}
else
{
System.out.println("a + b is less than or equal to 20");
}
}
else
{ if (a < 3)
{
System.out.println(“Check “);
}
else if (b % 2 == 0)
{
System.out.println("b is even and greater than or equal to 15");
}
else
{
System.out.println("b is odd and greater than or equal to 15");
}
}