KEMBAR78
Control Structure | PDF | Control Flow | Software Development
0% found this document useful (0 votes)
4 views5 pages

Control Structure

control structure

Uploaded by

Rahul Borate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Control Structure

control structure

Uploaded by

Rahul Borate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Introduction:

Welcome back to this beginner-friendly Dart programming


series! In this post, we will explore control structures and
loops in Dart.

Control structures allow us to make decisions and control


the flow of our code, while loops enable us to repeat a set
of instructions.

By understanding these concepts, you’ll gain the power to


create dynamic and interactive applications. So, let’s dive
in and unlock the secrets of control structures and loops in
Dart!

1. Conditional Statements: Making Decisions with if,


else-if, and else:
Conditional statements allow us to execute different blocks
of code based on certain conditions.
In Dart, we use the if, else-if, and else statements to
handle different scenarios. Here’s an example:

void main() {
int age = 25;
if (age < 18) {
print("You're a minor.");
} else if (age >= 18 && age < 65) {
print("You're an adult.");
} else {
print("You're a senior citizen.");
}
}
2. Switch Statements: Simplifying Multiple Condition
Checks:
Switch statements provide an elegant way to handle
multiple conditions and execute code blocks accordingly.
They can be a concise alternative to a series of if-
else statements. Here’s an example:

void main() {
String grade = 'A';
switch (grade) {
case 'A':
print("Excellent!");
break;
case 'B':
print("Good job!");
break;
case 'C':
print("Keep improving.");
break;
default:
print("You can do better!");
}
}

3. Using Loops: Repeating Instructions with while,


do-while, and for:
Loops allow us to repeat a set of instructions until a
certain condition is met. Dart provides three types of
loops: while, do-while, and for.

Here are examples of each:

 while loop:

void main() {
int count = 0;
while (count < 5) {
print("Count: $count");
count++;
}
}

 do-while loop:

void main() {
int count = 0;
do {
print("Count: $count");
count++;
} while (count < 5);
}

 for loop:

void main() {
for (int i = 0; i < 5; i++) {
print("Index: $i");
}
}

4. Iterating through Lists and Collections: Exploring


for-in and forEach Loops:
Dart provides convenient ways to iterate through lists and
collections using for-in and forEach loops. Here’s an
example:

void main() {
List<int> numbers = [1, 2, 3, 4, 5];
// Using for ... in
for (int number in numbers) {
print("Number: $number");
}

// Using forEach
numbers.forEach((number) {
print("Number: $number");
});
}

5. Breaking and Continuing within Loops: Controlling


Loop Flow:
Sometimes, we may need to exit a loop prematurely or skip
certain iterations.
In Dart, we can use the break and continue statements to
control the flow within loops. Here’s an example:

void main() {
for (int i = 0; i < 5; i++) {
if (i == 3) {
break; // Exit the loop when i equals 3
}
if (i == 1) {
continue; // Skip the iteration when i equals 1
}
print("Index: $i");
}
}

Conclusion:
Congratulations on completing this post of our beginner-
friendly Dart programming series!
In this module, we explored control structures and loops in
Dart. We learned how to make decisions with conditional
statements, simplify multiple condition checks with switch
statements, repeat instructions using while, do-while,

and for loops, iterate through lists and collections with for-

in and forEach loops, and control the flow within loops


using break and continue statements.

You might also like