# Control Flow and the if Statement – How Programs Make Decisions

> <div data-node-type="callout">
> <div data-node-type="callout-emoji">💡</div>
> <div data-node-type="callout-text">I recently started teaching a course on C programming and as part of my preparation for class, I usually write out lesson notes that I will be teaching from. I have therefore decided to make these into blog posts so anyone else learning C can also benefit from them. Therefore, this lesson is part of the series I am calling: <strong>C Programming for Absolute Beginners. </strong>I hope you enjoy it. Ask any questions or leave comments if you need further clarification or want to make a suggestion.</div>
> </div>

In the [previous lesson](https://blog.ehoneahobed.com/variables-in-c-how-programs-store-data), your program could store and print data.

Now we move to something more powerful.

We will teach the program how to **make decisions**.

Because real programs don’t just run from top to bottom.  
They check conditions and decide what to do next.

In this lesson, you’ll learn:

*   What control flow is
    
*   How C evaluates conditions
    
*   How to use the `if` statement
    
*   How comparison operators work
    

Let’s begin.

## 1\. What Is Control Flow?

By default, C runs code from top to bottom.

Example:

```c
printf("Hello\n");
printf("World\n");
```

The output will always be:

```plaintext
Hello
World
```

This is called **sequential execution**.

But what if we want something to happen only when a condition is true?

That’s where control flow comes in.

Control flow determines:

*   Which code runs
    
*   When it runs
    
*   Whether it runs at all
    

## 2\. Understanding Conditions

A condition is something that can be either:

*   True
    
*   False
    

Example:

```plaintext
5 > 3     → true
2 == 10   → false
```

In C:

*   True is any non-zero value
    
*   False is zero
    

You don’t need to memorize that deeply yet.  
Just remember: conditions must evaluate to true or false.

## 3\. The `if` Statement

The `if` statement allows a program to run code only if a condition is true.

### Basic Syntax

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

Notice:

*   The condition is inside parentheses.
    
*   The code block is inside curly braces `{ }`.
    

## 4\. Simple Example

```c
#include <stdio.h>

int main() {
    int age = 20;

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

    return 0;
}
```

Explanation:

*   `age >= 18` is the condition.
    
*   If it is true, the message prints.
    
*   If it is false, nothing happens.
    

## 5\. Comparison Operators

These operators help us create conditions.

<table style="min-width: 50px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><th colspan="1" rowspan="1"><p>Operator</p></th><th colspan="1" rowspan="1"><p>Meaning</p></th></tr><tr><td colspan="1" rowspan="1"><p><code>==</code></p></td><td colspan="1" rowspan="1"><p>equal to</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>!=</code></p></td><td colspan="1" rowspan="1"><p>not equal</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>&gt;</code></p></td><td colspan="1" rowspan="1"><p>greater than</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>&lt;</code></p></td><td colspan="1" rowspan="1"><p>less than</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>&gt;=</code></p></td><td colspan="1" rowspan="1"><p>greater than or equal</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>&lt;=</code></p></td><td colspan="1" rowspan="1"><p>less than or equal</p></td></tr></tbody></table>

Example:

```c
if (score == 50)
```

Important Warning:

`=` means assignment  
`==` means comparison

Beginners mix this up all the time.

This is wrong:

```c
if (score = 50)
```

This assigns 50 instead of checking it.

Always use `==` when comparing.

## 6\. Another Example: Pass or Not

```c
#include <stdio.h>

int main() {
    int score = 45;

    if (score >= 50) {
        printf("You passed!\n");
    }

    return 0;
}
```

Here:

*   If score is 50 or more → it prints.
    
*   If score is below 50 → nothing prints.
    

The program makes a decision.

## 7\. What Happens If the Condition Is False?

If the condition is false, the code inside the braces does not run.

Example:

```c
int temperature = 10;

if (temperature > 30) {
    printf("It is hot.\n");
}
```

Since 10 is not greater than 30, nothing prints.

The program simply moves on.

## 8\. Common Beginner Mistakes

1.  Using `=` instead of `==`
    
2.  Forgetting parentheses around the condition
    
3.  Forgetting curly braces
    
4.  Writing conditions that don’t make sense (like `if (5)`)
    

Always structure it like this:

```c
if (condition) {
    // action
}
```

## 9\. Practice Exercises

Try writing these programs:

1.  Create a variable called `number`.  
    If it is greater than 0, print “Positive”.
    
2.  Create a variable called `age`.  
    If it is 18 or more, print “Can vote”.
    
3.  Create a variable called `temperature`.  
    If it is less than 15, print “Cold”.
    

## Final Thoughts

Today you learned how to:

*   Understand program flow
    
*   Write conditions
    
*   Use comparison operators
    
*   Control when code runs
    

This is the beginning of real logic in programming.

In the next lesson, we will expand this with:

*   `if–else`
    
*   `else if`
    
*   Logical operators like AND and OR
    

And that’s when your program starts thinking in more complex ways.
