Pemodelan untuk Komputasi : Logical Operator & Selection dan Contoh Soal ( Bahasa Inggris )
Introduction of Logic Operator & Selection
The university bookstore has a Kilobyte Day sale every October 24, giving an 8 percent discount on all computer accessory purchases if the price is less than $128, and a 16 percent discount if the price is at least $128. Write a program that asks the cashier for the original price and then prints the discounted price.
Solution
- Check whether a condition is true or false à Is the price less than $128? (the answer is true or false).
- Select only an alternative to be done and ignore any other alternative(s)
Relational Operator & Expressions
In order to make a decision, we need to know a condition that makes an alternative is chosen. In many cases, the condition involves comparing values. To compare values, we need :
- Relational operator : an operator that is used to tests the relationship between two values.
- Relational expressions : expression using relational/logical operator, the result of this expression is true or false
Relational Operator
Example
- price < 128 (price is less than 128)
- price >= 128 (price is greater than 128)
- price = 128 (price equals to 128)
- price ≠ 128 (price does not equal to 128)
Relational Expression
- Relational expressions : expression using relational/logical operator, the result of this expression is true or false
- Logical operator (AND, OR, NOT)
- Example: the price <= 128 AND today = SUNDAY
Algorithms of Relational Expression
Use IF statement in our algorithm if it needs to choose between some different steps/alternatives. After the IF statement, we also need to write the conditions that are needed to be checked.
IF (conditions) then steps
The conditions are written using the relational expression. Example:
IF(price <=128) then discount = 0.08 * price
To choose an alternative, algorithm can have a compound conditions. Use the logical operator to make compound conditions. Example:
IF nilai >70 AND nilai <80 then nilaiHuruf = B
IF – ELSE
- IF-ELSE statement chooses between two alternatives.
- IF (conditions) then steps
- ELSE steps
- Example :
- IF (nilaiHuruf=E) then status = tidak lulus
- ELSE status = lulus
Example
Flowchart of Relational Expression
We use the diamond to symbolize the selection in flowchart. The diamond will have some branches. We can add the condition inside the diamond.
E.g. price<=128
Examples
Vowel or Consonant
Write the algorithms and draw the flowchart to check if a letter is a vowel or a consonant.
MAX
Write the algorithms and draw the flowchart to find maximum between two numbers
Exercise 1
Relational Operator
Some operators checks about the relationship between two values and these operators are called relational operators. Given two numerical values your job is just to find out the relationship between them that is (i) First one is greater than the second (ii) First one is less than the second.
Triangle
A long time ago, the Egyptians figured out that a triangle with sides of length 3, 4, and 5 had a right angle as its largest angle. Given the length of the sides of triangle, you must determine if the triangles have a similar property. The given lengths are always increasingly ordered.
Distance
Two wombats stand on a position in cartesian system of coordinates. Based on their position, we can determine if they are close friends or not. We can say that two wombats are close friends if their distance is less than or equals 200. Write the algorithms and the flowchart to determine if two wombats are close friends.
The Queen Problem
Given the positions (row and col number) of two queens on a chessboard. Your job is to write the algorithm and draw the flowchart to check if the two queens can attack each other.
Posting Komentar