Menu
Coddy logo textTech

論理 OR

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

論理OR演算子||)は、少なくとも1つの式が 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 を宣言します:
    • person が 18 歳以上である(age >= 18)、または
    • person が保護者の同意を得ている(hasParentalConsent が true)
  4. 正確に以下のフォーマットで結果を出力します:
Age: 15
Has parental consent: true
Can play game: true

次に hasParentalConsent を false に変更し、同じフォーマットで更新された結果を出力します。

自分で試してみよう

void main() {
  // ここに変数を宣言してください
  // 各プレースホルダーの値を、タスクで説明されている値に置き換えてください
  int age = 0;
  bool hasParentalConsent = false;
  
  // その人がゲームをプレイできるか確認する
  bool canPlayGame = age >= 0 || hasParentalConsent;
  
  // 初期結果を出力する
  print("Age: $age");
  print("Has parental consent: $hasParentalConsent");
  print("Can play game: $canPlayGame");
  
  // Change parental consent status
  hasParentalConsent = true;
  canPlayGame = age >= 0 || hasParentalConsent;
  
  // 更新された結果を出力する
  print("\nAge: $age");
  print("Has parental consent: $hasParentalConsent");
  print("Can play game: $canPlayGame");
}
quiz icon腕試し

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

基礎のすべてのレッスン

自分で練習してみよう: Dartオンラインコンパイラ