Conditional Statements:

In Java, control statements are used to control the flow of execution within a program. They determine the order in which statements are executed based on certain conditions. The following are the main control statements in Java:



if statement: Executes a block of code if a specified condition is true.


Certainly! Here's an example that demonstrates the usage of the if statement in Java:

java
public class IfExample
public static void main(String[] args) {
int x = 10; int y = 5
if (x > y) { 
 System.out.println("x is greater than y");
 } 
if (x < y) { 
 System.out.println("x is less than y"); }
if (x == y) { 
 System.out.println("x is equal to y"); }
 } }

In this example, we have two variables, x and y, initialized with the values 10 and 5 respectively. The if statement is used to perform different actions based on the comparison results.

The first if statement checks whether x is greater than y. If the condition is true, it prints "x is greater than y". In this case, since 10 is indeed greater than 5, this condition evaluates to true and the corresponding message is printed.

The second if statement checks whether x is less than y. Since 10 is not less than 5, this condition evaluates to false, and the corresponding message is not printed.

The third if statement checks whether x is equal to y. Since 10 is not equal to 5, this condition also evaluates to false, and the corresponding message is not printed.

So, when you run this program, it will output only "x is greater than y".


if-else statement: Executes a block of code if a condition is true, and another block if the condition is false.

Certainly! Here's an example of using an "if-else" statement in Java:

java
// Example of if-else statement 
int age = 20;
if (age >= 18) {
 System.out.println("You are eligible to vote.");
 } 
else
 System.out.println("You are not eligible to vote yet."); 
}

In this Java example, the variable age is declared and assigned a value of 20. The if statement checks if age is greater than or equal to 18. If the condition is true, the code block inside the if statement is executed, and it prints "You are eligible to vote." If the condition is false, the code block inside the else statement is executed, and it prints "You are not eligible to vote yet."

Based on the value of age, the output would be:

css
You are eligible to vote.

This is because the condition age >= 18 is true, as 20 is greater than 18.


else-if statement: Executes a block of code if the first condition is false and a specified condition is true.

Certainly! Here's an example of using an "if-else if-else" statement in Java:

java
// Example of if-else if-else statement 
int score = 85;
if (score >= 90) { 
 System.out.println("Excellent! You've got an A."); } 
else if (score >= 80) { System.out.println("Good job! You've got a B."); }
else if (score >= 70) { System.out.println("Not bad! You've got a C."); } 
else if (score >= 60) { System.out.println("Keep working! You've got a D."); }
else { System.out.println("Sorry! You've failed."); 
}

In this Java example, the variable score is declared and assigned a value of 85. The "if-else if-else" statement checks the score against multiple conditions in sequence. The first condition checks if the score is greater than or equal to 90, and if it is true, it prints "Excellent! You've got an A." If the first condition is false, it moves to the next condition, and so on. If none of the conditions are true, the code block inside the else statement is executed, and it prints "Sorry! You've failed."

Based on the value of score, the output would be:

css
Good job! You've got a B.

This is because the condition score >= 80 is true, as 85 is greater than or equal to 80, but it is not true for the first condition score >= 90.


switch statement: Evaluates an expression and executes code blocks based on matching cases.

Certainly! Here's an example of using a "switch" statement in Java:

java
// Example of switch statement
int dayOfWeek = 3
String dayName; 
switch (dayOfWeek) { 
case 1: dayName = "Sunday"break;
case 2: dayName = "Monday"; break;
case 3: dayName = "Tuesday"; break
case 4: dayName = "Wednesday"; break
case 5: dayName = "Thursday"; break
case 6: dayName = "Friday"; break
case 7: dayName = "Saturday"; break
default: dayName = "Invalid day"; break;
 }
 System.out.println("The day is: " + dayName);

In this Java example, the variable dayOfWeek is declared and assigned a value of 3, which represents Tuesday. The "switch" statement evaluates the value of dayOfWeek and matches it to the corresponding case label. In this case, the value of dayOfWeek is 3, so the code block inside the case 3: is executed, and it assigns the value "Tuesday" to the dayName variable.

If the value of dayOfWeek does not match any of the case labels, the code block inside the default case is executed, and it assigns the value "Invalid day" to the dayName variable.

The output of this code would be:

csharp
The day is: Tuesday

This is because dayOfWeek is 3, and it matches the case label case 3:, which sets dayName to "Tuesday".