Menu
CoddyTech

論理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 を宣言します。
    • その人の年齢が18歳以上である(age >= 18)、または
    • その人が保護者の同意を得ている(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");
  
  // 保護者の同意状況を変更する
  hasParentalConsent = true;
  canPlayGame = age >= 0 || hasParentalConsent;
  
  // 更新後の結果を出力する
  print("\nAge: $age");
  print("Has parental consent: $hasParentalConsent");
  print("Can play game: $canPlayGame");
}
quiz icon腕試し

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

基礎のすべてのレッスン

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