Understand and use arithmetic, logical and Boolean operators

Arithmetic Operators 🧮

Arithmetic operators perform mathematical calculations on numbers. They are the building blocks of any numerical computation. Think of them as the tools you use to add, subtract, multiply, and divide your favourite snacks.

Addition (+) & Subtraction (−)

  • Example: int total = 5 + 3;total = 8 ($5 + 3 = 8$)
  • Example: int diff = 10 - 4;diff = 6 ($10 - 4 = 6$)

Multiplication (×) & Division (÷)

  • Example: int prod = 6 * 7;prod = 42 ($6 \times 7 = 42$)
  • Example: int quot = 20 / 4;quot = 5 ($20 \div 4 = 5$)

Modulus (%)

  • Returns the remainder of a division.
  • Example: int rem = 17 % 5;rem = 2 ($17 \bmod 5 = 2$)

Exponentiation (**)

  • Raises a number to a power.
  • Example: int pow = 2 ** 3;pow = 8 ($2^3 = 8$)

Increment (++) & Decrement (−−)

  • Increase or decrease a variable by 1.
  • Example: int i = 5; i++; // i becomes 6
  • Example: int j = 10; j--; // j becomes 9

Logical Operators 🔀

Logical operators combine Boolean values (true/false) and produce a new Boolean result. They are essential for decision-making statements like if and while.

AND (&&)

  • True only if both operands are true.
  • Example: (a > 5) && (b < 10) → true if a is greater than 5 **and** b is less than 10.

OR (||)

  • True if at least one operand is true.
  • Example: (x == 0) || (y == 0) → true if either x or y is zero.

NOT (!)

  • Reverses the Boolean value.
  • Example: !true → false, !false → true.

XOR (^)

  • True if exactly one operand is true.
  • Example: true ^ false → true, true ^ true → false.

Boolean Values & Operators

Boolean values are the truth values true and false. In many languages, true is represented by 1 and false by 0. Logical operators always return a Boolean.

Example: int flag = (score >= 50) ? 1 : 0; converts a Boolean test into a numeric flag.

Operator Precedence

Operator precedence determines the order in which parts of an expression are evaluated. Use parentheses to make the intended order clear.

Operator Description Example
( ) Parentheses – highest priority (2 + 3) * 4 = 20
! (NOT) Logical NOT !true = false
*, /, % Multiplication, Division, Modulus 6 * 3 / 2 = 9
+, - Addition, Subtraction 5 + 2 - 1 = 6
<, >, <=, >= Relational 7 < 10 → true
==, != Equality / Inequality 5 == 5 → true
&& Logical AND true && false → false
|| Logical OR true || false → true
=, +=, -=, etc. Assignment x = 5; x += 3; // x becomes 8

📝 Exam Tips

  • ?? Use parentheses to make the order of operations clear.
  • ?? Check operator precedence – remember that * and / come before + and -.
  • ?? Test logical expressions with both true and false values.
  • ?? Practice if statements that combine multiple conditions.
  • Don’t forget that ! has higher precedence than && and ||.

Practice Questions

  1. What is the result of (3 + 4) * 2?
  2. What Boolean value does !(false || true) evaluate to?
  3. Write an expression that checks if a number n is between 10 and 20 inclusive.

Key Takeaways

  • Arithmetic operators perform calculations on numbers.
  • Logical operators combine Boolean values to form new conditions.
  • Operator precedence determines the order of evaluation.
  • Use parentheses to avoid confusion.
  • Practice writing expressions and testing them.

Revision

Log in to practice.

0 views 0 suggestions