Control flow statements are fundamental in programming as they allow you to dictate the flow of your program's execution. In Dart, control flow statements include conditional statements, loops, and other constructs that help manage the execution path of your code.
Key Concepts
-
Conditional Statements
if
andelse
switch
andcase
-
Loops
for
loopwhile
loopdo-while
loop
-
Other Control Flow Constructs
break
continue
return
Conditional Statements
if
and else
The if
statement allows you to execute a block of code based on a condition. The else
statement can be used to execute a block of code if the condition is false.
void main() { int number = 10; if (number > 0) { print('The number is positive.'); } else { print('The number is not positive.'); } }
switch
and case
The switch
statement allows you to execute one block of code among many options. It is often used as an alternative to multiple if-else
statements.
void main() { int day = 3; switch (day) { case 1: print('Monday'); break; case 2: print('Tuesday'); break; case 3: print('Wednesday'); break; default: print('Invalid day'); } }
Loops
for
Loop
The for
loop is used to execute a block of code a specific number of times.
while
Loop
The while
loop executes a block of code as long as a specified condition is true.
do-while
Loop
The do-while
loop is similar to the while
loop, but it guarantees that the block of code will be executed at least once.
Other Control Flow Constructs
break
The break
statement is used to exit a loop or switch statement prematurely.
continue
The continue
statement skips the current iteration of a loop and proceeds to the next iteration.
return
The return
statement is used to exit a function and optionally return a value.
Practical Exercises
Exercise 1: FizzBuzz
Write a program that prints the numbers from 1 to 20. For multiples of three, print "Fizz" instead of the number, and for the multiples of five, print "Buzz". For numbers that are multiples of both three and five, print "FizzBuzz".
void main() { for (int i = 1; i <= 20; i++) { if (i % 3 == 0 && i % 5 == 0) { print('FizzBuzz'); } else if (i % 3 == 0) { print('Fizz'); } else if (i % 5 == 0) { print('Buzz'); } else { print(i); } } }
Exercise 2: Factorial Calculation
Write a function that calculates the factorial of a given number using a for
loop.
int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; } return result; } void main() { int number = 5; print('Factorial of $number is ${factorial(number)}'); }
Common Mistakes and Tips
- Forgetting to update loop variables: Ensure that loop variables are updated correctly to avoid infinite loops.
- Misplacing
break
andcontinue
statements: Use these statements carefully to control the flow of loops. - Incorrect conditions in
if
statements: Double-check conditions to ensure they are logically correct.
Conclusion
Control flow statements are essential for managing the execution path of your Dart programs. By mastering conditional statements, loops, and other control flow constructs, you can write more efficient and readable code. Practice the exercises provided to reinforce your understanding and prepare for more advanced topics in Dart programming.
Flutter Development Course
Module 1: Introduction to Flutter
- What is Flutter?
- Setting Up the Development Environment
- Understanding Flutter Architecture
- Creating Your First Flutter App
Module 2: Dart Programming Basics
- Introduction to Dart
- Variables and Data Types
- Control Flow Statements
- Functions and Methods
- Object-Oriented Programming in Dart
Module 3: Flutter Widgets
- Introduction to Widgets
- Stateless vs Stateful Widgets
- Basic Widgets
- Layout Widgets
- Input and Form Widgets
Module 4: State Management
Module 5: Navigation and Routing
Module 6: Networking and APIs
- Fetching Data from the Internet
- Parsing JSON Data
- Handling Network Errors
- Using REST APIs
- GraphQL Integration
Module 7: Persistence and Storage
- Introduction to Persistence
- Shared Preferences
- File Storage
- SQLite Database
- Using Hive for Local Storage
Module 8: Advanced Flutter Concepts
- Animations in Flutter
- Custom Paint and Canvas
- Platform Channels
- Isolates and Concurrency
- Performance Optimization
Module 9: Testing and Debugging
Module 10: Deployment and Maintenance
- Preparing for Release
- Building for iOS
- Building for Android
- Continuous Integration/Continuous Deployment (CI/CD)
- Maintaining and Updating Your App