ProgramGuru

Arithmetic, comparisons and Boolean logic

Arithmetic, comparisons and Boolean logic

Attendance and quiz scores often combine into decisions. Arithmetic computes values, comparisons produce Bool values, and logical operators combine conditions. Keeping those jobs separate makes a rule such as “score at least 75 and enough attendance” easier to trace.

What you’ll learn

  • Trace arithmetic, comparisons and short-circuit Boolean logic.
  • Check an eligibility rule at its score and attendance boundaries.

Arithmetic, comparisons and Boolean logic in the example

  1. Compare score

    Attendance and quiz scores often combine into decisions.

    Compare score
  2. Compare attendance

    <code>==</code> tests equality while <code>=</code> includes the boundary.

    Compare attendance
  3. Combine conditions

    Both comparisons are true, so eligible is true.

    Combine conditions
Both comparisons are true, so eligible is true. The integer quotient is 3 and remainder is 2 because 17 equals 3 × 5 + 2. Assignment stores a result, whereas comparison produces one; confusing their symbols changes the program’s meaning.

How arithmetic, comparisons and boolean logic works

== tests equality while = includes the boundary. && means both conditions must hold; || means at least one; ! negates a Bool. These operators short-circuit: the second logical operand is evaluated only if needed. Parentheses make an intended grouping explicit.

Work through arithmetic, comparisons and boolean logic

Save this complete program as main.swift and run it with the Swift toolchain. No separate input file is needed.

Swift · complete program

let score = 75
let attended = 9
let eligible = score >= 75 && attended >= 8
print(eligible)
print(17 / 5, 17 % 5)

Expected output

true
3 2

Follow the execution

Both comparisons are true, so eligible is true. The integer quotient is 3 and remainder is 2 because 17 equals 3 × 5 + 2. Assignment stores a result, whereas comparison produces one; confusing their symbols changes the program’s meaning.

Try this program in the Swift compiler. Paste the complete source above.

Practice: Arithmetic, comparisons and Boolean logic

Your task: What happens to eligibility if attended becomes seven?

Swift · complete program

let score = 75
let attended = 9
let eligible = score >= 75 && attended >= 8
print(eligible)
print(17 / 5, 17 % 5)

Worked answer

It becomes false even though the score still meets the boundary. The rule requires both conditions, so the failed attendance condition determines the combined result.

Read both parts of the eligibility rule

Operator precedence can make unparenthesized expressions hard to read. Integer overflow and division by zero are not ordinary valid results; use checked policies and suitable types rather than assuming every arithmetic operation succeeds.

Check your decision

What happens to eligibility if attended becomes seven?
Choose one answer. You can try again.

Answer and explanation

It becomes false even though the score still meets the boundary.

It becomes false even though the score still meets the boundary. The rule requires both conditions, so the failed attendance condition determines the combined result.

Read both parts of the eligibility rule

It becomes false even though the score still meets the boundary. The rule requires both conditions, so the failed attendance condition determines the combined result. Operator precedence can make unparenthesized expressions hard to read.

Further reading: official Swift language guide.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Strings, characters and interpolation →