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

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

  Types of Operators :

  •  Augmented Assignment Operators:

        Augmented operators, also known as compound assignment operators, allow you to combine arithmetic operators with the assignment operator in a concise way. Instead of writing separate statements to perform an operation and assign the result back to a variable, you can use augmented operators to achieve the same result in a more compact form.

        For example, let's consider the statement `count = count + 1;`. This statement increases the value of the variable `count` by 1. In Java, you can use the addition assignment operator (`+=`) to achieve the same result. So, the statement can be written as `count += 1;`.

        The `+=` operator is an example of an augmented assignment operator. It combines the addition operator (`+`) with the assignment operator (`=`). This operator adds the value on the right side of the operator to the current value of the variable on the left side, and then assigns the result back to the variable.

Java provides several other augmented assignment operators, as shown in Table-1:

Table-1

        These operators can be used with arithmetic operations such as addition, subtraction, multiplication, division, and remainder. They are useful when you want to update the value of a variable by performing an operation on its current value and assigning the result back to the same variable. Using augmented operators can make your code more concise and readable.

The augmented assignment operator is performed last after all the other operators in the expression are evaluated. For example, x /= 4 + 5.5 * 1.5;

           is same as x = x / (4 + 5.5 * 1.5); 

  •  Increment and Decrement Operators:

        The increment operator (`++`) and decrement operator (`--`) are used to increase and decrease the value of a variable by 1, respectively.

        These operators provide a shorthand way to increment and decrement a variable by 1, which is a common operation in many programming tasks. For example, consider the following code:

  1. int i = 3, j = 3;
  2. i++; // i becomes 4
  3. j--; // j becomes 2

        In this code, `i++` is pronounced as "i plus plus" and `j--` is pronounced as "j minus minus." These operators are called postfix increment and postfix decrement because the `++` and `--` operators are placed after the variable. They can also be placed before the variable, resulting in a prefix increment (`++i`) and prefix decrement (`--j`), respectively.

So, in the code snippet:

  1. int i = 3, j = 3;
  2. ++i; // i becomes 4
  3. --j; // j becomes 2

        Here, `++i` increments `i` by 1 and `--j` decrements `j` by 1. These operators are known as prefix increment and prefix decrement.

        The effects of `i++` and `++i`, or `j--` and `--j`, are the same in the examples mentioned above. However, their effects can differ when they are used in statements that do more than just incrementing or decrementing a variable. Table 2.5 describes the differences between them and provides examples to illustrate their behavior.

  • Logical:

Logical operators in Java are used to perform logical operations on boolean values. There are three logical operators in Java: `&&` (logical AND), `||` (logical OR), and `!` (logical NOT).


1. Logical AND (`&&`): The logical AND operator returns `true` if both operands are `true`, and `false` otherwise. It evaluates the second operand only if the first operand is `true`. Here's the truth table for the logical AND operator:



 2. Logical OR (`||`): The logical OR operator returns `true` if at least one of the operands is `true`, and `false` otherwise. It evaluates the second operand only if the first operand is `false`. Here's the truth table for the logical OR operator:



3. Logical NOT (`!`): The logical NOT operator is a unary operator that negates the value of its operand. If the operand is `true`, the operator returns `false`, and if the operand is `false`, the operator returns `true`.


Here's an example code snippet demonstrating the use of logical operators:

  1. boolean a = true;
  2. boolean b = false;

  3. boolean result1 = a && b; // Logical AND
  4. boolean result2 = a || b; // Logical OR
  5. boolean result3 = !a;    // Logical NOT

  6. System.out.println(result1); // Output: false
  7. System.out.println(result2); // Output: true
  8. System.out.println(result3); // Output: false


        In this example, `result1` is `false` because `a` is `true` and `b` is `false`, `result2` is `true` because `a` is `true`, and `result3` is `false` because `a` is `true` and `!` negates it. 

Comments

Popular posts from this blog

History of The Java

Creating, compiling and Executing a simple java program