0% found this document useful (0 votes)
7 views16 pages

Conditional Statements

The document provides a comprehensive overview of conditional statements in Java, including 'if-else', 'if and only if', 'if-else if-else', and nested 'if-else' structures. It explains the logical operators AND (&&) and OR (||), along with their truth tables, and illustrates each concept with multiple code examples. The document emphasizes how conditions are evaluated and the flow of execution based on the truth values of these conditions.

Uploaded by

Ketaki Bhate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views16 pages

Conditional Statements

The document provides a comprehensive overview of conditional statements in Java, including 'if-else', 'if and only if', 'if-else if-else', and nested 'if-else' structures. It explains the logical operators AND (&&) and OR (||), along with their truth tables, and illustrates each concept with multiple code examples. The document emphasizes how conditions are evaluated and the flow of execution based on the truth values of these conditions.

Uploaded by

Ketaki Bhate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Table of contents

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

if (condition1 && condition2)


{
// Code executes only if both condition1 and condition2 are true
}

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)

int number = 10;


if (number > 5)
{
System.out.println("The number is greater than 5");
}
else
{
System.out.println("The number is 5 or less");
}
 The condition is number > 5.
 Since 10 > 5 is true, the code inside the if block is executed, and the

output is The number is greater than 5

2) int a = 8,b = 12;


if (a < b)
{
System.out.println("A is less than B");
}
else
{
System.out.println("A is not less than B");
}
Output : A is less than B
3) int number = 15;
if (number % 2 == 0)
{
System.out.println("Number is even");
}
else
{
System.out.println("Number is odd");
}
4) int age = 20;
boolean hasTicket = true;

if (age >= 18 && hasTicket)

System.out.println("Allowed to enter");

else

System.out.println("Condition not met");

Output : Allowed to enter

5) int a = 25, b=30,c=35

if ((a < b && c > a)

System.out.println("Condition is true");

else

System.out.println("Condition is false");

Output : Condition is true


2) if and only if (Multiple if )

 In a sequence of multiple if statements in Java, each if condition is evaluated independently.


 This means that every if condition is checked, regardless of whether the previous condition was true or
false.
EXAMPLE
int x = 5; y = 10;

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".

 Third if Statement: Again, this if is evaluated independently. The condition x + y == 15 checks


whether the sum of x and y is 15. Since 5 + 10 equals 15, it prints "The sum of X and Y is 15".

 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 (temperature >= 50)


{
System.out.println("It's not too hot outside.");
}
OUTPUT: It's hot outside
It's not too hot outside.
2)
int age = 17;
if (age >= 18)
{
System.out.println("You are eligible to vote.");
}

if (age < 18)


{
System.out.println("You are not eligible to vote.");
}
OUTPUT: You are not eligible to vote.
3)
int x = 6;
int 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("Both x and y are odd numbers.");
}

Output :Both x and y are single-digit numbers.


Both x and y are even 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 < 10 && y >= 10)


{
System.out.println("x is a single-digit number, but y is not.");
}
if (x >= 10 && y < 10)
{
System.out.println("x is not a single-digit number, but y is.");
}

if (x >= 10 && y >= 10)


{
System.out.println("Neither x nor 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.");
}
}
}

If- else if – else


How It Works (Step by Step):

1. The program evaluates the condition in the if statement.


2. If the if condition is true, its code block is executed, and the rest of the else if or else blocks are
ignored.
3. If the if condition is false, the program moves to the else if condition (if any).
o The program evaluates the else if condition.
o If true, its code block is executed, and the rest of the else if or else blocks are ignored.
o If false, the program moves to the next else if (if present) or to the else block.
4. If no if or else if conditions are true, the else block is executed.

Example

int temperature = 35;

if (temperature < 0)

System.out.println("It's freezing!");

else if (temperature < 20)

System.out.println("It's cold");

else if (temperature < 30)

System.out.println("It's warm");

else

System.out.println("It's hot!");

Output: It's hot!

1)
int x = 7;

int y = 15;

if ((x < 10 && y > 10) || (x > 5 && y < 20))

System.out.println("Condition 1 is true");

else if (x > 10 || y > 20)

System.out.println("Condition 2 is true");

else if (x == 7 && y == 15)

System.out.println("Exact match found");

else

System.out.println("No condition is true");

OUTPUT Condition 1 is true

2) int score = 65;


boolean extraCredit = true;
if (score >= 90 && extraCredit)
{
System.out.println("A+ with extra credit");
}

else if (score >= 80 && !extraCredit)


{
System.out.println("B without extra credit");
}

else if (score >= 70 || extraCredit)


{
System.out.println("C with or without extra credit");
}
else
{
System.out.println("Grade D or F");
}

Output: C with or without extra credit

3)
int age = 16;
boolean hasPermit = true;

if (age >= 18 && hasPermit)


{
System.out.println("You can drive alone.");
}

else if (age >= 16 && hasPermit)


{
System.out.println("You can drive with a guardian.");
}

else if (age >= 16 || hasPermit)


{
System.out.println("You need a permit to drive.");
}

else
{
System.out.println("You cannot drive.");
}

OUTPUT : You can drive with a guardian

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.

How Nested if-else Works:

1. Evaluate the Outer if Condition:


o The program first checks the condition in the outer if statement.
o If the condition is true, the code inside the outer if block is executed, which may contain
another if-else, multiple if and if –else if – else statement.

2. Move to the Outer 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:

if(2 >6 && 5>6)


{
If(5>10)
System.out.println(“smaller”);
If(8>5)
System.out.println(“greater”);
If(9/5==0)
System.out.println(“none”);
}
else
{
If(3==3)
System.out.println(“ equal”);
If(5==4)
System.out.println(“ equal to “);
If (2!=1)
System.out.println(“not equal”);
}

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");
}
}

You might also like