Skip to content
tezvyn:

Dart's Control Flow: Telling Your Code What to Do Next

Source: dart.devEasyHow cards are made

Control flow statements are the road signs for your code, directing execution beyond a simple top-to-bottom path. Use if/else for decisions, for/while for loops, and try/catch to handle errors. The footgun is forgetting break in a switch case.

Why it exists

Programs need to do more than run line-by-line from top to bottom. They must react to different inputs, repeat tasks, and handle problems without crashing. Control flow statements provide the essential structures to make these dynamic behaviors possible, forming the logical backbone of any non-trivial application.

The mental model

Think of your code as a recipe. Without control flow, you just execute every step in order. Control flow adds conditional steps ("if the oven is hot enough, put the dish in") and loops ("whisk for 2 minutes"). It gives the chef—the program—the ability to make decisions and repeat actions based on the current state of the kitchen.

How it works

Dart provides several kinds of control flow. First, branching statements like if/else and switch execute different code blocks based on a boolean condition. Second, loops like for, while, and for-in repeat a block of code multiple times. Third, exception handling with try/catch/finally lets you run code that might fail (like a network request) and gracefully recover. Finally, jump statements like break and continue let you interrupt or skip iterations within loops and switch cases.

When to use it

Use control flow constantly. Use an if statement to check a condition before acting, like verifying user input is not empty. Use a for loop to iterate over a list of items, like rendering a list of products in a Flutter UI. Use try/catch when calling an API that might fail due to network issues, allowing you to show a user-friendly error message instead of crashing.

When not to use it

Avoid deeply nested if/else blocks, as they become hard to read and maintain; consider a switch statement or refactoring into smaller functions instead. Be careful with while(true) loops; always ensure there's a break condition that will eventually be met to prevent an infinite loop that freezes your app.

One canonical example

A for loop is a classic control flow structure. for (int i = 1; i <= 3; i++) { print('Item $i'); } This code initializes a counter i to 1. On each iteration, it checks if i is less than or equal to 3, runs the print statement, and then increments i. The output will be "Item 1", "Item 2", and "Item 3".

Interview question

What is the primary reason for using control flow statements in a program?

  • a.To allow the program to make decisions and repeat actions based on conditions.Correct
  • b.To manage how data is stored and accessed in memory.
  • c.To define reusable blocks of code for common tasks.
  • d.To declare variables and constants for storing information.
Why?

The card explains that control flow allows programs to "react to different inputs, repeat tasks, and handle problems," which directly translates to making decisions and repeating actions. Defining reusable code blocks (functions) is a separate concept, even though functions often contain control flow.

Just read this? Test yourself on what you have been reading.

Read the original → dart.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on dart — each one lists the topics its interview covers.

See open roles