ProgramGuru

Operators and Boolean conditions

Operators and Boolean conditions

A school rule can require both a sufficient score and attendance. Comparisons produce Boolean values, and logical operators combine them. Arithmetic operators calculate quantities, so it helps to trace those stages separately rather than treating every symbol as a decision.

What you’ll learn

  • Trace arithmetic and short-circuit Boolean conditions.
  • Distinguish structural equality from reference identity.

Operators and Boolean conditions in the example

  1. Check score

    A school rule can require both a sufficient score and attendance.

    Check score
  2. Check attendance

    <code>>=</code> includes its boundary.

    Check attendance
  3. Combine with AND

    Both eligibility comparisons are true.

    Combine with AND
Both eligibility comparisons are true. Seventeen divided by five has integer quotient three and remainder two, because 17 equals 3 × 5 + 2. Assignment, equality and identity each ask a different question.

Understand operators and boolean conditions

>= includes its boundary. && requires both conditions; || requires at least one. These logical operators short-circuit when the first operand determines the result. Kotlin uses == for structural equality and === for reference identity; the latter is not a general replacement for value comparison.

Follow operators and boolean conditions

Save the complete source as Main.kt and use the Kotlin/JVM environment. No separate input file is needed.

Kotlin/JVM · complete program

fun main() {
  val score = 75
  val attended = 9
  println(score >= 75 && attended >= 8)
  println(17 / 5)
  println(17 % 5)
}

Expected output

true
3
2

Follow the execution

Both eligibility comparisons are true. Seventeen divided by five has integer quotient three and remainder two, because 17 equals 3 × 5 + 2. Assignment, equality and identity each ask a different question.

Try this in the Kotlin compiler by pasting the complete program.

Practice: Operators and Boolean conditions

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

Kotlin/JVM · complete program

fun main() {
  val score = 75
  val attended = 9
  println(score >= 75 && attended >= 8)
  println(17 / 5)
  println(17 % 5)
}

Worked answer

It becomes false because the rule requires both conditions. A sufficient score does not compensate for a failed attendance requirement in this written policy.

Operators and Boolean conditions: the important boundary

Use parentheses when grouping is unclear. Integer division by zero fails rather than returning a useful mark. Do not use reference identity to compare the numerical or textual meaning of ordinary values.

Check your understanding

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

Answer and explanation

It becomes false because the rule requires both conditions.

It becomes false because the rule requires both conditions. A sufficient score does not compensate for a failed attendance requirement in this written policy.

Operators and Boolean conditions: what to keep in mind

It becomes false because the rule requires both conditions. A sufficient score does not compensate for a failed attendance requirement in this written policy. Use parentheses when grouping is unclear.

Further reading: official Kotlin documentation for this topic.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Strings, characters and templates →