if else in java – Kensoft PH https://kensoftph.com Power up your knowledge in programming Wed, 21 Sep 2022 09:57:15 +0000 en-US hourly 1 https://wordpress.org/?v=6.7.1 https://kensoftph.com/wp-content/uploads/2021/07/cropped-Kensoftph-New-logo-32x32.png if else in java – Kensoft PH https://kensoftph.com 32 32 How to use If Else in Java | 100% best for beginners https://kensoftph.com/if-else-in-java/ https://kensoftph.com/if-else-in-java/#respond Fri, 19 Mar 2021 13:59:00 +0000 https://kensoftph.com/if-else-in-java/ If Else in Java Tutorial

Java has the following conditional statements of If else in java

  • if is used to specify a block of code if the condition is true

  • else if is used to specify a block of code if the first condition is false

  • else is used to specify a block of code if the conditions from if and else if are false

The if statement

How is the if else in Java works? if is used to execute a block of code if the condition is true. See this example code below.

Example code

package ifelse;

import java.util.Scanner;

/**
*
* @author KENSOFT
*/
public class IfElse {

public static void main(String[] args) {
    System.out.print("Rate from 1 - 10: ");
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    if(n >= 8 && n <= 10){
        System.out.println("Excellent");
    }
  }
}

That is an example of executing an if statement in Java

Output

Rate from 1 - 10: 10
Excellent

The else if statement

How the else if statement works? else if is used to execute a block of code if the first condition is false. See this example code below.

Example code

package ifelse;

import java.util.Scanner;

/**
*
* @author KENSOFT
*/
public class IfElse {

    public static void main(String[] args) {
        System.out.print("Rate from 1 - 10: ");
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        if(n >= 8 && n <= 10){
            System.out.println("Excellent");
        }else if(n >= 5 && n <= 7){
            System.out.println("Very Good");
        }
    }
}

That is an example of executing an if statement with else if statement in Java

Output

Rate from 1 - 10: 7
Very Good

The else statement

How does the else statement work? else is used to execute a block of code if the condition of if and else if are false. See this example code below.

Example code

package ifelse;

import java.util.Scanner;

/**
*
* @author KENSOFT
*/
public class IfElse {

    public static void main(String[] args) {
        System.out.print("Rate from 1 - 10: ");
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        if(n >= 8 && n <= 10){
            System.out.println("Excellent");
        }else if(n >= 5 && n <= 7){
            System.out.println("Very Good");
        }else if(n >= 3 && n <= 4){
            System.out.println("Good");
        }else if(n >= 1 && n <= 2){
            System.out.println("Bad");
        }else{
            System.out.println("Rating is out of scope");
        }
    }
}

Output

Rate from 1 - 10: 11
Rating is out of scope

That’s how you do If else in Java. 

If Else in Java

YouTube Video

]]>
https://kensoftph.com/if-else-in-java/feed/ 0 Java Tutorial: if else in java using netbeans nonadult