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

Review

    • 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

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com