0% found this document useful (0 votes)
1 views

Conditional Statements in Java

Conditional statements in Java enable decision-making based on boolean expressions, with various types including if, if-else, if-else-if, switch, and the ternary operator. Each type serves specific use cases, such as handling single conditions or multiple comparisons. The switch statement is particularly useful for comparing a variable against multiple constant values.

Uploaded by

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

Conditional Statements in Java

Conditional statements in Java enable decision-making based on boolean expressions, with various types including if, if-else, if-else-if, switch, and the ternary operator. Each type serves specific use cases, such as handling single conditions or multiple comparisons. The switch statement is particularly useful for comparing a variable against multiple constant values.

Uploaded by

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

🔀 Conditional Statements in Java

Conditional statements in Java allow your program to make decisions


based on boolean expressions. Depending on whether a condition is
true or false, different blocks of code are executed.

🔹 Types of Conditional Statements


1. if Statement

Executes a block of code if the condition is true.

java
CopyEdit
int age = 18;

if (age >= 18) {


System.out.println("You are an adult.");
}

2. if-else Statement

Executes one block if the condition is true, another if false.

java
CopyEdit
int number = 5;

if (number % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}

3. if-else-if Ladder

Checks multiple conditions in order. The first one that is true gets
executed.

java
CopyEdit
int marks = 75;

if (marks >= 90) {


System.out.println("Grade A");
} else if (marks >= 75) {
System.out.println("Grade B");
} else if (marks >= 50) {
System.out.println("Grade C");
} else {
System.out.println("Fail");
}

4. switch Statement

Best used when comparing a variable to multiple constant values (like


numbers or strings).

java
CopyEdit
int day = 3;

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");
}
🔸 Use break to prevent "fall-through" (i.e., executing multiple cases in a
row).🔸 switch works with byte, short, int, char, String, and enum
types.

5. Ternary Operator

A shorthand for simple if-else.

java
CopyEdit
int a = 10, b = 20;

int max = (a > b) ? a : b;


System.out.println("Max is " + max);

🧠 Summary Table
Statement Use Case
if Single condition
if-else Binary decision (true/false)
if-else-if Multiple possible conditions
Multiple exact matches (e.g.,
switch
enums, days)
Ternary ?: Simple one-line decisions

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy