Menu
Coddy logo textTech

The 'else' Statement

Part of the Fundamentals section of Coddy's Dart journey — lesson 32 of 94.

The else statement executes code when the if condition is false.

if (condition) {
  // Code to execute when condition is true
} else {
  // Code to execute when condition is false
}

Example:

void main() {
  int number = 10;
  
  if (number > 0) {
    print('The number is positive');
  } else {
    print('The number is not positive');
  }
}
challenge icon

Challenge

Easy

Create a program that determines if a student has passed or failed an exam based on their score:

  1. Declare an integer variable examScore with a value of 65
  2. Write an if statement that checks if the score is greater than or equal to 70
  3. If the condition is true, print "Exam result: Pass"
  4. Add an else statement that executes when the condition is false
  5. In the else block, print "Exam result: Fail"

Your output should match the expected format exactly.

Cheat sheet

The else statement executes code when the if condition is false.

if (condition) {
  // Code to execute when condition is true
} else {
  // Code to execute when condition is false
}

Example:

void main() {
  int number = 10;
  
  if (number > 0) {
    print('The number is positive');
  } else {
    print('The number is not positive');
  }
}

Try it yourself

void main() {
  // Declare the examScore variable here
  int examScore = _;
  
  // Write your if-else statement here

}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals