if statement in C with example program

if statement is one of the selection statements of C language.  It uses to select the statements depending on the value of controlling expression. In C programming, we use the if condition when we want to control the flow of statement(s) in the program. Or you can say that want to execute a statement or block of statements for a given condition (nonzero).

Syntax of if statement in C:

if ( controlling expression )
statement;

In the above scenario, the statement will only be executed if the controlling expression is non-zero.

 

Note: The statement could be a single statement or a compound statement.  A Compound or block statement is a group of statements within curly braces “{}”.  All statements of the compound statement are executed sequentially.

 

If statement

 

How does an if statement work?

I believe you are familiar with the basic syntax of the if selection statement. And for now, you have learned that an “if statement” executes a certain section of code if the controlling expression compares unequal to 0.

But how does it work let’s understand it with a programming example. Here I will not talk about the if-else statement. I have already written separate articles on C if-else statement.

The following C program uses to find the even and odd numbers with the help of the “if selection statement”.

#include <stdio.h>

int main(void)
{
    int data; //variable to store data

    /*Get input from the user*/
    printf("\n Enter any number: = ");
    scanf("%d", &data);

    /* If number is divisible by 2 then it is a even number */
    if((data % 2) == 0)
    {
        printf("\n Number is Even.\n\n");
    }
    else
    {
        printf("\n Number is Odd.\n\n");
    }

    return 0;
}

Output:

if in C

 

How the above program works:

1 Case: When you enter an even number:

We know that the modular division of an even number by 2 is 0. So expression (data % 2) return 0, as we know expression ( 0 == 0) return 1.

So for even number if((data % 2) == 0) become if(1), as per C standard if the controlling  expression is  non-zero then body associated with if will be execute. So statement printf(“\n Number is Even.\n\n”) will be execute.

 

2 Case: When you enter an odd number:

For odd number expression (data % 2) return 1, so  expression ( 1== 0) return 0. So for an odd number if((data % 2) == 0) becomes if(0), so the body of the “if statement” will be skipped, and print the message associated with the else statement.

 

Points need to remember before using the if statement:

1. Don’t put a semicolon just after the if(expression). If you put the semicolon after the if selection statement, then it has no meaning to write the “if selection statement” in your C Programming.

For better understanding consider the below C code.

#include <stdio.h>

int main()
{
    int data;
    
    printf("\n Enter any number: = ");
    scanf("%d", &data);
    
    if((data % 2) == 0); //<<semicolon
    printf("\n Number is Even.\n\n");
    
    return 0;
}

Due to the semicolon, there is no effect of the if statement in the code. The program will print “Number is Even” for any number.

 

2. Braces are optional when only a single statement belongs to an if statement.

Consider the below codes.

#include<stdio.h>

int main()
{
    if(1)
    printf("statement1 \n");
    printf("statement2 \n");
    printf("statement3 \n");

    return 0;
}

 

The second way is to use curly braces with a single statement. It is advisable to use braces with if statement. It increases the code readability.

#include<stdio.h>

int main()
{
    if(1)
    {
        printf("statement1 \n");
    }
    printf("statement2 \n");
    printf("statement3 \n");

    return 0;
}

 

3. Any non-zero value is considered true and zero (0) is considered false in C.

4. We can also call the function in place of controlling expression in the “if statement” but the function should return scaler type.

For example,

//str1 and str2 are two c string

if(!strcmp(str1,str2))
{

  //body

}

 

5. Using the logical operator we can combine more than one condition with an if statement.

Consider the below code where we have clubbed two conditions the number will print only if it is even and greater than 100.

#include <stdio.h>

int main()
{
    int data;

    printf("\n Enter any number: = ");
    scanf("%d", &data);

    if((data % 2) && (data > 100))
    {
        printf("Number is %", data);
    }

    return 0;
}

 

6.  Do not forget to use braces in the if statement. Because if there is more than one statement, that is associated with a controlling expression then without braces only the statement takes into account just after the if.

if (condition)
    printf("Print when condition is non-zero");
    
printf("Print always does not depend on condition");

 

7. Always avoid using the operands that have side effects in a logical binary expression. Consider the below code.

#include <stdio.h>

int main()
{
    int a = 1;
    int b = 2;

    if(--a && b++)
    {
        printf("%d%d", a, b);
    }
    else
    {
        printf("%d%d", a, b);
    }

    return 0;
}

 

 

Recommended Articles for you: