Java if-else statement

Java if-else Statement:
 The Java if-else.statement check whether the condition is true or false. where the condition returns only boolean values which is either true or false. If the returning boolean  value is true then the if statement will be executed, if the returning boolean value is false then it will executes the else statement.

Note: else statement without if statement are not acceptable

There are different types of if statement, which we will be discussed in detail with several examples. The types are:
  • if statement
  • if-else statement
  • if-else-if ladder statement
  • nested if statement

Java if statement:
The java if statement will be executed only the returning or boolean value is true.
java,if else in java,java if else,if else condition in java,java if else statement,if else,else,if statement,java tutorial,if else statement in java,if,java if statement,else if,java programming,statement,java if else statment,java nested if else statement,java if eseif else statment,java if else statements tutorial,nested if statement,java programming tutorial
Java if statement


Syntax:
if(condition){
// executing the true statement
}

if statement with boolean value
Example 1:


public class Example1 
{
public static void main(String[] args) 
{
if(true)
{
System.out.println("Executing the true statement");
}
System.out.println("After terminating the if statement");
}
}

Output:
Executing the true statement
After terminating the if statement

Example 2:

public class Example2
{
public static void main(String[] args) 
{
if(!true) // !true = false
{
System.out.println("Executing the true statement");
}
System.out.println("After terminating the if statement");
}
}

Output:
After terminating the if statement


if statemnet with the condition

Example 3:

public class Example3
{
public static void main(String[] args) 
{
int a=10,b=20;
if(b>a) // returning boolean value true
{
System.out.println("b is greater than a ");
}
System.out.println("After terminating the if statement");
}
}

Output:

b is greater than a
After terminating the if statement

Example 4:

public class Example4
{
public static void main(String[] args) 
{
int a=10,b=20;
if(a>b)  // returning boolean value false
{
System.out.println("b is greater than a ");
}
System.out.println("After terminating the if statement");
}
}


Output:
After terminating the if statement


Java if-else statement:
The java if-else statement, the if statement will be execute if the boolean value is true else it will execute the else statement.


java,if else in java,java if else,if else condition in java,java if else statement,if else,else,if statement,java tutorial,if else statement in java,if,java if statement,else if,java programming,statement,java if else statment,java nested if else statement,java if eseif else statment,java if else statements tutorial,nested if statement,java programming tutorial
Java if-else statement


Syntax:
if(condition){
// executing the true statement
}
else{
// executing the false statement
}

Example 1:

public class Example1
{
public static void main(String[] args)
{
int a=20,b=10;
if(a>b) // returns the boolean value true
{
System.out.println("executing the if statement ");
}
else
{
System.out.println("executing the else statement");
}
}
}

Output:
executing the if statement

Example 2:

public class Example2
{
public static void main(String[] args)
{
int a=20,b=10;
if(a<b) // returning the boolean value false
{
System.out.println("executing the if statement ");
}
else
{
System.out.println("executing the else statement");
}
}
}

Output:
executing the else statement


Java if-else-if ladder statement:
Once the if statement condition return false it will executes the else satement where there is another if condition to be check. In another words, this ladder statement executes one statement among the multiple conditions.

java,if else in java,java if else,if else condition in java,java if else statement,if else,else,if statement,java tutorial,if else statement in java,if,java if statement,else if,java programming,statement,java if else statment,java nested if else statement,java if eseif else statment,java if else statements tutorial,nested if statement,java programming tutorial
Java if-else-if ladder statement


Syntax:
if(condition1){
// executing the conditon1 true statement
}
else if(condition2){
// executing the condition2 true statement
}
else if(condition3){
// executing the condition 3 true statement
}
else{
// executing false statement
}

Example:

public class Example
{
public static void main(String[] args)
{
int marks=40;
if(marks<50)
{
System.out.println("you marks is less than 50 ");
}
else if(marks>=60 && marks<=90)
{
System.out.println("you are marks is between 60 to 90.");
}
else
{
System.out.println("you are marks is at max");
}
}
}

Output:
you marks is less then 50


Java nested if statement:
This nested if statement is used to nest the if statement inside the if statement or else statement inside if statement.

java,if else in java,java if else,if else condition in java,java if else statement,if else,else,if statement,java tutorial,if else statement in java,if,java if statement,else if,java programming,statement,java if else statment,java nested if else statement,java if eseif else statment,java if else statements tutorial,nested if statement,java programming tutorial
Nested if statement


Syntax:
if(condition){
      if (condition){
         // nested if statement - if inside if
      }
     else{
        // executing nested if statement's else statement
     }
}
else{
   if(condition){
     // nested else inside if statement
   }
   else{
    // appropriate else statement for the if statement
   }
 // executing the main if statement's else statement
}

Example:

public class Example
{
public static void main(String[] args)
{
int age=30;
if(age<50)
{
if(age>20)
{
System.out.println("your age is between 20 to 50");
}
else
{
System.out.println("your age is below 20");
}
}
else
{
if(age<70)
{
System.out.println("your age is between 50 to 70");
}
else
{
System.out.println("your age is 70 or above 70");
}
}
}
}

Output:
your age is between 20 to 50


Comments

Popular posts from this blog

PHP Login and Registration script

Multiple image upload to database and display from it

Java program to display diamond pattern