Days 3-4: Flow Control and Functions

I am a developer from Indonesia
Search for a command to run...

I am a developer from Indonesia
No comments yet. Be the first to comment.
Exploring Shortcut Routes to Professionalism: Delving into Dart, Flutter, and Serverpod for Building Stunning Applications. Github repository : https://github.com/ahliweb/learning-dart
Objective: Learn list, set, map. Start with basic OOP concepts: classes, objects, properties, methods. 1. Collections in Dart 1.1 List: A list is an ordered collection of items. Lists in Dart are similar to arrays in other languages. void main()...
Dokumen Nilai, Arah Hidup, dan Pedoman Implementasi 1. Pendahuluan Prinsip Hidup AhliWeb merupakan fondasi nilai yang menjadi arah berpikir, bersikap, mengambil keputusan, bekerja, membangun bisnis,
![[Indonesian] Prinsip Hidup AhliWeb Versi 2](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fuploads%2Fcovers%2F6608de5cd660c76b8626061d%2F8a6aa718-4fc2-493c-86ad-5f26b1dcaf99.png&w=3840&q=75)
Pendahuluan AW Non-Commercial License 1.0 adalah lisensi perangkat lunak source-available yang dirancang untuk memungkinkan akses publik terhadap source code sekaligus tetap menjaga kontrol eksklusif
![[Indonesian] Memahami Wiki AW Non-Commercial License 1.0](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fuploads%2Fcovers%2F6608de5cd660c76b8626061d%2Ff2bf3812-7079-4c4f-a6f9-fe2ba1c0aa06.png&w=3840&q=75)
Introduction The AW Non-Commercial License 1.0 is a source-available software license designed to allow public access to source code while preserving the copyright holder’s exclusive control over comm

Indonesia menghadapi tantangan besar di ruang digital, di mana jumlah pengguna media sosial telah melebihi 191,4 juta jiwa per Februari 2022, mencakup sekitar 68,9% dari populasi. Peningkatan sirkulasi konten ini, sayangnya, disertai dengan risiko ti...

🔰 RINGKASAN SINGKAT STRATEGI MARKETING HODi: HODi (Hybrid Omnichannel Distribution Initiative) adalah strategi distribusi dan pemasaran terpadu yang menggabungkan kekuatan online + offline, otomatisasi + human touch, teknologi + komunikasi personal...
Learn if-else, loops (for, while, do-while), and switch-case.
Create simple examples to understand flow control.
Learn to create and call functions.
1.1 If-Else Statements:
The if-else statement is used to execute code blocks based on a condition.
void main() {
int number = 10;
if (number > 0) {
print('$number is positive');
} else if (number < 0) {
print('$number is negative');
} else {
print('$number is zero');
}
}
Explanation:
number is greater than, less than, or equal to zero and prints the appropriate message.1.2 Loops:
For Loop: The for loop is used to repeat a block of code a fixed number of times.
void main() {
for (int i = 0; i < 5; i++) {
print('i = $i');
}
}
Explanation:
i = 0 and runs until i < 5, incrementing i by 1 in each iteration.While Loop: The while loop is used to repeat a block of code as long as a condition is true.
void main() {
int i = 0;
while (i < 5) {
print('i = $i');
i++;
}
}
Explanation:
i < 5 and increments i in each iteration.Do-While Loop: The do-while loop is similar to the while loop but guarantees at least one iteration.
void main() {
int i = 0;
do {
print('i = $i');
i++;
} while (i < 5);
}
Explanation:
i < 5.1.3 Switch-Case Statement:
The switch-case statement is used to execute one code block among many based on the value of a variable.
void main() {
int day = 3;
switch (day) {
case 1:
print('Monday');
break;
case 2:
print('Tuesday');
break;
case 3:
print('Wednesday');
break;
case 4:
print('Thursday');
break;
case 5:
print('Friday');
break;
case 6:
print('Saturday');
break;
case 7:
print('Sunday');
break;
default:
print('Invalid day');
}
}
Explanation:
switch statement evaluates the value of day and executes the matching case.Functions are reusable blocks of code that perform a specific task.
2.1 Defining and Calling Functions:
void greet(String name) {
print('Hello, $name!');
}
void main() {
greet('Alice');
greet('Bob');
}
Explanation:
The greet function takes a String parameter and prints a greeting message.
The main function calls greet with different arguments.
2.2 Returning Values from Functions:
int add(int a, int b) {
return a + b;
}
void main() {
int sum = add(3, 5);
print('Sum: $sum');
}
Explanation:
The add function takes two int parameters and returns their sum.
The main function calls add and prints the result.
2.3 Anonymous Functions:
Anonymous functions are functions without a name, often used for short operations.
void main() {
List<int> numbers = [1, 2, 3, 4, 5];
numbers.forEach((number) {
print(number);
});
}
Explanation:
forEach method takes an anonymous function and applies it to each element in the list.Example 1: Using If-Else and Loops
void main() {
int number = 5;
if (number % 2 == 0) {
print('$number is even');
} else {
print('$number is odd');
}
for (int i = 1; i <= 10; i++) {
print('$number x $i = ${number * i}');
}
}
Explanation:
The program checks if the number is even or odd and prints the result.
It then prints the multiplication table for the number using a for loop.
Example 2: Switch-Case with Functions
String getDayName(int day) {
switch (day) {
case 1:
return 'Monday';
case 2:
return 'Tuesday';
case 3:
return 'Wednesday';
case 4:
return 'Thursday';
case 5:
return 'Friday';
case 6:
return 'Saturday';
case 7:
return 'Sunday';
default:
return 'Invalid day';
}
}
void main() {
int day = 4;
print('Day $day is ${getDayName(day)}');
}
Explanation:
The getDayName function takes a day number and returns the corresponding day name using a switch-case statement.
The main function calls getDayName and prints the result.
1. If-Else Statement:
Figure 1.1: Flowchart of an if-else statement.

Figure 1.2: Example of using if-else statement in Dart.

2. For Loop:
Figure 2.1: Flowchart of a for loop.

Figure 2.2: Example of using for loop in Dart.

3. While Loop:
Figure 3.1: Flowchart of a while loop.

Figure 3.2: Example of using while loop in Dart.

4. Do-While Loop:
Figure 4.1: Flowchart of a do-while loop.

Figure 4.2: Example of using do-while loop in Dart.

5. Switch-Case Statement:
Figure 5.1: Flowchart of a switch-case statement.

Figure 5.2: Example of using switch-case statement in Dart.

6. Functions:
Figure 6.1: Diagram of a function call.

Figure 6.2: Example of using function call in Dart.

By following this guide, you'll have a solid understanding of flow control statements and functions in Dart, enabling you to write more complex and efficient programs. Happy coding!