If else in Java
If-else condition in Java is similar to if-else in C/c++ programming languages. It is used to check whether a particular condition is satisfied or not. if the condition is satisfied it print the statements otherwise it moves to the else block.
We can use single if also to check a condition.
1) if condition :
Syntax :
if(condition){
group of statements
}
Example program showing if condition :
package Hello;
public class TestIf {
private static int i = 5;
public static void main(String[] args) {
if(i>3){
System.out.println("Number is greater than 3");
}
}
}
Output :
Number is greater than 3
2) if else condition :
Syntax :
if(condition){
group of statements
}
else{
group of statements
}
Example program showing if condition :
package Hello;
public class TestIfElse {
private static int i = 2;
public static void main(String[] args) {
if(i>3){
System.out.println("Number is greater than 3");
}
else{
System.out.println("Number is less than 3");
}
}
}
Output :
Number is less than 3
3) if - else if :
Syntax :
if(condition){
group of statements
}
else if{
group of statements
}
else{
group of statements
}
Here is the simple program to check the value of an integer, whether it is greater than, less than, or equal to 5.
package Hello;
public class TestIfElseIf {
private static int i=3;
public static void main(String[] args) {
if(i<5){
System.out.println("i is less than 5");
}
else if(i==5){
System.out.println("i is equal to 5");
}
else{
System.out.println("i is greater than 5");
}
}
}
Output :
i is less than 5
4) Nested if in Java
if block in some other if block is known as nested if block.
Here are some programs showing nested if conditions.
package Hello;
public class TestNestedIf{
private static int i=3;
public static void main(String[] args) {
if(i<5){
System.out.println("i is less than 5");
if(i>2){
System.out.println("i is greater than 2 less than 5");
}
}
}
}
Output :
i is greater than 2 less than 5
Here is another program :
package Hello;
public class TestNestedIf2 {
private static int i=2;
public static void main(String[] args) {
if(i<5){
System.out.println("i is less than 5");
if(i>2){
System.out.println("i is greater than 2 less than 5");
}
else{
System.out.println("i is less than or equal to 2");
}
}
}
}
Output :
i is less than 5
i is less than or equal to 2
program showing if-else in else block :
package Hello;
public class TestNestedIf2 {
private static int i=9;
public static void main(String[] args) {
if(i<5){
System.out.println("i is less than 5");
}
else{
if(i==5){
System.out.println("i is equal to 5");
}
else{
System.out.println("i is greater than 5");
}
}
}
}
Output :
i is greater than 5
Now a simple program showing if and else-if block in else block
package Hello;
public class TestNestedIf2 {
private static int i=7;
public static void main(String[] args) {
if(i<5){
System.out.println("i is less than 5");
}
else{
if(i==5){
System.out.println("i is equal to 5");
}
else if(i>5 && i<7){
System.out.println("i is greater than 5 and less than 7");
}
else{
System.out.println("i is greater than or equal to 7");
}
}
}
}
Output
i is greater than or equal to 7
Multiple if conditions :
Syntax :
if(condition){
statements
}
if(condition){
statements
}
if(condition){
statements
}
.................
Sample program showing multiple if conditions
package Hello;
public class TestMultipleIf {
private static int i=8;
public static void main(String[] args) {
if(i>2){
System.out.println("i is greater than 2");
}
if(i>5){
System.out.println("i is greater than 5");
}
if(i>7){
System.out.println("i is greater than 7");
}
}
}
Output :
i is greater than 2
i is greater than 5
i is greater than 7
No comments:
Post a Comment