> ## Documentation Index
> Fetch the complete documentation index at: https://v1-learn.neoartd.my.id/llms.txt
> Use this file to discover all available pages before exploring further.

# Conditionals and Logic

# if

![if](https://i.ibb.co/VLfqB04/Screenshot-2024-04-03-154618.png)

# if ... else ...

![if else](https://i.ibb.co/BrJXs0T/Screenshot-2024-04-03-154629.png)

# Simple Example

<Tabs>
  <Tab title="Question">
    Write a program to determine whether the body temperature entered by the user can be categorized as a fever

    * Input: **body temperature in celcius (C)**
    * Output: **“Fever”, if body temperature is more than 37o C. If not, no outputs.**
  </Tab>

  <Tab title="Answer">
    ```java Program.java theme={null}
      import java.util.*;
      public class Program {
          public static void main(String[] args) {
              Scanner in = new Scanner(System.in);
              double temperature = in.nextDouble();
              if (temperature > 37) { // condition
                  System.out.println("Fever"); // Statements executed when the condition is true (fulfilled)
              }
          }
      }
    ```
  </Tab>
</Tabs>

# Relational Operators

| Operator | Description              | Example |
| :------: | ------------------------ | :-----: |
|    ==    | Equal to                 |  x == y |
|    !=    | Not equal to             |  x != y |
|     >    | Greater than             |  x > y  |
|    \<    | Less than                |  x \< y |
|    >=    | Greater than or equal to |  x >= y |
|    \<=   | Less than or equal to    | x \<= y |

# Logical Operators

| Operator | Description |  Example |
| :------: | ----------- | :------: |
|    &&    | Logical AND |  x && y  |
|   \|\|   | Logical OR  | x \|\| y |
|     !    | Logical NOT |    !x    |

* Example
  ```java theme={null}
  x < 0 && y >= 10
  number != 2 || number > 0
  !(temperature > 37)
  !(price < 10 || price > 100)
  (width > 0 && width < 10) || (length > 20)
  ```

# Chaining

```java theme={null}
if (condition1) {
// executed when condition1 is true
} else if (condition2) {
// executed when condition1 is false and condition2 is true
} else if (condition3) {
// executed when condition1 & 2 are false, condition3 is true
} else {
// executed when all conditions are false
}
```

# Exercise

## Exercise 1

<Tabs>
  <Tab title="Question">
    Write a program to calculate the total user must pay for an imported item.

    * If the price of the item exceeds USD 100, then an import tax of 20% is imposed
    * Input: **price of items before tax in USD**
    * Output: **total paid (item price plus tax)**
  </Tab>

  <Tab title="Answer">
    ```java Program.java theme={null}
      import java.util.*;
      public class Program {
          public static void main(String[] args) {
              Scanner in = new Scanner(System.in);
              double price = in.nextDouble();
              double tax = 0;
              if (price > 100) {
                  tax = price * 0.2;
              }
              double total = price + tax;
              System.out.printf("The total paid is %.2f\n", total);
          }
      }
    ```
  </Tab>
</Tabs>

## Exercise 2

<Tabs>
  <Tab title="Question">
    Write a program to determine whether the value entered by the user is odd or even.

    * Input: **integer**
    * Output: **“Odd” or “Even”**
  </Tab>

  <Tab title="Answer">
    ```java Program.java theme={null}
      import java.util.*;
      public class Program {
          public static void main(String[] args) {
              Scanner in = new Scanner(System.in);
              int number = in.nextInt();
              if (number % 2 == 0) {
                  System.out.println("Even");
              } else {
                  System.out.println("Odd");
              }
          }
      }
    ```
  </Tab>
</Tabs>

## Exercise 3

<Tabs>
  <Tab title="Question">
    Write a program to determine whether the size of the baby at birth is normal or not

    * Normal baby weight at birth ranges from 2.5 to 4.5 kg and lengths between 45 and 60 cm
    * Input: **baby's weight in kg and length in cm**
    * Output: **whether the baby's size is normal or not**
  </Tab>

  <Tab title="Answer">
    ```java Program.java theme={null}
      import java.util.*;
      public class Program {
          public static void main(String[] args) {
              Scanner in = new Scanner(System.in);

              System.out.print("Enter the baby's weight: ");
              double weight = in.nextDouble();

              System.out.print("Enter the baby's length: ");
              double length = in.nextDouble();

              // Flag variables
              boolean normalWeight = (weight >= 2.5 && weight <= 4.5);
              boolean normalLength = (length >= 45 && length <= 60);

              if (normalWeight && normalLength) {
                  System.out.println("Normal weight and length");
              } else if (normalWeight) {
                  System.out.println("Normal weight, Abnormal length");
              } else if (normalLength) {
                  System.out.println ("Abnormal weight, normal length");
              } else {
                  System.out.println ("Abnormal weight and length");
              }
          }
      }
    ```
  </Tab>
</Tabs>

## Exercise 4

<Tabs>
  <Tab title="Question">
    Write a program to convert grade in numbers to letters.

    * Conversion table:
      |    Number   | Letter |
      | :---------: | :----: |
      |    \< 41    |    E   |
      | 41 to \< 56 |    D   |
      | 56 to \< 61 |    C   |
      | 61 to \< 66 |   BC   |
      | 66 to \< 76 |    B   |
      | 76 to \< 81 |   AB   |
      |    >= 81    |    A   |

    * Input: **grade in number**

    * Output: **letter grade**
  </Tab>

  <Tab title="Answer">
    ```java Program.java theme={null}
      import java.util.*;
      public class Program {
          public static void main(String[] args) {
              Scanner in = new Scanner(System.in);

              System.out.print("Berapa nilaimu: ");
              double nilai = in.nextDouble();
              String nilaiHuruf;

              if (nilai < 41) {
                  nilaiHuruf = "E";
              } else if (nilai < 56) {
                  nilaiHuruf = "D";
              } else if (nilai < 61) {
                  nilaiHuruf = "C";
              } else if (nilai < 66) {
                  nilaiHuruf = "BC";
              } else if (nilai < 76) {
                  nilaiHuruf = "B";
              } else if (nilai < 81) {
                  nilaiHuruf = "AB";
              } else {
                  nilaiHuruf = "A";
              }

              System.out.println("Nilai Huruf = " + nilaiHuruf);
          }
      }
    ```
  </Tab>
</Tabs>
