If-Else Statements in Java

  • an if-else statement allows for conditional execution of code blocks based on a boolean expression. If the condition evaluates to true, the code within the "if" block executes; otherwise, the code within the "else" block executes.

  • Basic Structure :
  • The basic syntax of an if-else statement is:
    if (condition){
    # code to execute if the condition is True
    }else
    # code to execute if the condition is False
    }

    The if keyword is followed by a condition that is evaluated as True or False.

    If the condition is True, the indented code block after the if statement is executed.

    If the condition is False, the indented code block after the else statement (if present) is executed.


  • Example:
  • Example-1You are going to open a bank account. If your age is greater than 18 then you can open an account. Get your age by input and print “YES” if you can open an another account otherwise print “NO”.

    Java code
    import java.util.*; class HelloPBA { public static void main(String args[]){ int age; Scanner sc=new Scanner(System.in); System.out.println("Enter your age :"); age=sc.nextInt(); if(age>18){ System.out.println("YES"); }else{ System.out.println("NO"); } } }

    OUTPUT:

    Enter your age :
    20
    YES


    This program asks the user for their age. It then uses an if statement to check if the age are greater than 18 . If the condition is True, the "YES" message is printed. Otherwise, the "NO" message is printed.


  • Key Points :
  • The condition can be any expression that evaluates to True or False. Common comparisons include == (equal to), != (not equal to), < (less than),> (greater than), <= (less than or equal to), and>= (greater than or equal to).

    You can have multiple lines of code indented within the if and else blocks

    If the condition is False, and there's no else block, no code is executed after the if statement.


    Example-2:Admission to a professional course is subjected to the following –
    i) Marks in Math’s >=60
    ii) Marks in Physics >=50
    iii) Marks in Chemistry >=40
    iv) Total in all three subjects >=200
    OR
    Total in Math’s and physics >=150.Given the marks in the three subjects, write a program to process the applications to the eligible candidates.

    Java code
    import java.util.*; class HelloPBA { public static void main(String args[]){ int M,P,C,T1,T2; Scanner sc=new Scanner(System.in); System.out.println("Math :"); M=sc.nextInt(); System.out.println("Physics :"); P=sc.nextInt(); System.out.println("Chemistry :"); C=sc.nextInt(); T1=M+P+C; T2=M+P; if(M>=60 && P>=50 && C>=40 && T1>=200 || T2>=150){ System.out.println("Eligible candidates."); }else{ System.out.println("Not eligible candidates."); } } }

    OUTPUT:

    Math :
    99
    Physics :
    70
    Chemistry :
    60
    Eligible candidates.

    Example-3: Take two integers indicating the x and y coordinate of a two-dimensional graph paper where the center point is x=0, y=0.Now print the quadrant of the given point. [If user gives input (4,5) you should print ‘First Quadrant’; If user gives input (-4,-5) you should print ‘Third Quadrant’]

    Java code
    import java.util.*; class HelloPBA { public static void main(String args[]){ int x,y; Scanner sc=new Scanner(System.in); System.out.println("Enter x : "); x=sc.nextInt(); System.out.println("Enter Y : "); y=sc.nextInt(); if(x>0 && y>0){ System.out.println("First Quadrant."); } if(x<0 && y>0){ System.out.println("Second Quadrant."); } if(x<0 && y<0){ System.out.println("Third Quadrant."); }else{ System.out.print("Fourth Quadrant."); } } }s

    OUTPUT: Enter x :
    3
    Enter Y :
    -1
    Fourth Quadrant.

  • Practice Exercises:
  • 1. Write a program that asks the user for a number and checks if it's positive, negative, or zero.

    2. Write a program that asks the user for the day of the week (as a number, e.g., 1 for Monday) and prints the corresponding day name.


  • By mastering if-else statements, you can add logic and decision-making capabilities to your Python programs, making them more versatile and interactive.