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

Evaluating Expressions and operator Precedence

 Evaluating Expressions and operator Precedence:

        Java expressions follow the same evaluation rules as arithmetic expressions. When writing a numeric expression in Java, you can directly translate the arithmetic expression using Java operators.

        For example, consider the arithmetic expression:

(3 + 4x /5 )- (10(y - 5)(a + b + c) / x) + 9((4/x) + (9 + x)y) 

        This can be translated into a Java expression as:

(3 + 4 * x) / 5 - 10 * (y - 5) * (a + b + c) / x + 9 * (4 / x + (9 + x) / y)

        While Java has its own internal mechanism for evaluating expressions, the result of a Java expression and its corresponding arithmetic expression is the same. Therefore, you can safely apply the arithmetic rules for evaluating a Java expression. Operators enclosed within parentheses are evaluated first, and nested parentheses are evaluated from the innermost to the outermost. When multiple operators are present in an expression, operator precedence rules determine the order of evaluation.



        In an expression, the multiplication, division, and remainder operators take precedence over addition and subtraction operators. When an expression has multiple multiplication, division, or remainder operators, they are evaluated from left to right.

        After the multiplication, division, and remainder operations are resolved, the addition and subtraction operators are applied. If the expression contains multiple addition or subtraction operators, they are also evaluated from left to right.

        In summary, the order of evaluation for these operators is:

1. Multiplication, division, and remainder (from left to right)
2. Addition and subtraction (from left to right)


 

        Let's take a look at how the expression "3 + 4 * 4 + 5 * (4 + 3) - 1" is evaluated:

  1.  Inside the parentheses, the expression (4 + 3) is evaluated to 7.
  2.  Multiplication: 4 * 4 equals 16.
  3.  Multiplication: 5 * 7 equals 35.
  4.  Addition: 3 + 16 equals 19.
  5.  Addition: 19 + 35 equals 54.
  6.  Subtraction: 54 - 1 equals 53.


        The expression is evaluated by following the order of operations:

1. Parentheses are evaluated first.

2. Multiplication operations are performed next, from left to right.

3. Addition operations are performed last, from left to right.


        So, in summary, the expression is evaluated by performing the operations in the following order:

1. Parentheses

2. Multiplication

3. Multiplication

4. Addition

5. Addition

6. Subtraction 

        Below gives a program that converts a Fahrenheit degree to Celsius using the formula:

        celsius = (5 /9 )(fahrenheit - 32)

    1. import java.util.Scanner;

    2. public class FahrenheitToCelsius {
    3.     public static void main(String[] args) {
    4.         Scanner input = new Scanner(System.in);

    5.         System.out.print("Enter a temperature in Fahrenheit: ");
    6.         double fahrenheit = input.nextDouble();

    7.         // Convert Fahrenheit to Celsius
    8.         double celsius = (5.0 / 9) * (fahrenheit - 32);

    9.         System.out.println("Temperature in Celsius: " + celsius);
    10.     }
    11. }

Here's an explanation of how it works:

  1.  The code starts by importing the `Scanner` class from the `java.util` package, which allows us to read input from the user.
  2.  The `FahrenheitToCelsius` class is defined, which is the main class of the program.
  3.  The `main` method is the entry point of the program and where the execution begins.
  4.  Inside the `main` method, a `Scanner` object named `input` is created to read input from the user.
  5.  The program prompts the user to enter a temperature in Fahrenheit by printing the message "Enter a temperature in Fahrenheit: ".
  6.  The user's input is read using the `nextDouble()` method of the `Scanner` object and stored in the `fahrenheit` variable of type `double`.
  7.  The program performs the conversion from Fahrenheit to Celsius using the formula `(5.0 / 9) * (fahrenheit - 32)`. The result is stored in the `celsius` variable of type `double`.
  8.  Finally, the program outputs the converted temperature in Celsius by printing the message "Temperature in Celsius: " followed by the value of the `celsius` variable.
  9.  The program execution ends, and the output is displayed to the user.
  10.  Overall, this program allows the user to enter a temperature in Fahrenheit and calculates the equivalent temperature in Celsius using the conversion formula.



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