if statements in Java

  1. Syntax of the if statement in Java:

    
          if ( boolean expression )       
             one-statement
    
    if ( boolean expression ) { more-than-one-statement }
    if ( boolean expression ) one-statement else one-statement
    if ( boolean expression ) { more-than-one-statement } else { more-than-one-statement }

  2. Boolean Expressions:

  3. List of Relational Operators:

    Relational Operator Description
      > Greater than
      >= Greater than or equal
      < Less than
      <= Less than or equal
      == Equal
      != Not equal

  4. List of Logical Operators:

    Logical Operator Description
      && Logical AND
      || Logical OR
      ! Logical NOT

  5. Examples...

    Example: to test if variable x is between 10 and 20
    
          if ( x >= 10 && x <= 20 )      
    	 ...
          else
    	 ...
    
    

    Example: to test if variable x is outside the interval (10,20)
    
          if ( !(x >= 10 && x <= 20) )      
    	 ...
          else
    	 ...
    
    which is equivalent to:
    
          if ( x < 10 || x > 20 )      
    	 ...
          else
    	 ...
    

  6. Cascading if's

    As part of the pre-requisite, you should already know what cascading if statements do:
          if ( avg >= 90 )
             System.out.println("Grade is A");
          else if ( avg >= 80 )
             System.out.println("Grade is B");
          else if ( avg >= 70 )
             System.out.println("Grade is C");
          else if ( avg >= 60 )
             System.out.println("Grade is D");
          else 
             System.out.println("Grade is F");