Using If-Else Statements in C Programming

CCBeginner
Practice Now

Introduction

In C programming, the if else statement is used to execute a block of code conditionally. If the condition specified in the if statement is true, then the code inside the if block is executed, otherwise, the code inside the else block is executed. In this lab, you will learn how to use the if else statement in C programming language.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL c(("`C`")) -.-> c/UserInteractionGroup(["`User Interaction`"]) c(("`C`")) -.-> c/BasicsGroup(["`Basics`"]) c(("`C`")) -.-> c/ControlFlowGroup(["`Control Flow`"]) c(("`C`")) -.-> c/PointersandMemoryGroup(["`Pointers and Memory`"]) c(("`C`")) -.-> c/FunctionsGroup(["`Functions`"]) c/UserInteractionGroup -.-> c/output("`Output`") c/BasicsGroup -.-> c/variables("`Variables`") c/BasicsGroup -.-> c/data_types("`Data Types`") c/BasicsGroup -.-> c/operators("`Operators`") c/ControlFlowGroup -.-> c/if_else("`If...Else`") c/UserInteractionGroup -.-> c/user_input("`User Input`") c/PointersandMemoryGroup -.-> c/memory_address("`Memory Address`") c/FunctionsGroup -.-> c/function_parameters("`Function Parameters`") c/FunctionsGroup -.-> c/function_declaration("`Function Declaration`") subgraph Lab Skills c/output -.-> lab-123265{{"`Using If-Else Statements in C Programming`"}} c/variables -.-> lab-123265{{"`Using If-Else Statements in C Programming`"}} c/data_types -.-> lab-123265{{"`Using If-Else Statements in C Programming`"}} c/operators -.-> lab-123265{{"`Using If-Else Statements in C Programming`"}} c/if_else -.-> lab-123265{{"`Using If-Else Statements in C Programming`"}} c/user_input -.-> lab-123265{{"`Using If-Else Statements in C Programming`"}} c/memory_address -.-> lab-123265{{"`Using If-Else Statements in C Programming`"}} c/function_parameters -.-> lab-123265{{"`Using If-Else Statements in C Programming`"}} c/function_declaration -.-> lab-123265{{"`Using If-Else Statements in C Programming`"}} end

Creating the main.c file

Create a file named main.c in the ~/project/ directory, and include the necessary header files.

#include <stdio.h>

Getting input from the user

In this step, we will get a number from the user using the scanf function and store it in a variable number.

int number;
printf("Please enter a number:\n");
scanf("%d", &number);

Using the if else statement

In this step, we will use the if else statement to check whether the entered number is less than, equal to, or greater than 100 and display an appropriate message to the user.

if(number < 100)
{
    printf("Number is less than 100!\n");
}
else if(number == 100)
{
    printf("Number is 100!\n");
}
else
{
    printf("Number is greater than 100!\n");
}

Putting it all together

Combine all the above steps in the main function.

#include <stdio.h>

int main()
{
    int number;
    printf("Please enter a number:\n");
    scanf("%d", &number);

    if(number < 100)
    {
        printf("Number is less than 100!\n");
    }
    else if(number == 100)
    {
        printf("Number is 100!\n");
    }
    else
    {
        printf("Number is greater than 100!\n");
    }

    return 0;
}

Summary

In this lab, you have learned how to use the if else statement in C programming language to conditionally execute a block of code. You have also learned the syntax of the if else statement, how to get input from the user, and how to display output to the user.

Other C Tutorials you may like