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

Conditional Statements Java

Conditional statements in Java are used to make decisions based on true or false conditions, allowing the program to execute different code paths. The main types include if statements, if-else statements, if-else-if ladders, nested if statements, and switch statements, each with specific syntax and examples. These constructs enable developers to control the flow of execution in their programs effectively.

Uploaded by

Lalitha yamini
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 views3 pages

Conditional Statements Java

Conditional statements in Java are used to make decisions based on true or false conditions, allowing the program to execute different code paths. The main types include if statements, if-else statements, if-else-if ladders, nested if statements, and switch statements, each with specific syntax and examples. These constructs enable developers to control the flow of execution in their programs effectively.

Uploaded by

Lalitha yamini
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/ 3

Conditional Statements in Java

Definition:
Conditional statements are used to make decisions in a program. They execute specific
blocks of code based on whether a condition is true or false. These allow the program to
take different paths during execution.

Types of Conditional Statements:


 1. if statement
 2. if-else statement
 3. if-else-if ladder
 4. nested if
 5. switch statement

1. if Statement
Syntax:

if (condition) {
// code to be executed if condition is true
}

Example:

int age = 20;


if (age >= 18) {
System.out.println("You are eligible to vote.");
}

2. if-else Statement
Syntax:

if (condition) {
// code if condition is true
} else {
// code if condition is false
}

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

3. if-else-if Ladder
Syntax:

if (condition1) {
// code for condition1
} else if (condition2) {
// code for condition2
} else {
// code if none of the conditions are true
}

Example:

int marks = 85;


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

4. Nested if Statement
Syntax:

if (condition1) {
if (condition2) {
// code if both condition1 and condition2 are true
}
}

Example:

int age = 25;


String nationality = "Indian";
if (age >= 18) {
if (nationality.equals("Indian")) {
System.out.println("You are eligible to vote in India.");
}
}

5. switch Statement
Syntax:

switch (expression) {
case value1:
// code for case value1
break;
case value2:
// code for case value2
break;
...
default:
// default code
}

Example:

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

You might also like