Skip to main content

Command Palette

Search for a command to run...

Control Flow and the if Statement – How Programs Make Decisions

Published
4 min read
D

Heya! 👋 I love helping people, and one of the best ways I do this is by sharing my knowledge and experiences. My journey reflects the power of growth and transformation, and I’m here to document and share it with you.

I started as a pharmacist, practicing at a tertiary hospital in the Northern Region of Ghana. There, I saw firsthand the challenges in healthcare delivery and became fascinated by how technology could offer solutions. This sparked my interest in digital health, a field I believe holds the key to revolutionizing healthcare.

Determined to contribute, I taught myself programming, mastering tools like HTML, CSS, JavaScript, React, PHP, and more. But I craved deeper knowledge and practical experience. That’s when I joined the ALX Software Engineering program, which became a turning point. Spending over 70 hours a week learning, coding, and collaborating, I transitioned fully into tech.

Today, I am a Software Engineer and Digital Health Solutions Architect, building and contributing to innovative digital health solutions. I combine my healthcare expertise with technical skills to create impactful tools that solve real-world problems in health delivery.

Imposter syndrome has been part of my journey, but I’ve learned to embrace it as a sign of growth. Livestreaming my learning process, receiving feedback, and building in public have been crucial in overcoming self-doubt. Each experience has strengthened my belief in showing up, staying consistent, and growing through challenges.

Through this platform, I document my lessons, challenges, and successes to inspire and guide others—whether you’re transitioning careers, exploring digital health, or diving into software development.

I believe in accountability and the value of shared growth. Your feedback keeps me grounded and motivated to continue this journey. Let’s connect, learn, and grow together! 🚀

💡
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: C Programming for Absolute Beginners. I hope you enjoy it. Ask any questions or leave comments if you need further clarification or want to make a suggestion.

In the previous lesson, 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:

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

The output will always be:

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:

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

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

Notice:

  • The condition is inside parentheses.

  • The code block is inside curly braces { }.

4. Simple Example

#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.

Operator

Meaning

==

equal to

!=

not equal

>

greater than

<

less than

>=

greater than or equal

<=

less than or equal

Example:

if (score == 50)

Important Warning:

= means assignment
== means comparison

Beginners mix this up all the time.

This is wrong:

if (score = 50)

This assigns 50 instead of checking it.

Always use == when comparing.

6. Another Example: Pass or Not

#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:

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:

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.

More from this blog

D

Dr. Ehoneah Obed

78 posts

Software engineer writing about systems: in code, in learning, in life. I reverse-engineer complex problems into frameworks. Pharmacist → SWE → Founder.