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 program to print alphabets

Java program to print alphabets Java program to print alphabets on screen, i.e., a, b, c, ..., z; in lower case. Java source code class  Alphabets {     public   static   void  main ( String  args [ ] )     {        char  ch ;          for   ( ch  =   'a' ;  ch  <=   'z' ;  ch ++ )           System . out . println ( ch ) ;     } } You can easily modify the above java program to print alphabets in upper case. Download  Alphabets  program class file. Output of program: Printing alphabets using a while loop (Only the body of the main method is shown): char  c  =   'a' ;   while   ( c  <=   'z' )   {    System . out . println ( c ) ;   c ++; } Using a do while loop: char  c  =...

Java program to print multiplication table

Java program to print multiplication table of a number entered by a user using a  for  loop. You can modify it for  while  or  do while  loop for practice. Using nested loops (a loop inside another loop), we can print tables of numbers between a given range say a to b, for example, if the input numbers are '3' and '6' then tables of '3', '4', '5' and '6' will be printed. Java programming source code import   java.util.Scanner ;   class   MultiplicationTable {     public   static   void   main ( String   args [ ] )     {       int   n, c ;       System . out . println ( "Enter an integer to print it's multiplication table" ) ;     Scanner in   =   new   Scanner ( System . in ) ;     n   =   in. nextInt ( ) ;       System . out . println ( "Multiplication table of "   + ...

Use phone camera as a webcam without root in Hindi/urdu.