Java – The If-Else If Statement, Nested If Statements, Logical Operators

The If-Else If Statement, Nested If Statements, Logical Operators

    • In a decision structure’s simplest form certain statements are executed only when…
      • a specific condition exists.
    • It is said that the statements inside of the decision structure are…
      • conditionally executed.
    • Relational Operators determine whether…
      • a specific relationship exist between two values.
    • Some relational operators in Java are…
      • > , < , >= , <= , == , !=
    • General for of an if statement:

    • A Flag is…
      • a boolean variable that signals when some condition exists in a program.
    • When Java compares characters, it compares the character’s…
      • Unicode values


    • General form of an if-else statement:

The if-else if Statement

    • Sometimes you need to be able to test a series of conditions
      • You can do this with the if-else if statement
    • General form:

      • If BooleanExpression1 is true, then statement or block 1 is executed.
      • If BooleanExpression1 is false, then BooleanExpression2 is tested.
        • If BooleanExpression2 is true, then statement or block 2 is executed.
        • If BooleanExpression2 is false, then statement or block 3 is executed.
      • Note: You can have as many if else clauses as is needed.

if-else if Example

    • New Topics:
      • if-else if Statement
      • Using else as an error case

Nested if Statements

    • Nesting is enclosing one structure inside of another.
      • A block in Java can contain any valid Java code, this includes other if statements:

    • If BooleanExpression1 is true and BooleanExpression2 is true , what is executed?
      • statement1 , statement2 , statement3 , statement4
    • If BooleanExpression1 is true and BooleanExpression2 is false , what is executed?
      • statement3 , statement4
    • If BooleanExpression1 is false, what is executed?
      • Nothing

Nested if Statements Example

    • New Topics:
      • Nested if Statements

Logical Operators

    • Java provides logical operators.
      • The binary logical operators combine two boolean expressions into one.
      • The unary logical operator switches the value of a boolean expression.
      • Binary logical operators have lower precedence than relational operators (they will be evaluated after)
      • NOT has the same precedence as negation.


Operator Meaning Kind
&& AND Binary
|| OR Binary
! NOT Unary

Logical Operator Truth Tables

    • Truth Tables show the result of a logical expression based on the values of the operands.


Op1 Op2 Op1 && Op2
true true true
true false false
false true false
false false false


Op1 Op2 Op1 || Op2
true true true
true false true
false true true
false false false


Op1 !Op1
true false
false true

Logical Operator Practice

    • 2 > 3 && 4 < 5
      • false – first operand is false
    • 2 < 3 && 4 < 5
      • true
    • 2 > 3 || 4 < 5
      • true
    • 2 > 3 || 4 > 5
      • false – both operands are false
    • !(2 > 3)
      • true – operand is false

Logical AND Example

    • New Topics:
      • Logical AND

Logical AND and Nesting

    • If we have the && operator, do we need nesting?

Logical AND and Nesting

    • Answer: Yes, to act if one of the conditions is not met:

Example :


Partner Sites

  • Mir

    java is a programming language and computing platform first released by Sun Microsystems in 1995. There are lots of applications and websites that will not work unless you have java installed, and more are created every day.
    java was developed by Sun microsystem as an Object oriented language for general purpose business applications. The target of java is to write a program once and then run this program on multiple operating systems.The first publicly available version of java ( java 1.0) was released in ‘95. Sun Microsystems was acquired by the Oracle Corporation in 2k10. Oracle has now the steermanship for java . In 2k06 Sun started to make java accessible under the GNU General Public License (GPL). Oracle continued this plan called Open JDK.

    Over time new improved versions of java have been released. The present version of java is java 1.9 which is also known as java 9.
    java is defined by a specification and consists of a programming language, a compiler, core libraries and a runtime ( java virtual machine) The java runtime permit software developers to write program code in other languages than the java programming language which still runs on the java virtual machine. The java platform is usually associated with the java virtual machine and the java core libraries.
    The java syntax is similar to C++. java is case-sensitive, e.g., variables called myValue andmyvalue are treated as different variables.

    Basic topics Covered in java is
    Introduction of java

    JDK vs JVM vs JVM

    Java Data Types

    Java Operators

    Java Loop Control

    Java Decision Making

    Java Array

    Java String

    Class And Object

    This Keyword in Java

    Static Keyword in Java

    Constructor in Java

    Overloading in Java

    Overriding in Java

  • In Java programming language there are two selection statements if-else and switch statement to control the execution flow.