Day 4 : Conditional Statements in Java
POOJAASHREE P V
Full Stack Developer | JavaScript | Bootstrap | React | Spring Boot | MySQL
Conditional Statements :
Conditional statements are control flow statements that allow you to execute different blocks of code based on whether a specified condition evaluates to true or false. These statements help in making decisions within a program, enabling it to behave differently under different circumstances.
if statement :
It is the most basic statement among all control flow statements in Java. It evaluates a Boolean expression and enables the program to enter a block of code if the expression evaluates to true.
Syntax :
if(condition) {
statement 1; // executes when condition is true
}
Example :
public class IfExample {
public static void main(String[] args) {
int a = 10;
int b = 5
// Check in if condition a is greater than b
if (a> b) {
System.out.println("a is greater"); // Output : a is greater
}
}
}
In this example :
if-else statement :
The if-else statement is an extension to the if-statement, which uses another block of code, i.e., else block. The else block is executed if the condition of the if-block is evaluated as false.
Syntax :
if(condition) {
statement 1; // executes when condition is true
}
else{
statement 2; // executes when condition is false
}
Example :
public class ElseIfExample {
public static void main(String[] args) {
int a = 10;
int b = 25;
// Check in if condition a is greater than b
if (a> b) {
System.out.println("a is greater"); // Output : a is greater
}
else {
System.out.println("b is greater");
}
}
}
In this example :
if-else-if ladder :
The if-else-if statement contains the if-statement followed by multiple else-if statements. In other words, we can say that it is the chain of if-else statements that create a decision tree where the program may enter in the block of code where the condition is true. We can also define an else statement at the end of the chain.
Syntax :
if(condition 1) {
statement 1; // executes when condition 1 is true
}
else if(condition 2) {
statement 2; // executes when condition 2 is true
}
else {
statement 2; // executes when all the conditions are false
}
Example :
public class IfElseIfLadderExample {
public static void main(String[] args) {
int num1 = 20;
int num2 = 50;
int num3 = 25;
int num4 = 30;
if (num1 >= num2 && num1 >= num3 && num1 >= num4) {
System.out.println("The largest number is : " + num1);
}
else if (num2 >= num1 && num2 >= num3 && num2 >= num4) {
System.out.println("The largest number is : " + num2);
}
else if (num3 >= num1 && num3 >= num2 && num3 >= num4) {
System.out.println("The largest number is : " + num3);
}
else {
System.out.println("The largest number is : " + num4);
}
}
}
In this example :
Nested if statement :
In nested if-statements, the if statement can contain a if or if-else statement inside another if or else-if statement.
Syntax :
if(condition 1) {
statement 1; // executes when condition 1 is true
if(condition 2) {
statement 2; // executes when condition 2 is true
}
else{
statement 2; // executes when condition 2 is false
}
}
Example :
public class LargestThreeNumbers {
public static void main(String[] args) {
int num1 = 20;
int num2 = 50;
int num3 = 25;
if (num1 >= num2) {
if (num1 >= num3) {
System.out.println("The largest number is : " + num1);
}
else {
System.out.println("The largest number is : " + num3);
}
}
else {
if (num2 >= num3) {
System.out.println("The largest number is : " + num2);
}
else {
System.out.println("The largest number is : " + num3);
}
}
}
}
In this example :
Switch Statement :
Switch statements are similar to if-else-if statements. The switch statement contains multiple blocks of code called cases and a single case is executed based on the variable which is being switched. The switch statement is easier to use instead of if-else-if statements. It also enhances the readability of the program.
Here are some key points to keep in mind about the switch statement in Java :
Syntax :
switch (expression){
case value1:
statement1;
break;
case value2:
statement2;
break;
.
.
.
case valueN:
statementN;
break;
default:
default statement;
}
Example :
public class SwitchExample {
public static void main(String[] args) {
int day = 3;
String dayName;
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
case 4:
dayName = "Thursday";
break;
case 5:
dayName = "Friday";
break;
case 6:
dayName = "Saturday";
break;
case 7:
dayName = "Sunday";
break;
default:
dayName = "Invalid day";
break;
}
System.out.println("Day " + day + " is " + dayName);
}
}
In this example :