Menu
Coddy logo textTech

論理 OR

CoddyのDartジャーニー「基礎」セクションの一部 — レッスン 24/94。

論理 OR 演算子 (||) は、少なくとも一つの式が true の場合に true を返し、両方が false の場合にのみ false を返します。

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

出力: 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

チャレンジ

初心者

論理 OR (||) 演算子を使用して、ゲームの参加資格チェックを作成してください:

  1. 値 15 の整数型変数 age を宣言してください
  2. 値 true のブール型変数 hasParentalConsent を宣言してください
  3. ブール型変数 canPlayGame を宣言し、以下のいずれかをチェックしてください:
    • その人が 18 歳以上である (age >= 18)、または
    • その人が親の同意を得ている (hasParentalConsent is true)
  4. この正確な形式で結果を出力してください:
Age: 15
Has parental consent: true
Can play game: true

次に hasParentalConsent を false に変更し、同じ形式で更新された結果を出力してください。

チートシート

論理 OR 演算子 (||) は、少なくとも1つの式が true の場合に true を返し、両方が false の場合にのみ false を返します。

bool canPayOnline = hasCreditCard || hasDebitCard;

OR を他の論理演算子と組み合わせることができます:

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

自分で試してみよう

void main() {
  // ここで変数を宣言してください
  int age = ?;
  bool hasParentalConsent = ?;
  
  // その人がゲームをプレイできるかどうかをチェック
  bool canPlayGame = age >= ? || hasParentalConsent;
  
  // 初期の結果を出力
  print("Age: $age");
  print("Has parental consent: $hasParentalConsent");
  print("Can play game: $canPlayGame");
  
  // 親の同意ステータスを変更
  hasParentalConsent = ?;
  canPlayGame = age >= 18 ? hasParentalConsent;
  
  // 更新された結果を出力
  print("\nAge: $age");
  print("Has parental consent: $hasParentalConsent");
  print("Can play game: $canPlayGame");
}
quiz icon腕試し

このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。

基礎のすべてのレッスン