CodeBlanks Logo Icon CodeBlanks

Logical operators

Learn boolean logic with &&, || and !.

Logical operators

Logical operators combine bool values: && (and), || (or), ! (not).

&& gives true only when both sides are true. || gives true when at least one side is true. ! flips the value: !true = false.

Example: if (age >= 18 && hasTicket) — passes only if age is right and there is a ticket. These operators are the basic way to combine conditions.

Short-circuit evaluation

The && and || operators short-circuit: once the result is known, they skip the rest.

For example in false && expensiveCall() the left side is false, so the right side is not called; the result is false. This avoids unnecessary work.

Similarly in true || expensiveCall() the left side is true, so the right side is not called. This feature is used to safely guard side effects.

Practice Logical operations

Write print(true && false), print(true || false) and print(!true) inside void main().

Starter code

main.dart
// Logical operations

Sample solution

main.dart
void main() {
  print(true && false);
  print(true || false);
  print(!true);
}

Expected output

false
true
false

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What is the result of true && false?

  • true
  • false Correct answer
  • 1
  • Throws an error

Explanation: && requires both sides to be true.

2 And operator

Question
if (a > 0 ____ a < 10) { print('in range'); }
Answer
if (a > 0 && a < 10) { print('in range'); }

Explanation: && requires both conditions together.

3 Logical operators

Term Meaning
&& And
|| Or
! Not
true True

All lessons in Beginner