Used to perform mathematical computations on numerical values. Allow to add, subtract, multiply, divide, and perform other mathematical operations. Addition (+) Used to add two numbers together. Example int result = 5 + 3; The value of result will be 8. Subtraction (-) Used to subtract one number from another. Example int result = 7 - 2; The value of result will be 5. Multiplication (*) Used to multiply two numbers. Example int result = 4 * 6; The value of result will be 24. Arithmetic Operators Operators 92
Division (/) Used to divide one number by another. Example int result = 10 / 2; The value of result will be 5. Modulus (%) Used to find the remainder after division. Example int result = 13 % 5; The value of result will be 3, as 13 divided by 5 leaves a remainder of 3. Increment (++) Used to increase the value of a variable by 1. Example int num = 5; num++; The value of num will be 6 after the increment operation. Decrement (--) Used to decrease the value of a variable by 1. Example int num = 8; num--; The value of num will be 7 after the decrement operation. Arithmetic Operators Operators 93
Arithmetic Operator Precedence Operator Description ( ) Parentheses (grouping) ++, -- Increment, decrement *, /, % Multiplication, division, modulus +, - Addition, subtraction Arithmetic Operators Operators Highest Lo w est 94
#include <iostream> using namespace std; int main() { int x = 5; int y = 3; int result; // Addition result = x + y; cout << "Addition: " << result << endl; // Subtraction result = x - y; cout << "Subtraction: " << result << endl; // Multiplication result = x * y; cout << "Multiplication: " << result << endl; // Division result = x / y; cout << "Division: " << result << endl; // Modulus result = x % y; cout << "Modulus: " << result << endl; // Increment x++; cout << "Increment: " << x << endl; // Decrement y--; cout << "Decrement: " << y << endl; return 0; } Arithmetic Operators Output: Addition: 8 Output: Subtraction: 2 Output: Multiplication: 15 Output: Division: 1 Output: Decrement: 2 Output: Modulus: 2 Output: Increment: 6 Operators 95
Used to compare values and determine the relationship between them. Commonly used in conditional statements and loops to make decisions based on the comparison results. Equality (==) Used to check if two values are equal and returns true if they are, and false otherwise. Example int x = 5; int y = 3; bool result = (x == y); The value of result will be false because x is not equal to y. Inequality (!=) Used to check if two values are not equal and returns true if they are different, and false if they are equal. Example: int x = 5; int y = 3; bool result = (x != y); The value of result will be true because x is not equal to y. Relational Operators Operators 96
Greater than (>) Use to check if the left-hand side value is greater than the right-hand side value and returns true if it is, and false otherwise. Example: int x = 5; int y = 3; bool result = (x > y); The value of result will be true because x is greater than y. Less than (<) Use to check if the left-hand side value is less than the right-hand side value and returns true if it is, and false otherwise. Example: int x = 5; int y = 3; bool result = (x < y); The value of result will be false because x is not less than y. Relational Operators Operators 97
Greater than or equal to (>=) Use to check if the left-hand side value is greater than or equal to the right-hand side value and returns true if it is, and false otherwise. Example: int x = 5; int y = 3; bool result = (x >= y); The value of result will be true because x is greater than y. Less than or equal to (<=) Used to check if the left-hand side value is less than or equal to the right-hand side value and returns true if it is, and false otherwise. Example: int x = 5; int y = 3; bool result = (x <= y); The value of result will be false because x is not less than or equal to y. Relational Operators Operators 98
#include <iostream> using namespace std; int main() { int x = 5; int y = 3; bool result; // Equality result = (x == y); cout << "Equality: " << result << endl; // Inequality result = (x != y); cout << "Inequality: " << result << endl; // Greater than result = (x > y); cout << "Greater than: " << result << endl; // Less than result = (x < y); cout << "Less than: " << result << endl; // Greater than or equal to result = (x >= y); cout << "Greater than or equal to: "<< result << endl; // Less than or equal to result = (x <= y); cout << "Less than or equal to: " << result << endl; return 0; } Output: Equality: 0 (false) Output: Inequality: 1 (true) Output: Greater than: 1 (true) Output: Less than: 0 (false) Output: Greater than or equal to: 1 (true) Relational Operators Output: Less than or equal to: 0 (false) Operators 99
Used to combine and manipulate boolean values. Used to perform logical operations on boolean expressions and make decisions based on their results. Logical AND (&&) Returns true if both the left-hand side and the right-hand side of the operator are true, and false otherwise. Example bool a = true; bool b = false; bool result = a && b; The value of result will be false because b is false. Logical OR (||) Returns true if either the left-hand side or the right-hand side of the operator is true, and false otherwise. Example bool a = true; bool b = false; bool result = a || b; The value of result will be true because a is true. Logical Operators Operators 100
Logical NOT (!) The logical NOT operator negates the boolean value. It returns true if the operand is false, and false if the operand is true. Example bool a = true; bool result = !a; The value of result will be false because a is true. Logical Operators Operators && || ! Logical AND Logical OR Logical NOT Returns true if both operands are true. Returns false otherwise. Returns true if at least one of the operands is true. Returns false if both operands are false. Reverses the Boolean value of the operand. Returns true if the operand is false, and vice versa. returns true if x and y are both true returns true if either a or b (or both) is true. returns true if a is false, and false if a is true. x && y a || b !a 101 Operator Name Description Example
#include <iostream> using namespace std; int main() { bool a = true; bool b = false; bool result; // Logical AND result = a && b; cout << "Logical AND: " << result << endl; // Logical OR result = a || b; cout << "Logical OR: " << result << endl; // Logical NOT result = !a; cout << "Logical NOT: " << result << endl; return 0; } Output: Logical AND: 0 (false) Output: Logical OR: 1 (true) Output: Logical NOT: 0 (false) Logical Operators Operators 102
Used to increase or decrease the value of a variable by a specific amount. Useful when working with loops or when we need to modify a variable's value by a fixed quantity. Increment (++) The increment operator increases the value of a variable by 1. Example int x = 5; x++; // Increment x by 1 The value of x will become 6 after the increment operation. Decrement (--) The decrement operator decreases the value of a variable by 1. Example int y = 8; y--; // Decrement y by 1 The value of y will become 7 after the decrement operation. Logical Operators Operators 103
Increment and decrement operators can be used in different ways within a program. int a = 5; int b = 3; a++; // Increment a by 1 b--; // Decrement b by 1 int c = ++a; // Increment a by 1 and assign the new value to c int d = b--; // Assign the current value of b to d and then decrement b by 1 cout << "a: " << a << endl; // Output: a: 7 cout << "b: " << b << endl; // Output: b: 1 cout << "c: " << c << endl; // Output: c: 7 cout << "d: " << d << endl; // Output: d: 2 Logical Operators increments or decrements the variable before using its value Prefix (++a or --a) uses the current value and then increments or decrements the variable Postfix (a++ or b--) **The position of the increment or decrement operator can affect the behavior of the expression. a is initially 5, and after applying the increment operator (a++), a becomes 6. b is initially 3, and after applying the decrement operator (b--), b becomes 2. Operators 104
#include <iostream> using namespace std; int main() { int x = 5; int y = 3; // Increment x++; // Increment x by 1 cout << "Increment: " << x << endl; // Decrement y--; // Decrement y by 1 cout << "Decrement: " << y << endl; // Prefix increment int a = 5; int b = ++a; // Increment a by 1 and assign the new value to b cout << "Prefix increment - a: " << a << ", b: " << b << endl; // Postfix increment int c = 5; int d = c++; // Assign the current value of c to d and then increment c by 1 cout << "Postfix increment - c: " << c << ", d: " << d << endl; return 0; } Output: Increment: 6 Output: Decrement: 2 Output: Prefix increment - a: 6, b: 6 Logical Operators Operators Output: Postfix increment - c: 6, d: 5 105
evaluates to true && true, which is true. True is represented by the value 1. Therefore, the value of result will be 1. Evaluate the following expression to determine the correct answer. Given: int x = 5; int y = 3; int z = 7; int result = (x > y) && (z != y); What is the value of result? A) 0 B) 1 C) 1 && 1 D) true && true Correct Answer: B) 1 The expression (x > y) && (z != y) 1 LET'S CHECK! LET'S CHECK! Explanation 106
Evaluate the following expression to determine the correct answer. Given: int a = 8; int b = 3; int c = 2; int result = (a % b) * c++; What is the value of result? A) 2 B) 4 C) 6 D) 8 Correct Answer: B) 4 The expression (a % b) * c++ evaluates to 2 * 2, since a % b is 2. The ++ operator increments the value of the variable by 1. However, since it is post-increment (c++), the value of c in the current expression remains 2, and it is only incremented afterward. Therefore, the value of result will be 2 * 2 = 4. 2 LET'S CHECK! LET'S CHECK! Explanation 107
Evaluate the following expression to determine the correct answer. Given: int x = 5; int y = 2; int z = 7; int result = x++ + (y * --z); What is the value of result? A) 10 B) 12 C) 16 D) 17 Correct Answer: D) 17 x++ uses the post-increment operator, which means the current value of x (5) is used in the expression, and then x is incremented by 1. So, x++ is 5. --z uses the current value of z (7) and then decrements it by 1, resulting in z becoming 6. Therefore, the value of result will be 5 + (2 * 6) = 5 + 12 = 17. 3 LET'S CHECK! LET'S CHECK! Explanation 108
Solve the following expression. int a = 5; int b = 3; int result = a + b * 2; A) 8 B) 11 C) 16 D) 21 int x = 12; int y = 4; int z = 2; int result = (x / y) % z; A) 0 B) 1 C) 2 D) 3 int a = 5; int b = 3; int result = a > b || b < 2; A) 0 B) 1 int a = 6; int b = 3; int result = a >= b && !(a == b); A) 0 B) 1 QUIZ! Time 4123 Answer: 1. B 2. B 3. B 4.B 109
Solve the following expression. int x = 8; int y = 3; int result = x % y + 2 * y; A) 11 B) 8 C) 12 D) 20 int a = 3; int b = 5; int result = (a != b) && (a < b); A) 0 B) 1 int x = 7; int y = 3; int z = 2; int result = x / (y + z); A) 1 B) 2 C) 3 D) 4 int a = 4; int b = 5; int result = (a == b) || (a >= b); A) 0 B) 1 QUIZ! Time 8567 Answer: 5. B 6. B 7. A 8. A 110
Solve the following expression. int x = 10; int y = 4; int result = (x % y) - y; A) 0 B) -2 C) 4 D) 6 int a = 3; int b = 2; int result = a / (b + 1) + a % b; A) 2 B) 3 C) 4 D) 5 int x = 12; int y = 3; int z = 2; int result = x / (y * z) % z; A) 0 B) 1 C) 2 D) 3 int a = 4; int b = 5; int result = (a != b) && (a > b); A) 0 B) 1 QUIZ! Time 1291011 Answer: 9. B 10. A 11. A 12. A 111
Given a = 5, b = 3, c = 2 Given x = 10, y = 7, z = 15 Given p = 4, q = 9, r = 6 Identify whether the value is true or false for the following expressions. (a > b) && (c != b) (x <= y) || (z > y) (p < q) && (q < r) && (r > p) Given m = 12, n = 6: !(m == n) || (m % n == 0) QUIZ! Time 4 1 2 3 Answer: 1. True 2. True 3. False 4. True 112
Identify whether the value is true or false for the following expressions. Given u = 8, v = 10, w = 12 (u + v > w) && (v - u < w) Given d = 3.5, e = 2.8, f = 4.2 (d >= e) && (f <= e) || (d + e > f) Given k = 5, l = 5 (k == l) && (k != l) Given g = 7, h = 9, i = 11 (g < h) || (h < i) && (i > g) QUIZ! Time 8 5 6 7 Answer: 5. True 6. True 7. False 8. True 113
Identify whether the value is true or false for the following expressions. Given s = 15, t = 20 (s != t) || (s % t == 0) Given j = 6.2, k = 5.8, l = 7.1 (j >= k) && (l <= k) || (j + k > l) Given x = 5, y = 7, z = 3 (x > y) && !(y == z) Given p = 4, q = 2, r = 6: (p >= q) || (r < q) && (p != r) QUIZ! Time 12 9 10 11 Answer: 9. Ture 10. True 11. False 12. True 114
Find the value for the following expression. Show all the steps clearly. Given a = 5, b = 2 (a % b) * (a + b) Given x = 7, y = 3 ++x + --y Given m = 10, n = 4 m++ - n-- Given p = 6, q = 2 p++ % q QUIZ! Time 4 1 2 3 Answer: 1. 7 2. 10 3. 6 4. 0 115
(a++) * (b + c--) Find the value for the following expression. Show all the steps clearly. Given x = 5, y = 3, z = 2 (x++ * y) % z Given a = 7, b = 3, c = 2 ++a * b-- + c Given x = 10, y = 3, z = 4 x % y + (z--) Given a = 5, b = 2, c = 8 QUIZ! Time 8 5 6 7 Answer: 5. 1 6. 26 7. 5 8. 50 116
Find the value for the following expression. Show all the steps clearly. Given p = 12, q = 5, r = 3 (--p) + (q % r) Given m = 7, n = 2, o = 4 m-- * (n++ + o--) Given x = 8, y = 3, z = 6 (++x) - (y++ % z) Given a = 4, b = 7, c = 2 (++a % b) * (c--) QUIZ! Time 12 9 10 11 Answer: 9. 13 10. 42 11. 6 12. 10 117
Write expression to solve problem by using appropriate operators. Calculate the perimeter of a square. Given its side length. Calculate the total cost of a meal at a restaurant, including the cost of food, tax, and service charge. The tax rate is 10% and the service charge percentage is 5% from the food cost. Calculate the total cost of purchasing multiple items from a store. Each item has the same price. Write a formula to calculate the total cost. QUIZ! Time 3 1 2 118
Chapter 3 FUNDAMENTALS OF PROGRAMMING LANGUAGE 3.3.1 Explain the control structures in problem solving: a. Sequence b. Selection c. Repetition 3.3.2 Write pseudo code and flowchart using control structures in solving given problems. 3.3.3 Design the algorithm for a given case study using problem solving tools. 3.3 DESCRIBE CONTROL STRUCTURES IN PROBLEM SOLVING 119
Control Structures determines the order in which statements or instructions are executed in a program. allows to control the flow of execution, make decisions, and repeat certain actions based on conditions. used to create more complex and dynamic programs by directing the flow of execution based on specific conditions or criteria. Sequential, selection, and repetition are three fundamental control structures used in programming. Sequential Selection Repetition 120
Control Structures Sequential Control Structure default flow of execution in a program. execute statements in a sequential order, one after another. each statement is executed exactly once unless there are loops or conditional statements that alter the flow. End Start Statement 1 Statement 2 Pseudo code Start StatemS ent 1 StatemS ent 2 End Flowchart 121
Control Structures The statements are executed sequentially. First, the variables x and y are initialized. Then, their sum is calculated and displayed. Example Start x = 10; y = 20; sum = x + y; Display sum; End Start x =S 10 y =S 20 End sum =S x + y DisplaSy sum Pseudo code Flowchart 122 Sequential Control Structure
Start StatemSent 1 End Control Structures Selection Control Structure also known as decision-making structures. used to make decisions and execute different blocks of code based on certain conditions. allow programs to take different paths of execution based on whether a condition evaluates is true or false. primary constructs used for selection control are if...endif, if...else, and switch statements. if (condition) { // code to be executed if the condition is true endif } if...endif Syntax Flowchart Condition True False 123
Control Structures In this example, if the age is 18 or above (TRUE), the program will display "You are eligible to vote." If the age is below 18 (FALSE), the program will end. Example Start Insert age if (age >= 18) Display "You are eligible to vote." endif End Pseudo code Flowchart Start End age >= 18 True False InserSt age S Display "You are eligible to vote." 124 Selection Control Structure
Start S End Condition Control Structures if (condition) { // code to be executed if the condition is true } else { // code to be executed if the condition is false } endif Syntax if...else Statement 1 Flowchart True StatemSent 2 False In this structure, if the condition is true, the code inside the first block is executed. If the condition is false, the code inside the else block is executed. 125 Selection Control Structure
Control Structures In this example, the program checks if the number is even or odd and prints the corresponding message. Example Start Insert num if (num % 2 == 0) Display "The number is even." else Display "The number is odd." endif End Pseudo code Flowchart Start End if num % 2 == 0 True False InserSt num S Display "The number is even." S Display "The number is odd." 126 Selection Control Structure
Start End Condition 1 S Control Structures if (condition1) { // block of code to be executed if condition1 is true } else if (condition2) { // block of code to be executed if the condition1 is false and condition2 is true } else { // block of code to be executed if the condition1 is false and condition2 is false } endif Syntax if...else if..else Flowchart False Statement 1 True used to specify a new condition if the first condition is false. Condition 2 False StatemSent 3 StatemSent 2 True 127 Selection Control Structure
Example Start Insert score if (score >= 90) Display "Grade: A" else if (score >= 80) Display "Grade: B" else if (score >= 70) Display "Grade: C" else if (score >= 60) Display "Grade: D" else Display "Grade: F" endif End Start End score >= 90 True S Display "SGrade: A" Control Structures Pseudo code Flowchart False Insert score score >= 80 True Display "SGrade: B" False score >= 70 True Display "SGrade: C" False score >= 60 True Display "SGrade: D" False Display "SGrade: F" In this example, the program checks the student's score and assigns a grade based on the given criteria. 128 Selection Control Structure
Start End Case 1 Case 2 S Control Structures switch (expression) { case value1: // code to be executed if the expression matches value1 break; case value2: // code to be executed if the expression matches value2 break; // more cases... default: // code to be executed if none of the cases match the expression } Syntax switch case Flowchart True False Used to select one of many possible blocks of code to execute based on the value of an expression. Statement 1 StateSment 2 StateSment 3 False 129 Selection Control Structure
Example Start Insert number switch (number) { case 1: Display "Durian" break; case 2: Display "Rambutan" break; case 3: Display "Tembikai" break; case 4: Display "Nanas" break; default: Display "Invalid number" } End Start End Case 1 True S DisplayS"Durian" Control Structures Pseudo code Flowchart False Insert number Case 2 True Display "SRambutan" False Case 3 True Display "STembikai" False Case 4 True DisplayS "Nanas" False Display "InSvalid number" The switch expression is evaluated once. The value of the expression is compared with the values of each case. If there is a match, the associated block of code is executed. 130 Selection Control Structure
endif if (condition 1) if (condition 2) if (condition 3) // Statement will be executed if condition 1, condition 2 and condition 3 are true endif endif Control Structures Type 1: Syntax nested if Nested if control structure refers to the use of if statements within other if statements. It allows for more complex decision-making and branching logic by creating multiple levels of conditions and corresponding actions. endif if (condition 1) if (condition 2) if (condition 3) //Statement will be executed if condition 1,condition 2 and condition 3 are true else //Statement will be executed if condition 1, condition 2 are true but condition 3 is false endif else //Statement will be executed if condition 1 is true but condition 2 and condition 3 are false endif else //Statement will be executed if condition 1 is false Type 2: Syntax 131 Selection Control Structure
endif if (condition 1) //Statement will be executed if condition 1 is true else if (condition 2) //Statement will be executed if condition 2 is true but condition 1 is false else if (condition 3) //Statement will be executed if condition 3 is true but condition 1 and condition 2 are false else //Statement will be executed if condition 1, condition 2 and condition 3 are false endif endif Control Structures Type 3: Syntax nested if Nested if control structure refers to the use of if statements within other if statements. It allows for more complex decision-making and branching logic by creating multiple levels of conditions and corresponding actions. 132 Selection Control Structure
Example Start Insert purchaseAmount Insert customerType discountPercentage = 0 if customerType == "VIP" then discountPercentage = 15 else if purchaseAmount > 250 then discountPercentage = 5 endif endif discountAmount= (purchaseAmount*discountPercentage)/100 totalAmount=purchaseAmount-discountAmount Display discountAmount Display totalAmount End Start End customerType == "VIP" True S Control Structures Pseudo code Flowchart Insert purchaseAmount, customerType S Display discountAmount, totalAmount In this example, the nested if statements check the customer type and the purchase amount to determine the appropriate discount percentage. discountPerScentage = 0 discountPerScentage=15 purchaseAmount > 250 True discountPeSrcentage=5 discountAmount=(purchaseAmoSunt*discountPercentage)/100 False False 133 Selection Control Structure
Write a pseudo code and flowchart that asks a user to enter their age. If the age is 17 years old or above, display the statement "You are eligible to apply for a driving license." QUIZ! Time Pseudo code Flowchart 134
Write a pseudo code and flowchart that receives the current temperature as input. If the temperature is 70 degrees or higher, output a message encouraging the user to go for a bike ride. Otherwise, if the temperature is 50 degrees or higher, suggest going for a brisk walk, and if it's colder than 50 degrees, advise the user to do indoor exercises. QUIZ! Time Pseudo code Flowchart 135
Write a pseudo code and flowchart that asks the user for the current temperature. If the temperature is 65 degrees or higher, output a message telling the user it's a great time for gardening. Otherwise, if the temperature is 55 degrees or higher, suggest doing some light gardening, and if it's colder than 55 degrees, advise the user to postpone gardening to a warmer day. QUIZ! Time Pseudo code Flowchart 136
If the balance is greater than or equal to 1000, print the statement "You have enough balance for a vacation." If the balance is greater than or equal to 500, suggest saving more money for a vacation. If the balance is less than 500, advise the user to start saving for a vacation. Write a pseudo code and design a flowchart based on the following problem statement. Asks a user to enter their account balance. QUIZ! Time Pseudo code Flowchart 137
Write a pseudo code and design a flowchart based on the following problem statement. Ilman Mart App asks customer to enter the total purchase amount. If the purchase amount is greater than or equal to 200, print the statement "You are eligible for a 10% discount." Otherwise, if the purchase amount is greater than or equal to 100, suggest adding more items to qualify for the discount, and if the purchase amount is less than 100, inform the user that they can use discounts on future purchases. QUIZ! Time Pseudo code Flowchart 138
Children (age < 13) pay RM12 for morning shows and RM15 for evening shows. Adults (age >= 13 and age < 65) pay RM18 for morning shows and RM20 for evening shows. Senior citizens (age >= 65) pay RM15 for both morning and evening shows. Write a pseudo code and design a flowchart based on the following problem statement. Filmify Cinema charges different ticket prices based on age and the time of the movie. The criteria are as follows: QUIZ! Time Pseudo code Flowchart 139
If the weight is less than or equal to 70 kg, print the statement "You have achieved your fitness goal." If the weight is less than or equal to 80 kg, suggest continuing the fitness routine. If the weight is greater than 80 kg, advise the user to intensify their fitness efforts. Write a pseudo code and design a flowchart based on the following problem statement. Healthy Fit Management System asks user for their weight. QUIZ! Time Pseudo code Flowchart 140
Children (age < 5) can enter for free. Children (age >= 5 and age < 12) pay RM20 for entry before 5pm and RM15 for entry after 5pm Adults (age >= 12 and age < 60) pay RM40 for entry before 5pm and RM35 for entry after 5pm Senior citizens (age >= 60) pay RM25 for entry before 5pm and RM20 for entry after 5pm Write a pseudo code and design a flowchart based on the following problem statement. Bernam theme park charges different entry fees based on age and the time of entry. The criteria are as follows: QUIZ! Time Pseudo code Flowchart 141