BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

If statement in C programming with example

Last Updated: September 23, 2017 by Chaitanya Singh | Filed Under: c-programming

When we need to execute a block of statements only when a given condition is true then we use if statement. In the next tutorial, we will learn C if..else, nested if..else and else..if.

C – If statement

Syntax of if statement:
The statements inside the body of “if” only execute if the given condition returns true. If the condition returns false then the statements inside “if” are skipped.

if (condition)
{
     //Block of C statements here
     //These statements will only execute if the condition is true
}

Flow Diagram of if statement

C-if-statement

Example of if statement

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

Output:

Variable x is less than y

Explanation: 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.

Example of multiple if statements

We can use multiple if statements to check more than one conditions.

#include <stdio.h>
int main()
{
    int x, y;
    printf("enter the value of x:");
    scanf("%d", &x);
    printf("enter the value of y:");
    scanf("%d", &y);
    if (x>y)
    {
	printf("x is greater than y\n");
    }
    if (x<y)
    {
	printf("x is less than y\n");
    }
    if (x==y)
    {
	printf("x is equal to y\n");
    }
    printf("End of Program");
    return 0;
}

In the above example the output depends on the user input.

Output:

enter the value of x:20
enter the value of y:20
x is equal to y
End of Program
❮ PreviousNext ❯

Top Related Articles:

  1. File I/O in C programming with examples
  2. C – Strings and String functions with examples
  3. C – If..else, Nested If..else and else..if Statement with example
  4. C Program Structure – First C Program
  5. User-defined function in C with Examples

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

C Programming Tutorial

  • C Tutorial
  • History of C
  • Features of C
  • Turbo C++ installation
  • First C Program
  • printf scanf
  • Variables in C
  • Data Types in C
  • C - Keywords
  • C Identifiers
  • C Comments
  • Operator precedence
  • C - if statement
  • C - if..else
  • C - for loop
  • C - while loop
  • C - do while loop
  • C - continue
  • C - break statement
  • C - switch..case
  • C - goto statement
  • C - Arrays
  • 2 D array
  • C - String
  • C - functions
  • Function call by reference
  • Function call by value
  • Array to function
  • C - Structures
  • C - Pointers
  • Pointer to Pointer
  • Pointers to functions
  • C - function pointers
  • Pointer & Array
  • C - File I/O
  • C Programming Examples

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap