Menu
Coddy logo textTech

Nested 'if' Statements

Part of the Fundamentals section of Coddy's Dart journey. Lesson 35 of 94.

Nested if statements check conditions inside other conditions, creating complex decision paths. The inner condition only evaluates when the outer condition is true.

void main() {
  int age = 25;
  bool hasLicense = true;
  
  if (age >= 18) {
    print('You are an adult');
    
    if (hasLicense) {
      print('You can drive a car');
    }
  }
}

Output:

You are an adult
You can drive a car
challenge icon

Challenge

Easy

Create a program that determines a student's grade based on their exam score and attendance record:

  1. Declare an integer variable examScore with a value of 78
  2. Declare a boolean variable hasGoodAttendance with a value of true
  3. Use nested if statements to determine the student's grade according to these rules:
  • If examScore is 90 or above, the grade is "A"
  • If examScore is between 80 and 89:
    • If hasGoodAttendance is true, the grade is "B+"
    • Otherwise, the grade is "B"
  • If examScore is between 70 and 79:
    • If hasGoodAttendance is true, the grade is "C+"
    • Otherwise, the grade is "C"
  • If examScore is below 70, the grade is "F"

Print the final grade with the exact format: Student grade: X (where X is the determined grade)

Try it yourself

void main() {
  // Declare your variables here
  int examScore = 78;
  bool hasGoodAttendance = true;
  String grade;
  
  // Implement nested if statements to determine the grade

  
  // Print the final grade
  print("Student grade: " + grade);
}
quiz iconTest yourself

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

All lessons in Fundamentals

Practice on your own: Online Dart compiler