[go: up one dir, main page]

0% found this document useful (0 votes)
9 views2 pages

Loops

The document discusses four types of conditional statements in Java: if statements execute code if a condition is true, if-else statements execute one code block if a condition is true and another if false, if-else if-else statements allow checking multiple conditions in sequence, and switch statements allow executing different code blocks based on the value of an expression and can replace multiple if-else statements. Examples are provided for the syntax and usage of each statement type.

Uploaded by

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

Loops

The document discusses four types of conditional statements in Java: if statements execute code if a condition is true, if-else statements execute one code block if a condition is true and another if false, if-else if-else statements allow checking multiple conditions in sequence, and switch statements allow executing different code blocks based on the value of an expression and can replace multiple if-else statements. Examples are provided for the syntax and usage of each statement type.

Uploaded by

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

Conditional Statements in Java

1. if Statement:
Syntax:
java
Copy code
if (condition) {
// code to be executed if the condition is true
}
Example:
java
Copy code
int num = 10;
if (num > 0) {
System.out.println("Number is positive");
}
2. if-else Statement:
Syntax:
java
Copy code
if (condition) {
// code to be executed if the condition is true
} else {
// code to be executed if the condition is false
}
Example:
java
Copy code
int num = -5;
if (num > 0) {
System.out.println("Number is positive");
} else {
System.out.println("Number is non-positive");
}
3. if-else if-else Statement:
Syntax:
java
Copy code
if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition2 is true
} else {
// code to be executed if none of the conditions is true
}
Example:
java
Copy code
int num = 0;
if (num > 0) {
System.out.println("Number is positive");
} else if (num < 0) {
System.out.println("Number is negative");
} else {
System.out.println("Number is zero");
}
4. switch Statement:
Used for multiple branches based on the value of an expression.
Syntax:
java
Copy code
switch (expression) {
case value1:
// code to be executed if expression equals value1
break;
case value2:
// code to be executed if expression equals value2
break;
// ... more cases
default:
// code to be executed if none of the cases match
}
Example:
java
Copy code
int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
// ... more cases
default:
System.out.println("Unknown day");
}
These notes cover the basic syntax and examples of if, if-else, if-else if-else,
and switch statements in Java, offering different ways to handle conditional logic
in your programs.

You might also like