CodeBlanks Logo Icon CodeBlanks

Summary - Operators

Combine arithmetic, null-aware and logical operators to build a grade calculator.

Summary - Operators

In this group you learned Dart's operators: arithmetic (+ - * / % ~/), assignment (= += ++ --), comparison (== != < > <= >=), logical (&& || !) and null-aware (?? ?. ??=).

Combining them you can write a real grade calculator. We will practice with two small tasks.

In later lessons we will use these operators in collections, conditional expressions and more complex operations. The foundation is laid here.

Practice Calculate grade

Declare var score = 75;. Print the result with print('Passed: ${score >= 50}'). ${score >= 50} gives a bool result (true).

Starter code

main.dart
// Calculate the grade

Sample solution

main.dart
void main() {
  var score = 75;
  print('Passed: ${score >= 50}');
}

Expected output

Passed: true

Practice Final grade calculator

Declare var score = 85;. Get a bool result with var passed = score >= 50;. Print with print(passed). Expected output: true.

Starter code

main.dart
// Final grade calculator

Sample solution

main.dart
void main() {
  var score = 85;
  var passed = score >= 50;
  print(passed);
}

Expected output

true

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Passing grade is 50 and above. Which condition is correct?

  • score <= 50
  • score > 50
  • score = 50
  • score >= 50 Correct answer

Explanation: >= is used for 50 and above.

2 Default value

Question
int? x; print(x ?? ____);
Answer
int? x; print(x ?? 0);

Explanation: If x is null, 0 is printed.

3 Match all operators

Term Meaning
+ Addition
&& And
?? Default
== Equal

All lessons in Beginner