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 di...

If statements

 If statements:

        An if statement is a programming construct that allows a program to choose between different paths of execution based on a specified condition. In the previous program, a message like "6 + 2 = 7 is false" is displayed. If you want the message to be "6 + 2 = 7 is incorrect," you can use a selection statement to make this minor modification.

        In Java, there are several types of selection statements available: one-way if statements, two-way if-else statements, nested if statements, multi-way if-else statements, switch statements, and conditional expressions.

        A one-way if statement is used to execute a block of code only if a certain condition is true. The syntax for a one-way if statement is as follows:

if (boolean-expression) {
    statement(s);
}

        The flowchart in Figure-1a visually represents how Java executes the syntax of an if statement. A flowchart is a diagram that illustrates an algorithm or process, displaying the steps as boxes of different shapes and connecting them with arrows to indicate the flow of control. In a flowchart, a diamond-shaped box represents a Boolean condition, and a rectangular box represents statements or actions.

Fig-1

        If the boolean expression evaluates to true, the statements inside the block are executed. Consider the following code as an example:


if (radius >= 0) {
    area = radius * radius * PI;
    System.out.println("The area for the circle of radius " + radius + " is " + area);
}


        The flowchart in Figure-1 b represents the execution flow of the above statement. If the value of `radius` is greater than or equal to 0, the area is calculated and the result is displayed. Otherwise, if the value is less than 0, the two statements inside the block will not be executed.

        The boolean expression is enclosed in parentheses to clearly define its scope. It is important to ensure that the boolean expression is properly formatted. For example, the code in (a) is incorrect and needs to be corrected as shown in (b):


(a) Wrong
if i > 0 {
    System.out.println("i is positive");
}

(b) Correct
if (i > 0) {
    System.out.println("i is positive");
}


        The curly braces that enclose the block of statements can be omitted if the block consists of a single statement. The following two examples are equivalent:


(a)
if (i > 0) {
    System.out.println("i is positive");
}

(b)
if (i > 0)
    System.out.println("i is positive");


        Both examples will execute the `System.out.println("i is positive")` statement if the condition `(i > 0)` is true.

        The following program prompts the user to enter an integer. If the number is a multiple of 5, the program displays "HiFive". If the number is divisible by 2, it displays "HiEven".
  1. import java.util.Scanner;

  2. public class SimpleIfDemo {
  3.     public static void main(String[] args) {
  4.         Scanner input = new Scanner(System.in);
  5.         System.out.println("Enter an integer: ");
  6.         int number = input.nextInt();

  7.         if (number % 5 == 0) {
  8.             System.out.println("HiFive");
  9.         }

  10.         if (number % 2 == 0) {
  11.             System.out.println("HiEven");
  12.         }
  13.     }
  14. }
        In the above program, the `Scanner` class is used to read input from the user. The entered integer is stored in the variable `number`. The program then checks if `number` is divisible by 5 using the condition `number % 5 == 0`. If true, it prints "HiFive". Similarly, it checks if `number` is divisible by 2 using the condition `number % 2 == 0`. If true, it prints "HiEven". If neither condition is true, no message is displayed.

        This program demonstrates the use of multiple `if` statements to perform independent checks on the value of `number` and display different messages based on the results.

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