Menu
Coddy logo textTech

אופרטורי השוואה

חלק מהיחידה יסודות במסלול ה-Dart של Coddy. שיעור 22 מתוך 94.

אופרטורי השוואה ב-Dart מחזירים ערכים בוליאניים, החיוניים לקבלת החלטות.

void main() {
  int a = 10;
  int b = 10;
  bool areEqual = a == b;
  
  print('Is $a equal to $b? $areEqual');
}

Dart מציעה: שוויון (==), אי־שוויון (!=), גדול מ־(>), קטן מ־(<), גדול מ־או שווה ל־(>=), וקטן מ־או שווה ל־(<=).

challenge icon

אתגר

מתחילים

צרו תוכנית שמשווה בין שתי תוצאות במשחק באמצעות אופרטורי השוואה:

  1. הכריזו על שני משתנים שלמים: playerScore עם הערך 85 ו-highScore עם הערך 90
  2. השתמשו באופרטורי ההשוואה כדי לבדוק:
    • אם playerScore שווה ל-highScore
    • אם playerScore אינו שווה ל-highScore
    • אם playerScore גדול מ-80
    • אם playerScore קטן מ-highScore
    • אם playerScore גדול או שווה ל-85
    • אם playerScore קטן או שווה ל-100
  3. שמרו כל תוצאת השוואה במשתנה בוליאני עם שם שמתאר אותה
  4. הדפיסו את כל התוצאות עם תוויות בדיוק כפי שהן מופיעות בפלט הצפוי

הפלט שלכם חייב להתאים בדיוק לתבנית הזאת:

Player score: 85
High score: 90
Scores are equal: false
Scores are different: true
Player score > 80: true
Player score < High score: true
Player score >= 85: true
Player score <= 100: true

נסו בעצמכם

void main() {
  // הכריזו כאן על משתני הניקוד
  int playerScore = ?;
  int highScore = ?;
  
  // בצעו השוואות ושמרו את התוצאות במשתנים בוליאניים
  bool scoresEqual = playerScore ? highScore;
  bool scoresDifferent = playerScore ? highScore;
  bool scoreAbove80 = playerScore ? 80;
  bool scoreLessThanHigh = playerScore ? highScore;
  bool scoreAtLeast85 = playerScore ? 85;
  bool scoreAtMost100 = playerScore ? 100;
  
  // הדפיסו את הניקוד ואת תוצאות ההשוואות
  print("Player score: $playerScore");
  print("High score: $highScore");
  print("Scores are equal: $scoresEqual");
  print("Scores are different: $scoresDifferent");
  print("Player score > 80: $scoreAbove80");
  print("Player score < High score: $scoreLessThanHigh");
  print("Player score >= 85: $scoreAtLeast85");
  print("Player score <= 100: $scoreAtMost100");
}
quiz iconבחנו את עצמכם

השיעור הזה כולל חידון קצר. התחילו את השיעור כדי לענות עליו ולעקוב אחרי ההתקדמות.

כל השיעורים ביחידה יסודות

תרגלו בעצמכם: קומפיילר Dart אונליין