Java if-else-if ladder with Example

A programming language uses control statements to control the flow of execution of a program. In Java programming, we can control the flow of execution of a program based on some conditions. Java control statements can be put into the following three categories: selection, iteration, and jump.

Java if-else-if ladder:

Java if-else-if ladder is used to work on multiple conditions. 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 if 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. The final else acts as a default condition; that is, if all other conditional tests fail, then the last else statement is performed. If there is no final else and all other conditions are false, then no action will take place.

Syntax of if-else-if:

else if(condition2){
else if(condition3){
//default statement;

Flowchart if-else-if ladder:

java if-else-if ladder

Java if-else-if ladder Example:

class IfElseLadder {
    public static void main(String[] args) {

        int examscore = 75;
        char grade;

        if(examscore >= 90) {
            grade = 'A';
        } else if (examscore >= 80) {
            grade = 'B';
        } else if(examscore >= 70) {
            grade = 'C';
        } else if(examscore >= 60) {
            grade = 'D';
        } else {
            grade = 'F';
        System.out.println("Grade is = " + grade);

 Grade is = C 

You may also like these related posts:

1. Java nested-if statement with example.
2. Java if-else statement with example.
3. Java if statement with example.
4. Types of operators in java with example.
5. Types of variables in java with example.
6. How to make first program in java.
7. Java collection framework complete tutorial.
8. Top 100 java interview question and answer.
9. Java serialization with example.
10. Java Inheritance complete tutorial.
Java if-else-if ladder with Example Java if-else-if ladder with Example Reviewed by Prashant Srivastava on December 17, 2019 Rating: 5

No comments:

Powered by Blogger.