⚠️ This lesson is retired and might contain outdated information.

Control Flow Statements in Dart

Jermaine Oppong
InstructorJermaine Oppong
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 2 years ago

In this lesson, we will look at various ways we can control the flow of our Dart code. Control flow statements allow us to break up the flow of code by employing decision-making, looping, branching and enabling our program to conditionally execute particular blocks of code. Dart adopts common control flow statements such as if/else, switch, while, do-while and for loops.

Instructor: [00:00] Control flow statements are possible with Dart in the following forms. A nest statement allows us to evaluate a condition and decide what to do depending on its outcome.

[00:11] The else block is optional, which reflects behavior similar across most programming languages. The condition we are evaluating must explicitly result to a Boolean value, else an error is thrown.

[00:24] You can also chain your if statements. You can write the statement in its short form using the ternary operator. Dart support various for loops with a familiar syntax.

[00:39] We can also capture the values of the current loop index inside a closure. When working with iterable types like list and set, you can use the for each method to loop over its items.

[00:52] You could also use a for loop for iterable types. The while loop can be used as an alternate form of the for loop. Use a dual loop to ensure that a print logic runs at least once. Use a break statement to stop a running loop. Use continue to skip the current loop iteration.

[01:19] Use switch and case statements for comparing integers, strings, and compile time constant values. Here, the break statement stops further execution of the switch statement. Omitting the break will give you an error unless you are following through to the next block within the sequence.

[01:43] Use an assert to disrupt code execution if the given condition evaluates the false. Parse a string as the second argument to attach a message to the assert. This concludes the lesson.

~ 2 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today