CS31-AppDev (Android Development and Emerging Technologies)
College of Computer Studies
I. INTRODUCTION
Control Flow/Structures can be considered as
the building blocks of computer programs. They
are commands that enable a program to “take
decisions”, following one path or another. A
program is usually not limited to a linear
Module 2 sequence of instructions since during its process
it may bifurcate, repeat code or bypass sections.
Introduction to Dart Control Flow/Structures are the blocks that
analyze variables and choose directions in which
Programming - Control to go based on given parameters.
Structures II. OBJECTIVES
1. Define the different forms of control flow,
statements, and expressions
2. Analyze and explain the importance of
control flow, statement, and expression
in a program
3. Create simple program using control flow,
statement, and expression
III. PRELIM INARY ACTIVITIES
N/A
IV. LESSON PROPER
A. Control Flows
This page shows how you can control the flow of your Dart code using loops and supporting
statements:
for loops
while and do while loops
break and continue
You can also manipulate control flow in Dart using:
Branching, like if and switch
Exceptions, like try, catch, and throw
Page 1 of 9
CS31-AppDev (Android Development and Emerging Technologies)
College of Computer Studies
B. For loops
You can iterate with the standard for loop. For example:
Closures inside of Dart's for loops capture the value of the index. This avoids a common pitfall found in
JavaScript. For example, consider:
The output is 0 and then 1, as expected. In contrast, the example would print 2 and then 2 in JavaScript.
Sometimes you might not need to know the current iteration counter when iterating over an Iterable type,
like List or Set. In that case, use the for-in loop for cleaner code:
To process the values obtained from the iterable, you can also use a pattern in a for-in loop:
Page 2 of 9
CS31-AppDev (Android Development and Emerging Technologies)
College of Computer Studies
Iterable classes also have a forEach() method as another option:
C. While and do-while
A while loop evaluates the condition before the loop:
A do-while loop evaluates the condition after the loop:
Page 3 of 9
CS31-AppDev (Android Development and Emerging Technologies)
College of Computer Studies
D. Break and Continue
Use break to stop looping:
Use continue to skip to the next loop iteration:
E. Branches
This part of the module will show you how you can control the flow of your Dart code using
Branches or also known as conditional statements:
If Statements
Switch statements
Switch expressions
Page 4 of 9
CS31-AppDev (Android Development and Emerging Technologies)
College of Computer Studies
F. If Statement
Dart supports if statements with optional else clauses. The condition in parentheses after if must be
an expression that evaluates to a Boolean:
G. Switch statement
A switch statement evaluates a value expression against a series of cases. Each case clause is
a pattern for the value to match against. You can use any kind of pattern for a case.
When the value matches a case's pattern, the case body executes. Non-empty case clauses jump to the
end of the switch after completion. They do not require a break statement. Other valid ways to end a non-
empty case clause are a continue, throw, or return statement.
Use a default or wildcard “_” clause to execute code when no case clause matches:
You can use logical-or and logical-and patterns to allow cases to share a body or a guard.
Page 5 of 9
CS31-AppDev (Android Development and Emerging Technologies)
College of Computer Studies
H. Switch Expressions
A switch expression produces a value based on the expression body of whichever case matches. You
can use a switch expression wherever Dart allows expressions, except at the start of an expression
statement. For example:
If you want to use a switch at the start of an expression statement, use a switch statement.
Switch expressions allow you to rewrite a switch statement like this:
Into an expression, like this:
Page 6 of 9
CS31-AppDev (Android Development and Emerging Technologies)
College of Computer Studies
The syntax of a switch expression differs from switch statement syntax:
Cases do not start with the case keyword.
A case body is a single expression instead of a series of statements.
Each case must have a body; there is no implicit fall through for empty cases.
Case patterns are separated from their bodies using => instead “of:”.
Cases are separated by, (and an optional trailing, is allowed).
Default cases can only use _, instead of allowing both default and _.
I. Guard Clause
To set an optional guard clause after a case clause, use the keyword when. A guard clause can follow if
case, and both switch statements and expressions.
Guards evaluate an arbitrary boolean expression after matching. This allows you to add further
constraints on whether a case body should execute. When the guard clause evaluates to false,
execution proceeds to the next case rather than exiting the entire switch.
J.
Page 7 of 9
CS31-AppDev (Android Development and Emerging Technologies)
College of Computer Studies
V. PRACTICE EXERCISES/ACTIVITIES
Exercises:
Print the following output using while, do-while and for loops (separately):
1.
Enter number: 7
1
2
3
5
8
13
21
Page 8 of 9
CS31-AppDev (Android Development and Emerging Technologies)
College of Computer Studies
2.
Enter text: PNUEMONOULTRAMICROSCOPICSILICAVOLCANOCONIOSIS
Character count: 45
Consonant count: 25
Vowel count: 20
VI. ADDITIONAL RESOURCES
VII. ASSESSMENT
VIII. REFERENCES
https://dart.dev/language/
Page 9 of 9