0% found this document useful (0 votes)
2 views11 pages

Java Conditional Statements PDF

Conditional statements in Java enable decision-making by executing different code blocks based on true or false conditions. The main types include if, if-else, if-else-if ladder, nested if, and switch statements, each serving specific purposes for flow control. The document also outlines when to use each type of statement, emphasizing that switch statements are more efficient for exact value matching.

Uploaded by

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

Java Conditional Statements PDF

Conditional statements in Java enable decision-making by executing different code blocks based on true or false conditions. The main types include if, if-else, if-else-if ladder, nested if, and switch statements, each serving specific purposes for flow control. The document also outlines when to use each type of statement, emphasizing that switch statements are more efficient for exact value matching.

Uploaded by

rathinakumari.a
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Conditional

Statements
What Are Conditional Statements?

Conditional statements allow a program to make decisions and execute different


blocks of code based on whether a condition is true or false.
* Decision Making-Execute specific code only when a condition is met
* Flow Control-Direct the program along different paths of execution
* Boolean Logic-Conditions evaluate to either true or false
Types of Conditional Statements

1. if statement : If a condition is true then only execute the code.


2. if-else statement : Choose between two paths.
3. if-else-if-else ladder : Checks multiple conditions in order.
4. Nested - if : If statement inside another is statement.
5. Switch statement : Picks one case from fixed values.
cleaner than if-else chains.

6.
The if Statement

SYNTAX
How it works

if (condition) { 1 Condition is evaluated first

// code block
2 If true → code block executes
}

3 If false → block is skipped


EXAMPLE

4 Condition must return boolean


int age = 20;
if (age >= 18) {
[Link](
Output:Right age for vote
“Right age for vote");
}
The if-else Statement
EXAMPLE
condition is true?

int number = 7;
true false
if (number%2 == 0) {
[Link](“Even");
} if block runs else block runs
else {
[Link](“Odd");
}
Key Points

▸ Only one block executes

▸ else is optional in if-else


Output: Odd
▸ Use for two-way decisions
The if-else-if Ladder

Grade Ladder

int marks = 85;


marks ≥ 90 A+
if (marks >= 90) {
[Link] (" grade A+ ");
}
else if (marks >= 80) {
[Link] (" grade A ");
marks ≥ 80 A
}
else if (marks >= 70) {

}
[Link] (" grade B"); marks ≥ 70 B
else {
[Link] (" grade C");
} otherwise C

Output: grade A
Nested - if Statements

int age = 20;


Boolean haslicence=true;
if (age >= 18)
if (haslicence)
{
[Link](“Can Drive");
}
Output:
Can Drive
The switch Statement

Key Components
int day = 3;
switch (day) { switch Evaluates one variable/expression
case 1: [Link]("Mon“);
break;
case 2: [Link](“Tues“);
break; case Matches specific value to execute
case 3: [Link](“Wed“);
break;
case 4: [Link](“Thurs“);
break; break Exits switch block (prevents fall-through)
default:
[Link](“Invalid
Day“);
default Runs when no case matches
}

Output: Wed
When to Use Which?

Statement Best Used When Example Use Case

if Single condition check Validate user input

if-else Two possible outcomes Login success / failure

if-else-if-
Multiple range conditions Grade classification
Ladder

Can Drive
Nested - if If inside another if

switch Match exact discrete values Day of week, menu choice

📌 switch is faster than long if-else-if chains for exact value matching (uses jump table internally).
Summary

if Executes block only when condition is true

if-else Chooses between two blocks based on condition

if-else-if Multiple conditions checked in sequence (ladder)

Nested - if Nested if conditions checked

switch
Matches variable to discrete values; needs break
What Are Conditional Statements?

THANK YOU

You might also like