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 switch statements. This section will cover the following topics:
- Conditional Statements
if
andelse
else if
- Ternary Operator
- Loops
for
loopwhile
loopdo-while
loop
- Switch Statements
- 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.
Syntax:
if (condition) { // code to be executed if condition is true } else { // code to be executed if condition is false }
Example:
void main() { int number = 10; if (number > 0) { print('The number is positive.'); } else { print('The number is not positive.'); } }
else if
The else if
statement allows you to check multiple conditions.
Syntax:
if (condition1) { // code to be executed if condition1 is true } else if (condition2) { // code to be executed if condition2 is true } else { // code to be executed if both conditions are false }
Example:
void main() { int number = 0; if (number > 0) { print('The number is positive.'); } else if (number < 0) { print('The number is negative.'); } else { print('The number is zero.'); } }
Ternary Operator
The ternary operator is a shorthand for if-else
statements.
Syntax:
Example:
void main() { int number = 5; String result = number > 0 ? 'Positive' : 'Negative or Zero'; print(result); }
- Loops
for
Loop
The for
loop is used to execute a block of code a specific number of times.
Syntax:
Example:
while
Loop
The while
loop executes a block of code as long as a specified condition is true.
Syntax:
Example:
do-while
Loop
The do-while
loop is similar to the while
loop, but it guarantees that the block of code is executed at least once.
Syntax:
Example:
- Switch Statements
The switch
statement allows you to execute one block of code among many based on the value of a variable.
Syntax:
switch (variable) { case value1: // code to be executed if variable == value1 break; case value2: // code to be executed if variable == value2 break; default: // code to be executed if variable doesn't match any case }
Example:
void main() { String grade = 'A'; switch (grade) { case 'A': print('Excellent'); break; case 'B': print('Good'); break; case 'C': print('Fair'); break; case 'D': print('Poor'); break; default: print('Invalid grade'); } }
Practical Exercises
Exercise 1: Even or Odd
Write a Dart program that checks if a number is even or odd.
Solution:
void main() { int number = 4; if (number % 2 == 0) { print('The number is even.'); } else { print('The number is odd.'); } }
Exercise 2: Factorial Calculation
Write a Dart program to calculate the factorial of a number using a for
loop.
Solution:
void main() { int number = 5; int factorial = 1; for (int i = 1; i <= number; i++) { factorial *= i; } print('The factorial of $number is $factorial.'); }
Exercise 3: Grade Evaluation
Write a Dart program that evaluates a student's grade using a switch
statement.
Solution:
void main() { String grade = 'B'; switch (grade) { case 'A': print('Excellent'); break; case 'B': print('Good'); break; case 'C': print('Fair'); break; case 'D': print('Poor'); break; default: print('Invalid grade'); } }
Common Mistakes and Tips
- Forgetting the
break
statement inswitch
cases: This can lead to fall-through behavior, where multiple cases are executed. - Infinite loops: Ensure that the loop's condition will eventually become false to avoid infinite loops.
- Using the ternary operator for complex conditions: The ternary operator is best used for simple conditions. For complex conditions, use
if-else
statements for better readability.
Conclusion
In this section, you learned about control flow statements in Dart, including conditional statements, loops, and switch statements. These constructs are essential for controlling the flow of your program and making decisions based on conditions. Practice the exercises provided to reinforce your understanding and prepare for more advanced topics.
Dart Programming Course
Module 1: Introduction to Dart
- Introduction to Dart
- Setting Up the Development Environment
- Your First Dart Program
- Basic Syntax and Structure