if
if … else …
Simple Example
- Question
- Answer
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.
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
Chaining
Exercise
Exercise 1
- Question
- Answer
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)
Exercise 2
- Question
- Answer
Write a program to determine whether the value entered by the user is odd or even.
- Input: integer
- Output: “Odd” or “Even”
Exercise 3
- Question
- Answer
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
Exercise 4
- Question
- Answer
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