Control flow helps your app decide what to do next. It lets your app choose between options, repeat tasks, or pick actions based on conditions.
Control flow (if, for, while, switch) in Flutter
if (condition) { // code to run if condition is true } else if (anotherCondition) { // code if anotherCondition is true } else { // code if none above are true } for (var item in collection) { // code to run for each item } while (condition) { // code to repeat while condition is true } switch (variable) { case value1: // code for value1 break; case value2: // code for value2 break; default: // code if no case matches break; }
if checks conditions and runs code accordingly.
for repeats code for each item in a list.
while repeats code while a condition stays true.
switch picks code to run based on a variable's value.
if (age >= 18) { print('Adult'); } else { print('Child'); }
for (var i = 0; i < 3; i++) { print('Number $i'); }
int count = 0; while (count < 3) { print('Count is $count'); count++; }
var color = 'red'; switch (color) { case 'red': print('Stop'); break; case 'green': print('Go'); break; default: print('Wait'); break; }
This app shows a greeting based on the current time. It uses if and else if to pick the right greeting.
import 'package:flutter/material.dart'; void main() => runApp(const MyApp()); class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { int hour = DateTime.now().hour; String greeting; if (hour < 12) { greeting = 'Good morning!'; } else if (hour < 18) { greeting = 'Good afternoon!'; } else { greeting = 'Good evening!'; } return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('Control Flow Example')), body: Center( child: Text( greeting, style: const TextStyle(fontSize: 24), ), ), ), ); } }
Always make sure your while loops have a way to stop, or your app may freeze.
Use break; in switch cases to stop checking further cases.
Indent your code inside control flow blocks to keep it clear and easy to read.
Control flow lets your app make decisions and repeat tasks.
if, for, while, and switch are common ways to control flow.
Use them to create interactive and dynamic apps.
Practice
Solution
Step 1: Understand the purpose of control flow statements
ifstatements let the app choose between actions based on conditions.Step 2: Identify the decision-making statement
Among the options, onlyifdirectly tests a condition to decide what code runs.Final Answer:
if-> Option AQuick Check:
Decision making =if[OK]
if to check conditions and decide [OK]- Confusing
forloops with decision making - Thinking
whileis for choices instead of repetition - Assuming
switchis the only decision tool
for loop in Flutter to print numbers 0 to 4?Solution
Step 1: Check the loop initialization and condition
for (int i = 0; i < 5; i++) { print(i); } initializesiat 0 and loops whilei < 5, which prints 0 to 4.Step 2: Verify syntax correctness
for (int i = 0; i < 5; i++) { print(i); } uses correct Dart syntax with types and braces.Final Answer:
for (int i = 0; i < 5; i++) { print(i); } -> Option CQuick Check:
Correct Dartforloop syntax = for (int i = 0; i < 5; i++) { print(i); } [OK]
for loops use semicolons and braces [OK]- Using Python-style loops like 'for i in 0 to 4'
- Off-by-one errors in loop conditions
- Missing type declaration for loop variable
int count = 0;
while (count < 3) {
print(count);
count++;
}Solution
Step 1: Analyze the while loop condition and increments
The loop starts with count = 0 and runs while count < 3, printing count each time.Step 2: Trace the printed values
It prints 0, then increments to 1, prints 1, increments to 2, prints 2, increments to 3, then stops.Final Answer:
0 1 2 -> Option DQuick Check:
Loop prints values less than 3 = 0 1 2 [OK]
- Including 3 in output mistakenly
- Confusing post-increment behavior
- Thinking loop runs zero times
int i = 0;
while (i < 5) {
print(i);
}Solution
Step 1: Check loop control variables
The variableiis initialized but never changed inside the loop.Step 2: Understand loop behavior
Without incrementingi, the conditioni < 5is always true, causing an infinite loop.Final Answer:
Missing increment of i inside the loop -> Option AQuick Check:
Loops need changing variables to stop [OK]
- Forgetting to increment loop counters
- Assuming loop ends automatically
- Misunderstanding while loop syntax
int score = 75;
switch (score ~/ 10) {
case 10:
case 9:
print('Excellent');
break;
case 8:
print('Very Good');
break;
case 7:
print('Good');
break;
default:
print('Keep Trying');
}What message will be printed?
Solution
Step 1: Calculate the switch expression value
score is 75, soscore ~/ 10is integer division 75/10 = 7.Step 2: Match the case for value 7
Case 7 matches and prints 'Good'.Final Answer:
Good -> Option BQuick Check:
Integer division selects case 7 = 'Good' [OK]
- Forgetting integer division operator ~/
- Assuming score matches case directly
- Missing break statements causing fall-through
Ready to prove it?
Pass the test and this pattern moves unknown
