Nested if and Multi-Way if-else Statements

Image
Nested if and Multi-Way if-else Statements:           An if statement can be nested inside another if statement, forming a nested if statement. This allows for more complex decision-making in a program.           In a nested if statement, an if statement is placed inside another if statement. The inner if statement is considered to be nested within the outer if statement. There is no limit to the depth of nesting and an inner if statement can also contain another if statement.           Here is an example of a nested if statement: if (i > k) {     if (j > k)         System.out.println("i and j are greater than k"); } else {     System.out.println("i is less than or equal to k"); }           In this example, the inner if statement ` if (j > k) ` is nested inside the outer if statement ` if (i > k) `. This allows for different actions to be taken based on multiple conditions.           A nested if statement can be used to implement multiple alternatives. F

Two-Way if-else Statements

Two-Way if-else Statements:

        An if-else statement allows a program to choose between two different execution paths based on whether a condition is true or false.

        While a one-way if statement performs an action only when the specified condition is true, a two-way if-else statement provides alternative actions to be taken depending on whether the condition is true or false.

        The syntax for a two-way if-else statement is as follows:

  1. if (boolean-expression) {
  2.     statement(s)-for-the-true-case;
  3. }
  4. else {
  5.     statement(s)-for-the-false-case;
  6. }


        The flowchart of this statement is depicted in Figure -1.
  
Figure-1

  
        Figure-1 An if-else statement executes statements for the true case if the Boolean expression evaluates to true; otherwise, statements for the false case are executed. 

       If the boolean expression evaluates to true, the statement(s) for the true case is executed. If it evaluates to false, the statement(s) for the false case is executed.

        For instance, consider the following code:

  1. if (radius >= 0) {
  2.     area = radius * radius * PI;
  3.     System.out.println("The area for the circle of radius " + radius + " is " + area);
  4. }
  5. else {
  6.     System.out.println("Negative input");
  7. }

        If the condition `radius >= 0` is true, the area is computed and displayed. If it is false, the message "Negative input" is displayed.

        Braces enclosing the statements within the if and else blocks can be omitted if there is only one statement within each block.

        Here is another example using the if-else statement. The code checks whether a number is even or odd:

    1. if (number % 2 == 0)
    2.     System.out.println(number + " is even.");
    3. else
    4.     System.out.println(number + " is odd.");

        If the condition `number % 2 == 0` is true, the number is considered even and the corresponding message is printed. Otherwise, if the condition is false, the number is considered odd and a different message is printed.

Comments

Popular posts from this blog

Types of Operators (Augmented assignment, Increment and Decrement, Logical)

History of The Java

Creating, compiling and Executing a simple java program