If Statement in C programming

If Statement in C programming

C if Statement; In this tutorial, you will learn everything about if statement in C programming with examples.

C if Statement

In c programming, if statement is a programming conditional statement that, if proved true, performs a function or displays information.

In other words; If statements in C is used to control the program flow based on some specified condition in the program, it’s used to execute some block of code, if the given expression is evaluated to true. Otherwise, it will get skipped. This is the simplest way to modify the control flow of the program.

Syntax of C if Statement

The syntax of the if statement in C programming; as shown below:

if (test expression) 
{
   // code
}

Example 1 – if Statement in C

See the following c program for if statement; as shown below:

// Program to display a number if it is negative

#include <stdio.h>
int main() {
    int number;

    printf("Enter an integer: ");
    scanf("%d", &number);

    // true if number is less than 0
    if (number < 0) {
        printf("You entered %d.\n", number);
    }

    printf("The if statement is easy.");

    return 0;
}

Output of the above c program

Enter an integer: -2
You entered -2.
The if statement is easy.

Explaination of above c program

The if statement evaluates the test expression inside the parenthesis ().

  • If the test expression is evaluated to true, statements inside the body of if are executed.
  • If the test expression is evaluated to false, statements inside the body of if are not executed.

Example 2 – if statement in c

See the second c program for if statement; as shown below:

#include <stdio.h>
int main()
{
    int x = 20;
    int y = 22;
    if (x<y)
    {
        printf("Variable x is less than y");
    }
    return 0;
}

Output of the above c program

Variable x is less than y

Explanation of above c program

  • The condition (x<y) specified in the “if” returns true for the value of x and y, so the statement inside the body of if is executed.

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *