java tutorial - The If-Else If Statement, Nested If Statements, Logical Operators in Java - java programming - learn java - java basics - java for beginners



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

Learn java - java tutorial - The If-Else If Statement, Nested If Statements, Logical Operators - java examples - java programs

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
  • If BooleanExpression1 is true, then the statement or block 1 is executed.
  • If BooleanExpression1 is false, then the BooleanExpression2 is tested.
  • If BooleanExpression2 is true, then the statement or block 2 is executed.
  • If BooleanExpression2 is false, then the statement or block 3 is executed.

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:

Logical Operators:

  • Java provides logical operators.
  • Binary logical operators combines two different 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.

Sample Code:

public class  kaashiv_infotech
{
   public static void main(String[] args)
        {
        int a = 100;
		if (a <= 20)
		{
			System.out.println("correct a == 100");
		}
		else
		{
			System.out.println("worng a != 100");
		}
		
	}
}

Output:

worng a != 100

Related Searches to The If-Else If Statement, Nested If Statements, Logical Operators in Java