If Statement in C Programming

The if statement in C programming language is used to execute a block of code only if condition is true.

Syntax of If Statement

if(condition_expression) {
    /* code to be execute if the condition_expression is true */

The if statement checks whether condition_expression is true or false. If the condition_expression is true, then the block of code inside the if statement is executed but if condition_expression is false, then the block of code inside the if statement is ignored and the next statement after if block is executed.

C If Statement Program Control flow Diagram

C Program to print Pass or Failed using If Condition Statement

#include <stdio.h>
#include <conio.h>

int main(){

    int marks;
    printf("Enter your Marks : ");
    scanf("%d", &marks);
    /* Using if statement to decide whether 
       a student failed in examination or not*/
    if(marks < 40){
        printf("You Failed :(\n");
    if(marks >= 40){
        printf("Congrats You Passed :)\n");

Above program check whether a student passed or failed in examination using if statement.

Enter your Marks : 70
Congrats You Passed :)
Enter your Marks : 35
You Failed :(

Points to Remember about If Statement

  • The condition_expression is a boolean expression. It must evaluate to true or false value(In C, all non-zero and non-null values are considered as true and zero and null value is considered as false).
  • We may use more than one condition inside if statement
    For Example:
    if(condition_one && condition_two) {
        /* if code block */
  • Opening and Closing Braces are optional, If the block of code of if statement contains only one statement
    For Example:
    In above example only statement1 will gets executed if condition_expression is true.