java if-else program

java if-else program
java programming and c, c++, Matlab, Python, HTML, CSS programming language, and new techniques news and any history.
if-else Statement: When we have to make a decision based on two or more conditions or have to do some special work from the program, then we use the if-else statement. This is usually the detailed form of if statement. Its Syntax is shown below-
java if-else program



















  if ( Expression and Condition )
      {
                      Statement 1;
                      Statement 2;
                       “     “      “
                      Statement n;
        }
        else
        {
                      Statement 3;
                      Statement 4;
                      “      “      “
                      Statement m;
          }
         Sequential Statement a;

According to this Syntax, if Condition is true, then Execution from Statement 1, Statement 2, to Statement n will be done and if If Condition is false, then Program Control, if Statement will leave the block and default Statement of Second Condition Execution will happen


public class IfElseControlStatement
{
  public static void main(String args[])
  {
    float firstNumber = 5787.54f;
    float secondNumber = 8454.12F;
    float result;
   
    if(firstNumber > secondNumber)
    {
      System.out.println("firstNumber is Greater than secondNumber ");
      System.out.println("and secondNumber is Less than firstNumber");
    }
    else 
    {
      System.out.println("secondNumber is Greater than firstNumber ");
      System.out.println("and firstNumber is Less than secondNumber ");
    }
  }
}
// Output
   secondNumber is Greater than firstNumber
   and firstNumber is Less than secondNumber

if Statement in Java

if Statement in Java: If a statement of all Control Statements is the most powerful Statement, through which we can control the flow of Execution of Statements. This is a Two Way Statement, depending on the condition being true or false, the program's control can reach at two different points.

if ( Expression and Condition )      Statement 1;


if ( Expression and Condition )
          {
               Statement 1;
               Statement 2;
               “    “    “
               “    “    “
               Statement n;
          }
          Other Statements

In this, when the condition is true, then all the statements written in the block are executed, then there is the Execution of State statements out of the other statements. But if the condition is false, then Program Control, except for the Block of Condition, simply executes the statements of other statements outside the block.

Whether the condition is true or false, there is also the Execution of State statements outside the Other Statements, because this statement is in the Sequential Flow. Based on this control statement we can develop an example program as follows.


public class IfControlStatement
  {
    public static void main(String args[])
    {
      float firstNumber = 5787.54f;
      float secondNumber = 8454.12F;
      float result;
     
      if(firstNumber > secondNumber){
        System.out.println("firstNumber is Greater than secondNumber ");
        System.out.println("and secondNumber is Less than firstNumber");
      }
     
      if(secondNumber > firstNumber)
      {
        System.out.println("secondNumber is Greater than firstNumber ");
        System.out.println("and firstNumber is Less than secondNumber ");
      }
    }
  }
// Output
   secondNumber is Greater than firstNumber
   and firstNumber is Less than secondNumber

if-else-if ladder: Here, a user can decide among multiple options.The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that it is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.
if (condition)
    statement;
else if (condition)
    statement;
.
.
else
    statement;
Example:
// Java program to illustrate if-else-if ladder
class ifelseifDemo
{
    public static void main(String args[])
    {
        int i = 20;
  
        if (i == 10)
            System.out.println("i is 10");
        else if (i == 15)
            System.out.println("i is 15");
        else if (i == 20)
            System.out.println("i is 20");
        else
            System.out.println("i is not present");
    }
}
Output:
i is 20


































































Comments