Menu
Coddy logo textTech

Logical OR

Part of the Fundamentals section of Coddy's Dart journey — lesson 24 of 94.

The logical OR operator (||) returns true when at least one expression is true, and false only when both are false.

void main() {
  bool hasCreditCard = false;
  bool hasDebitCard = true;
  
  bool canPayOnline = hasCreditCard || hasDebitCard;
  
  print('Can this person pay online? $canPayOnline');
}

Output: Can this person pay online? true

void main() {
  int age = 16;
  bool hasParentalConsent = true;
  
  bool canWatchMovie = (age >= 18) || (age >= 13 && hasParentalConsent);
  
  print('Can watch the movie? $canWatchMovie');
}
challenge icon

Challenge

Beginner

Create a game eligibility checker using the Logical OR (||) operator:

  1. Declare an integer variable age with value 15
  2. Declare a boolean variable hasParentalConsent with value true
  3. Declare a boolean variable canPlayGame that checks if either:
    • The person is 18 or older (age >= 18), OR
    • The person has parental consent (hasParentalConsent is true)
  4. Print the result with this exact format:
Age: 15
Has parental consent: true
Can play game: true

Then change hasParentalConsent to false and print the updated result with the same format.

Cheat sheet

The logical OR operator (||) returns true when at least one expression is true, and false only when both are false.

bool canPayOnline = hasCreditCard || hasDebitCard;

You can combine OR with other logical operators:

bool canWatchMovie = (age >= 18) || (age >= 13 && hasParentalConsent);

Try it yourself

void main() {
  // Declare your variables here
  int age = ?;
  bool hasParentalConsent = ?;
  
  // Check if the person can play the game
  bool canPlayGame = age >= ? || hasParentalConsent;
  
  // Print the initial results
  print("Age: $age");
  print("Has parental consent: $hasParentalConsent");
  print("Can play game: $canPlayGame");
  
  // Change parental consent status
  hasParentalConsent = ?;
  canPlayGame = age >= 18 ? hasParentalConsent;
  
  // Print the updated results
  print("\nAge: $age");
  print("Has parental consent: $hasParentalConsent");
  print("Can play game: $canPlayGame");
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals