ProgramGuru

Operators and Boolean conditions

Operators and Boolean conditions

A certificate can require both a sufficient mark and sufficient attendance. Comparisons produce bool values, and logical operators combine them. Separating arithmetic from comparisons helps you see whether a mistake concerns a calculation or the policy applied to it.

What you’ll learn

  • Translate a two-part school rule into Boolean comparisons.
  • Trace division, remainder and an inclusive threshold.

Operators and Boolean conditions in action

  1. Mark: passes threshold

    Both comparisons are true.

    Mark: passes threshold
  2. Attendance: passes threshold

    Seventeen divided by five has quotient three and remainder two: 17 equals 3 × 5 + 2.

    Attendance: passes threshold
  3. Combined result: true

    Equality uses ==; writing = instead changes a binding where assignment is allowed.

    Combined result: true
Both comparisons are true. Seventeen divided by five has quotient three and remainder two: 17 equals 3 × 5 + 2. Equality uses ==; writing = instead changes a binding where assignment is allowed.

Operators and Boolean conditions explained

>= includes the boundary; && requires both conditions and || requires at least one. These logical operators short-circuit: they do not evaluate the second operand when the first determines the answer. The remainder operator % answers how much is left after integer division.

Work through the Rust program

Save the complete source as main.rs in the Rust environment. No standard input is needed.

Rust 2024 · complete program

fn main() {
  let score = 75;
  let attendance = 9;
  println!("{}", score >= 75 && attendance >= 8);
  println!("{} {}", 17 / 5, 17 % 5);
}

Expected output

true
3 2

Follow the execution

Both comparisons are true. Seventeen divided by five has quotient three and remainder two: 17 equals 3 × 5 + 2. Equality uses ==; writing = instead changes a binding where assignment is allowed.

Try the complete program in the Rust compiler. For tests, Cargo commands and local files, use the specified local environment.

Try a small variation

Your task: If attendance becomes seven, is the student eligible?

Rust 2024 · complete program

fn main() {
  let score = 75;
  let attendance = 9;
  println!("{}", score >= 75 && attendance >= 8);
  println!("{} {}", 17 / 5, 17 % 5);
}

Worked answer

No. The rule requires both comparisons to be true. A sufficient mark does not replace the separate attendance requirement.

Mistakes and limits to recognize

Parentheses make a complex policy easier to review. Rust does not treat an arbitrary integer as a Boolean condition. A technically valid expression can still encode the wrong policy, so test its boundaries.

Check your understanding

If attendance becomes seven, is the student eligible?
Choose one answer. You can try again.

Answer and explanation

No.

No. The rule requires both comparisons to be true. A sufficient mark does not replace the separate attendance requirement.

Take the idea into your next program

No. The rule requires both comparisons to be true. A sufficient mark does not replace the separate attendance requirement. Parentheses make a complex policy easier to review.

Further reading: the official Rust Book on operators and boolean conditions.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Formatted output and comments →