Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL SQLITE MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING HTML & CSS BASH RUST DART PLAYWRIGHT TOOLS MATH SHEETS POWER BI

Dart Tutorial

Dart HOME Dart Intro Dart Get Started Dart Syntax Dart Output Dart Comments Dart Variables Dart final and const Dart Data Types Dart Numbers Dart Strings Dart String Interpolation Dart String Methods Dart Booleans Dart Type Conversion Dart Null Safety Dart late Dart Operators Dart Arithmetic Dart Assignment Dart Comparison Dart Logical Dart Type Test Dart Null Operators Dart Cascade Dart If...Else Dart Switch Dart While Loop Dart For Loop Dart Break/Continue

Dart Collections

Dart Lists Dart Access List Items Dart Change List Items Dart Loop Lists Dart List Methods Dart Sets Dart Maps Dart Access Map Items Dart Loop Maps Dart Collection if/for Dart Spread Operator

Dart Functions

Dart Functions Dart Parameters Dart Named Parameters Dart Return Values Dart Arrow Functions Dart Anonymous Functions Dart Closures Dart Scope Dart Recursion

Dart Classes

Dart OOP Dart Classes/Objects Dart Constructors Dart Named Constructors Dart Factory Constructors Dart Getters and Setters Dart Static Members Dart Inheritance Dart Abstract Classes Dart Interfaces Dart Mixins Dart Extension Methods Dart Enums Dart Generics

Dart 3 Features

Dart Records Dart Patterns Dart Switch Expressions Dart Sealed Classes Dart Class Modifiers

Dart Errors

Dart Exceptions Dart Custom Exceptions

Dart Async

Dart Async Intro Dart Futures Dart async/await Dart Streams Dart Isolates

Dart Libraries

Dart Imports Dart Packages Dart Math Dart Dates Dart JSON Dart RegExp Dart User Input Dart Files Dart Testing

Dart Examples

Dart Examples Dart Exercises


Dart If...Else


Share

Dart If...Else

A program often has to make a choice.

The if statement lets Dart run a block of code only when a condition is true.

A condition is an expression that gives a bool value, true or false.

You often build conditions with the comparison operators, like <, >, == and !=.

Dart has these conditional statements:

  • if runs a block of code when a condition is true
  • else runs a block of code when the same condition is false
  • else if tests a new condition when the first condition is false
  • switch picks one of many blocks of code (see the next chapter)

The if Statement

Write the condition in parentheses () after if.

Write the code to run in curly brackets {}.

if (condition) {
  // code to run if the condition is true
}

Here we test if 20 is greater than 18:

Example

void main() {
  if (20 > 18) {
    print('20 is greater than 18');
  }
}
Try it Yourself »

Result:

20 is greater than 18

You can also test variables:

Example

void main() {
  final x = 20;
  final y = 18;
  if (x > y) {
    print('x is greater than y');
  }
}
Try it Yourself »

Result:

x is greater than y

The condition must be a bool.

Dart does not treat numbers or strings as true or false, so if (1) does not work:

Example

void main() {
  final count = 1;
  if (count) {
    print('count is not zero');
  }
}
Try it Yourself »

Result:

main.dart:3:7: Error: A value of type 'int' can't be assigned to a variable of type 'bool'.
  if (count) {
      ^


The else Statement

Use else to run a block of code when the condition is false:

Example

void main() {
  final time = 20;
  if (time < 18) {
    print('Good day.');
  } else {
    print('Good evening.');
  }
}
Try it Yourself »

Result:

Good evening.

The value of time is 20, so the condition time < 18 is false.

Dart skips the first block and runs the else block.


The else if Statement

Use else if to test a new condition when the first condition is false.

Dart tests the conditions from top to bottom and runs only the first block whose condition is true:

Example

void main() {
  final time = 16;
  if (time < 10) {
    print('Good morning.');
  } else if (time < 18) {
    print('Good day.');
  } else {
    print('Good evening.');
  }
}
Try it Yourself »

Result:

Good day.

You can use as many else if parts as you need:

Example

void main() {
  final score = 72;
  if (score >= 90) {
    print('Grade A');
  } else if (score >= 80) {
    print('Grade B');
  } else if (score >= 70) {
    print('Grade C');
  } else {
    print('Grade F');
  }
}
Try it Yourself »

Result:

Grade C

Nested If

You can put an if statement inside another if statement.

This is called a nested if:

Example

void main() {
  final age = 25;
  final hasTicket = true;
  if (age >= 18) {
    print('You are an adult.');
    if (hasTicket) {
      print('You can enter the concert.');
    } else {
      print('You need a ticket.');
    }
  }
}
Try it Yourself »

Result:

You are an adult.
You can enter the concert.

You can often write the same check with the logical operator && instead:

Example

void main() {
  final age = 25;
  final hasTicket = true;
  if (age >= 18 && hasTicket) {
    print('You can enter the concert.');
  }
}
Try it Yourself »

Result:

You can enter the concert.

The Conditional Expression (Ternary)

Dart has a short way to choose between two values.

It is called the conditional expression, or the ternary operator, because it has three parts:

condition ? valueIfTrue : valueIfFalse

Instead of writing this:

Example

void main() {
  final time = 20;
  String greeting;
  if (time < 18) {
    greeting = 'Good day.';
  } else {
    greeting = 'Good evening.';
  }
  print(greeting);
}
Try it Yourself »

Result:

Good evening.

You can simply write:

Example

void main() {
  final time = 20;
  final greeting = time < 18 ? 'Good day.' : 'Good evening.';
  print(greeting);
}
Try it Yourself »

Result:

Good evening.

Tip: Use the conditional expression for short, simple choices.

For longer logic, an if...else statement is easier to read.



Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by W3Schools. All Rights Reserved. W3Schools is Powered by W3.CSS.