C Academy · Lesson

Conditional Statements

Learn how to use if, else, and switch statements to implement decision-making in C programs.

Lesson 1 of 310 steps

Conditional Statements is a free C Academy lesson on CoddyKit. This is lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, and your progress syncs across the web and the CoddyKit app. The C Academy course includes 3 lessons in total.

Conditional Statements in C

In C, conditional statements allow a program to make decisions based on certain conditions.

In this lesson, you will learn how to use if, else, and switch statements to control program flow.

Conditional Statements — illustration 1

The if Statement

The if statement executes a block of code only if a condition is true.

Syntax:

if (condition) { // Code to execute if condition is true }

Example: if Statement

This program checks if a number is positive.

#include <stdio.h>

int main() {
    int num = 10;
    if (num > 0) {
        printf("The number is positive.\n");
    }
    return 0;
}

if-else Statement

The if-else statement executes one block of code if the condition is true and another block if the condition is false.

Syntax:

if (condition) { // Code if condition is true } else { // Code if condition is false }

Example: if-else Statement

This program checks if a number is positive or negative.

#include <stdio.h>

int main() {
    int num = -5;
    if (num > 0) {
        printf("Positive number.\n");
    } else {
        printf("Negative number.\n");
    }
    return 0;
}

Nested if Statements

You can use multiple if statements inside one another.

Example:

if (condition1) { if (condition2) { // Code block } }

The switch Statement

The switch statement is an alternative to multiple if-else conditions.

Syntax:

switch (variable) { case value1: // Code block break; case value2: // Code block break; default: // Default block }

Example: switch Statement

This program prints the name of a day based on an integer input.

#include <stdio.h>

int main() {
    int day = 3;
    switch (day) {
        case 1:
            printf("Monday\n");
            break;
        case 2:
            printf("Tuesday\n");
            break;
        case 3:
            printf("Wednesday\n");
            break;
        default:
            printf("Invalid day\n");
    }
    return 0;
}

Summary

In this lesson, you learned:

  • How to use if, if-else, and switch statements.
  • How to use nested if statements for complex conditions.
  • The difference between if-else and switch.

Next, we will explore looping structures in C!

Conditional Statements — illustration 10
Free to start

Learn C with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
39
Lessons
144

Frequently Asked Questions

Is the “Conditional Statements” lesson free?

Yes — the full text of “Conditional Statements” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO. The C Academy course includes 3 lessons in total.

What will I learn in “Conditional Statements”?

Learn how to use if, else, and switch statements to implement decision-making in C programs. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 3.

How long does the “Conditional Statements” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Conditional Statements
  2. Looping Structures
  3. Logical and Comparison Operators
← Back to C Academy