Menu
Coddy logo textTech

If Statement

Part of the Fundamentals section of Coddy's C journey. Lesson 25 of 63.

The if statement is a fundamental control flow structure in C that allows your program to make decisions.

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

Basic syntax of an if statement:

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

Let's look at a simple example:

int age = 20;

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

In this example:

  • We check if the value in age is greater than or equal to 18
  • If this condition is true, the message "You are an adult." is printed
  • If the condition is false, the program skips the code block and continues with the next statement
challenge icon

Challenge

Easy

You are writing a simple program to check the weather condition based on a temperature value read from the input.

Here’s the task:

  1. The int variable temperature is already declared and read from the input for you. Do not change the lines above the marker.
  2. Use if statements to print messages based on the value of temperature:
    • If the temperature is above 30, print "It's a hot day!"
    • If the temperature is between 20 and 30 (inclusive), print "The weather is nice."
    • If the temperature is below 20, print "It's a bit cold today."

Try it yourself

#include <stdio.h>

int main() {
    int temperature;
    scanf("%d", &temperature);
    // Don't change above this line

    // Write your if statements below
    

    return 0;
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Fundamentals

Practice on your own: Online C compiler