In Java programming language, it is equipped with a specific statement through which we can check a condition and execute the code, depending on whether the condition is true or false. Such statements are called conditional statements and are a form of a composite statement also.
In Java, there are two types of conditional statements:
1. java if-else statement
2. java switch statement,
In java we basically use two conditional statements, the first one is if/else and the second one is switch statements
Watch Step by Step guide on the topic Conditional Statements in Java:
Java Code for if-else statement:
public class ConditionalStatement{
public static void main(String[] args){
int a = 10;
if(a>10){
System.out.println (“A is greater than 10”);
}
else{ System.ou.println (“A is less than or equal to 10”);}
}
}
Java Code for if-else statement:
public class ConditionalStatement{
public static void main(String[] args){
int a = 10;
if(a>10){
System.out.println (“A is greater than 10”);
}else if (a == 10){
System.out.println (“A is equals to 10”);
} else{
System.ou.println (“A is less than 10”);}
}
}
Java Code for if-else statement another one:
public class ConditionalStatement{
public static void main(String[] args){
int passmarks = 25;
if(passmarks<35){
System.out.println(“Student has failed”);
}else if (passmarks >=35){
System.out.println(“Student has passed”);}
}
}
Java Code for switch statement:
public class ConditionalStatement{
public static void main(String[] args){
int passmarks = 35;
switch(passmarks){
case 35:
System.out.println(“Value is 35”);
break;
case 40:
System.out.println(“Value is 40”);
break;
case 45:
System.out.println(“Value is 45”);
break;
default:
System.out.println(“Value is default”);
}
}
}