⚠️ 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

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.