> For the complete documentation index, see [llms.txt](https://cs.d19.in/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cs.d19.in/c-programming/basics/control-flow.md).

# Control Flow

Now we're stepping into the **control flow** of C, which is how we make decisions in our code and control the order in which statements execute. Control flow gives your program the ability to **branch**, **loop**, and **handle decisions** based on conditions.

Let’s break it down into two major categories: **decision-making** and **loops**.

***

#### **1. Decision-Making:**

In C, decision-making statements allow you to execute a block of code only if a certain condition is true. The main decision-making structures are:

* **`if` statement**
* **`else` statement**
* **`else if` ladder**
* **`switch` statement**

**a. `if` statement**

The `if` statement is used to check a condition. If the condition is true, the block of code inside the `if` statement is executed.

**Syntax:**

```c
if (condition) {
    // code to execute if the condition is true
}
```

**Example:**

```c
int age = 20;
if (age >= 18) {
    printf("You are an adult.\n");
}
```

* The condition `(age >= 18)` is true, so `"You are an adult."` will be printed.

**b. `else` statement**

The `else` block executes when the `if` condition is false.

**Syntax:**

```c
if (condition) {
    // code if condition is true
} else {
    // code if condition is false
}
```

**Example:**

```c
int age = 16;
if (age >= 18) {
    printf("You are an adult.\n");
} else {
    printf("You are a minor.\n");
}
```

* Since `age` is 16, the else block will execute, printing `"You are a minor."`

**c. `else if` ladder**

If you need multiple conditions to check, you can use `else if`.

**Syntax:**

```c
if (condition1) {
    // code if condition1 is true
} else if (condition2) {
    // code if condition2 is true
} else {
    // code if all conditions are false
}
```

**Example:**

```c
int marks = 85;
if (marks >= 90) {
    printf("Grade: A\n");
} else if (marks >= 80) {
    printf("Grade: B\n");
} else if (marks >= 70) {
    printf("Grade: C\n");
} else {
    printf("Grade: D\n");
}
```

* Here, `marks` is 85, so it prints `"Grade: B"`.

**d. `switch` statement**

When you have multiple values to compare a single variable against, a `switch` statement can be more efficient than using multiple `if-else` statements.

**Syntax:**

```c
switch (variable) {
    case value1:
        // code for value1
        break;
    case value2:
        // code for value2
        break;
    default:
        // default code if no case matches
}
```

**Example:**

```c
int day = 2;
switch (day) {
    case 1:
        printf("Monday\n");
        break;
    case 2:
        printf("Tuesday\n");
        break;
    case 3:
        printf("Wednesday\n");
        break;
    default:
        printf("Other day\n");
}
```

* Since `day` is 2, it prints `"Tuesday"`.

***

#### **2. Loops:**

Loops allow you to execute a block of code repeatedly until a condition is met. C provides three types of loops:

* **`while` loop**
* **`do-while` loop**
* **`for` loop**

**a. `while` loop**

The `while` loop repeats a block of code **as long as the condition is true**.

**Syntax:**

```c
while (condition) {
    // code to execute while the condition is true
}
```

**Example:**

```c
int i = 1;
while (i <= 5) {
    printf("%d\n", i);  // prints 1, 2, 3, 4, 5
    i++;
}
```

* The condition `i <= 5` is checked each time, and the loop stops once `i` exceeds 5.

**b. `do-while` loop**

The `do-while` loop is similar to `while`, but the code block is executed **at least once**, even if the condition is false, because the condition is checked **after** the loop body.

**Syntax:**

```c
do {
    // code to execute
} while (condition);
```

**Example:**

```c
int i = 6;
do {
    printf("%d\n", i);  // prints 6 once
    i++;
} while (i <= 5);
```

* Here, `i` starts as 6, so the block runs once, but after that, the condition `i <= 5` is false, so the loop stops.

**c. `for` loop**

The `for` loop is used when you know exactly how many times you want to loop.

**Syntax:**

```c
for (initialization; condition; increment/decrement) {
    // code to execute
}
```

**Example:**

```c
for (int i = 1; i <= 5; i++) {
    printf("%d\n", i);  // prints 1, 2, 3, 4, 5
}
```

* The loop runs as long as the condition `i <= 5` is true. After each iteration, `i` is incremented by 1.

***

#### **3. Breaking Out of Loops**

You can **control** the flow of loops using:

* **`break`**: Immediately exits the loop.
* **`continue`**: Skips the current iteration and moves to the next one.

**a. `break` Example:**

```c
for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        break;  // stops the loop when i equals 3
    }
    printf("%d\n", i);  // prints 1, 2
}
```

**b. `continue` Example:**

```c
for (int i = 1; i <= 5; i++) {
    if (i == 3) {
        continue;  // skips printing when i equals 3
    }
    printf("%d\n", i);  // prints 1, 2, 4, 5
}
```

***

#### **Control Flow Summary**

| Statement  | Purpose                                     |
| ---------- | ------------------------------------------- |
| `if-else`  | Conditional branching based on true/false   |
| `switch`   | Multi-way branching based on variable value |
| `while`    | Loop as long as the condition is true       |
| `do-while` | Loop at least once, then repeat if true     |
| `for`      | Loop a fixed number of times                |
| `break`    | Exit the loop entirely                      |
| `continue` | Skip the current iteration of the loop      |

***
