Control Flow in Dart

Control Flow teaches your program to decide and repeat.

In this chapter, you will:

  • make decisions with if, else if, switch and switch expressions
  • repeat work with for, while and do-while loops
  • stop or skip rounds with break and continue
  • write your own functions, with parameters, return values, closures and recursion

What each lesson covers:

  • Conditional Statements: How a program makes decisions with if, else, else if, nested conditions, switch statements and switch expressions.
  • Loops: How to repeat work with for, while and do-while loops, how to stop or skip rounds, nested loops, and looping over a list.
  • Functions: How to write and call functions, return values, positional, optional and named parameters, arrow and anonymous functions, closures, recursion and scope.

This chapter has 3 lessons. Plan on about 53 minutes, with 9 exercises to check what you learned. Every example is real Dart code: change it, run it again and compare the output, which is the fastest way to learn how each feature behaves.

Simple lesson: finish this chapter and you can write programs that choose, repeat and reuse code.

Lessons in this section

  1. Conditional Statements

    How a program makes decisions with if, else, else if, nested conditions, switch statements and switch expressions.

    • Write if, if else and else if chains
    • Use switch statements and switch expressions
    • Pick the right kind of decision for the job
  2. Loops

    How to repeat work with for, while and do-while loops, how to stop or skip rounds, nested loops, and looping over a list.

    • Write for, while and do-while loops
    • Use break and continue
    • Loop over a list with for-in and forEach
  3. Functions

    How to write and call functions, return values, positional, optional and named parameters, arrow and anonymous functions, closures, recursion and scope.

    • Define and call functions that return values
    • Use positional, optional and named parameters
    • Use arrow functions, callbacks, closures and recursion