Skip to main content

Java if else program

Java if else program



Java if else program uses if else to execute statement(s) when a condition holds true. Below is a simple program which explains the usage of if else in Java programming language. In this program, a user inputs marks obtained in an exam, and it is compared with the minimum passing marks. An appropriate message is printed on the screen based on whether the user passed the exam or not.

Java programming if else statement

  1. // If else in Java code
  2. import java.util.Scanner;
  3.  
  4. class IfElse {
  5.   public static void main(String[] args) {
  6.     int marksObtained, passingMarks;
  7.    
  8.     passingMarks = 40;
  9.    
  10.     Scanner input = new Scanner(System.in);
  11.    
  12.     System.out.println("Input marks scored by you");
  13.    
  14.     marksObtained = input.nextInt();
  15.    
  16.     if (marksObtained >= passingMarks) {
  17.       System.out.println("You passed the exam.");
  18.     }
  19.     else {
  20.       System.out.println("Unfortunately, you failed to pass the exam.");
  21.     }
  22.   }
  23. }
Output of program:

In this program both if and else blocks contain only one statement but we can execute as many statements as required.

Nested If Else statements

You can use nested if else which means that you can use if else statements in any if or else block.
  1. import java.util.Scanner;
  2.  
  3. class NestedIfElse {
  4.   public static void main(String[] args) {
  5.     int marksObtained, passingMarks;
  6.     char grade;
  7.    
  8.     passingMarks = 40;
  9.  
  10.     Scanner input = new Scanner(System.in);
  11.  
  12.     System.out.println("Input marks scored by you");
  13.  
  14.     marksObtained = input.nextInt();
  15.  
  16.     if (marksObtained >= passingMarks) {
  17.      
  18.       if (marksObtained > 90)
  19.         grade = 'A';
  20.       else if (marksObtained > 75)
  21.         grade = 'B';
  22.       else if (marksObtained > 60)
  23.         grade = 'C';
  24.       else
  25.         grade = 'D';
  26.          
  27.       System.out.println("You passed the exam and your grade is " + grade);
  28.     }
  29.     else {
  30.       grade = 'F';
  31.       System.out.println("You failed and your grade is " + grade);
  32.     }
  33.   }
  34. }

Comments

Popular posts from this blog

Java while loop

Java while loop Java while loop is used to execute statement(s) until a condition is true. In this tutorial, we will learn to use while loop with examples. First of all, let's discuss while loop syntax: while (condition(s)) { // Body of loop } 1. If the condition(s) holds true then the body of the loop is executed, after execution of the loop body condition is tested again and if the condition is still true then the body of the loop is executed again, and the process repeats until the condition(s) becomes false. The condition evaluates to true or false and if it is a constant, for example, while (c) {…}, where c is a constant, then any non zero value of c is considered to be true, and zero is considered false. 2. You can test multiple conditions such as while   ( a  >  b  &&  c  !=   0 )   {   // Loop body } Loop body is executed till value of variable a is greater than value of...

Install & Use Gujrati Indic Input Font | સરળ છે ગુજરાતી ઈન્ડિક ઇન્પુટ (શ...

Chromecast unboxing