# Days 3-4: Flow Control and Functions

#### Objective:

* 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\. Flow Control

**1.1 If-Else Statements:**

The `if-else` statement is used to execute code blocks based on a condition.

```dart
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:**

* The program checks if the `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.

```dart
void main() {
  for (int i = 0; i < 5; i++) {
    print('i = $i');
  }
}
```

**Explanation:**

* The loop starts from `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.

```dart
void main() {
  int i = 0;

  while (i < 5) {
    print('i = $i');
    i++;
  }
}
```

**Explanation:**

* The loop runs as long as `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.

```dart
void main() {
  int i = 0;

  do {
    print('i = $i');
    i++;
  } while (i < 5);
}
```

**Explanation:**

* The loop runs at least once and continues as long as `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.

```dart
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:**

* The `switch` statement evaluates the value of `day` and executes the matching case.
    

---

### 2\. Functions

Functions are reusable blocks of code that perform a specific task.

**2.1 Defining and Calling Functions:**

```dart
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:**

```dart
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.

```dart
void main() {
  List<int> numbers = [1, 2, 3, 4, 5];

  numbers.forEach((number) {
    print(number);
  });
}
```

**Explanation:**

* The `forEach` method takes an anonymous function and applies it to each element in the list.
    

---

### Examples with Explanations

**Example 1: Using If-Else and Loops**

```dart
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**

```dart
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.
    

---

### Visual Explanations

**1\. If-Else Statement:**

* **Figure 1.1:** Flowchart of an if-else statement.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716504316126/84e9b58f-abae-4ee5-b728-b41b42d8c804.png align="left")
    

* **Figure 1.2:** Example of using if-else statement in Dart.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716505359461/3f266899-71c0-4467-88a4-a4b409ae19fb.png align="center")
    

**2\. For Loop:**

* **Figure 2.1:** Flowchart of a for loop.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716512167849/6106fc71-d87b-4b6a-abc7-92d513be3f67.jpeg align="left")
    

* **Figure 2.2:** Example of using for loop in Dart.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716510680084/c4639dbf-3fa5-44ad-a7df-c766b91bf449.png align="center")
    

**3\. While Loop:**

* **Figure 3.1:** Flowchart of a while loop.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716512407745/f15ef2a8-3689-418f-b83a-ccc1cc2462ea.jpeg align="left")
    

* **Figure 3.2:** Example of using while loop in Dart.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716510916155/d1779143-838c-440a-a8d9-48337c16cfda.png align="center")
    

**4\. Do-While Loop:**

* **Figure 4.1:** Flowchart of a do-while loop.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716512586394/f1c8b026-4841-4d6d-93f7-d78c7e12021b.jpeg align="left")
    

* **Figure 4.2:** Example of using do-while loop in Dart.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716515852827/8f982e6a-9afa-4551-b7b9-368f4caffef9.png align="center")
    

**5\. Switch-Case Statement:**

* **Figure 5.1:** Flowchart of a switch-case statement.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716514856216/26ac34d5-c9d0-4ab2-aea4-addaadae3fb9.jpeg align="left")
    

* **Figure 5.2:** Example of using switch-case statement in Dart.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716516084746/1eb13618-8e1d-466c-b35b-75d16e5c06de.png align="center")
    

**6\. Functions:**

* **Figure 6.1:** Diagram of a function call.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716515282460/ef1304f0-41c5-4163-b398-68caedf554b2.jpeg align="left")
    

* **Figure 6.2:** Example of using function call in Dart.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1716516521385/773535b1-b943-4bf8-bc7d-d78065670f3b.png align="center")
    

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!
