If Statement in C

quiz
Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Overview

When situations come in our real life we need to make some decisions and based on these decisions, we decide what should we do next. either we should do this thing-1 or we should do this thing-2. Similar situations occur in programming also where we need to make some decisions and based on these decisions we execute the next block of statement. So, in this article, we will see the If Statement in C.

Scope of Article

  • This article defines the If Statement in C and explains the intuitive logic of the control flow of the statement. We also learn how to use if statements in the C program.
  • This article shows the types of if statements in C with the help of programming examples.
  • This article also explains How does an if statement in C works and the advantages and disadvantages of using them.
  • This article also explains Important points that need to remember before using the if statement in C.

What is If Statement ?

In C language, the if statement is a simple decision-making and branching statement and it is used to control the flow of the program execution.
If statement is a two-way branching statement and it involves boolean expression. It means depending on the condition the control is transferred either to the true block or false block. It is also called a control statement.

Syntax of If Statement

if(expression)

In the above syntax. we apply conditions in the paranthesis brackets and depending on this condition the control is transferred either to the true block or false block

Flow Diagram of C If Statement

Flow Diagram of C If Statement

How does an If Statement Work?

If statement allows to evaluate the test expression first and then, building upon whether the condition of the expression is true(non-zero) or false(zero), it shifts the control to a particular block of statement. This point of the program has two paths to follow, one path for the true condition and the other path for the false.

If a certain condition is true then it will execute a block of the statement below it otherwise not.

Some examples of control statement, using if statement in C:

if(gender is Female)
   Person is Female
if(age is more than 60)
   person is retired

Types of If Statement

if statement may be implemented in different forms depending on the complexity of testing conditions to be evaluated.

  • Simple if Statement
  • if-else Statement
  • Nested if-else Statement
  • else-if Ladder

Simple if Statement:

If the given condition is true then the statements inside the body of “if” will be executed. If the condition is false then the statements inside the body of “if” will be skipped.

The general form of a simple if statement is given below:

if(condition)
{
    block of the statement;
}
another block of statement;

general form of a simple if statement

In the above general form of simple if statement, the ‘block of the statement’ can be a either single statement or it can be also a group of statements.

  • If the given condition of expression is true:
    -- The ‘block of the statement’ will be executed;
    -- Otherwise, the ‘block of the statement’ will be skipped and the execution of the program will jump to the ‘another block of statement’.

Note: In a simple if statement, when the condition is true of the expression then both the block of statement and the another block of the statement will be executed frequently.

Illustration of Simple If Statement:


#include<stdio.h>
int main(){
    int n;
    printf("Enter a Number");
    scanf("%d",&n);
    for(int i = 1; i <= n; i++){
        if(i%2 == 0){
            continue;
        }
        printf("%d\n", i);
    }
    return 0;
}

Output:

Enter a Number10
1
3
5
7
9

In the above example we are printing all odd numbers till n. The condition we have applied in the condition block of if statement is checking whether it is odd number and if condition is true then it is printing all odd numbers in new line.

If-else Statement

The if-else statement is an extension of the simple if control statement. The general form of the if-else statement is given below:

if(test expression) {
  true-block statement(s)
}
else {
  false-block statement(s)
}
  another-statement

If the test expression is true, then the true-block statement(s), immediately following the if statements are executed first; otherwise, the false-block statement(s) are executed first.

This is illustrated in the below flowchart. In both cases, the control is transferred to another statement.

If-else Statement

Illustration of If-else Statement:

#include <stdio.h>
int main() {
    int n;
    printf("Enter the number: ");
    scanf("%d", &n);

    if  (n%2 == 0) {
        printf("%d is an even number.",n);
    }
    else {
        printf("%d is an odd number.",n);
    }

    return 0;
}

Output:

Enter the number: 6
6 is an even number.

In the above example, we are checking that given number is even or odd. The condition we have applied in the condition block of if statement is checking whether it is even number if the given number is even then it will print statement of if block and if the given number is not even then the control is transferred to the else block and according to the else block statment it will print the statement.

Nested If-else Statement:

When multiple decisions are involved, we can use more than one if-else statement in nested form. In the flowchart below we can see:

  • If condition-1 is false the statement-3 will be executed, and condition-1 is true then the control is transferred to condition-2.
  • If condition-2 is true, statement-1 will be executed; otherwise, statement-2 will be evaluated and then the control is transferred to another block of statement.

Nested If-else Statement

Illustration of Nested If-else Statement:

#include <stdio.h>
int main()
{
   int n1, n2;
   printf("Input the value of n1:");
   scanf("%d", &n1);
   printf("Input the value of n2:");
   scanf("%d",&n2);
   if (n1 != n2)
   {
	printf("n1 is not equal to n2\n");
	//Nested if else
	if (n1 > n2)
	{
		printf("n1 is greater than n2\n");
	}
	else
	{
		printf("n2 is greater than n1\n");
	}
   }
   else
   {
	printf("n1 is equal to n2\n");
   }
   return 0;
}

Output:

Input the value of n1:90
Input the value of n2:80
n1 is not equal to n2
n1 is greater than n2

Else-if Ladder:

There is another way of setting up if statement together when multi-way decisions are involved. A multi-way decision is a series of ifs in which the statement linked with each else statement is an if statement.

Syntax:

if(test expression) {
  true-block statement
}
else if(test expression){
  block of statement
}
else if(test expression){
 block of statement
}
else {
  false-block statement
}

Else-if Ladder

Illustration of Else-if Ladder Statement:

#include <stdio.h>

int main() {
    char button;
    printf("Input a character:");
    scanf("%c", &button);
    if(button == 'a')
    {
        printf("Hello");
    }
    else if(button == 'b')
    {
        printf("Namastey");
    }
    else if(button == 'c')
    {
        printf("Hola");
    }
    else if(button == 'd')
    {
        printf("Ciao");
    }
    else if(button == 'e')
    {
        printf("Salut");
    }
    else {
        printf("I am still learning more...");
    }
    return 0;
}

Output:

Input a character: e
Salut

Important Points Need to Remember

  • Never put semicolon just after the if(expression).
  • A non-zero value is considered as true and a zero(0) value is considered as false in C.
  • We can use more than one condition inside the if statement using the logical operator.
  • We should always use braces on separate lines to identify a block of statements.
  • We should always align the opening and closing braces.
  • Do not ignore placing parentheses for the if condition/expression.
  • Be aware of dangling else statements.
  • Avoid using operands that have side effects in a logical binary expression such as (a-- && ++b). The second operand may not be evaluated in any case.

Advantages and Disadvantages of C If Statement

Advantages

  • It checks every condition, It also makes a program more robust by allowing only a portion of code to run if a condition has been met.
  • If statements are required to make decisions in the programs. Technically, this could be done with loops and goto(break). But in reality, the if statement is most concise.

Disadvantages

During execution as it checks every condition:

  • This makes it difficult to test the code.
  • It is a little bit complex in terms of reading conditions of programs as compared to the switch case.
  • It takes more time for each possible condition because it does fall through all the if statements compared to switch case.

Conclusion

  • Using if statement we can control the flow of statement(s) in the program.
  • There are four types of if Statement in c: simple if, if-else, nested if-else, and else-if ladder.
  • In C, if statement supports two-way branching statement and multi-way branching statement.
  • We can ignore the ‘else’ part of the program statement and we can simply show the result of the ‘if’ condition/expression in our program.
Challenge Time!
quiz
quiz
Time to test your skills and win rewards! Note: Rewards will be credited after the next product update.
Free Courses by top Scaler instructors
certificate icon
Certificates
C Tutorial
This program includes modules that cover the basics to advance constructs of C Tutorial. The highly interactive and curated modules are designed to help you become a master of this language.'
If you’re a learning enthusiast, this is for you.
Module Certificate
Criteria
Upon successful completion of all the modules in the hub, you will be eligible for a certificate.
You need to sign in, in the beginning, to track your progress and get your certificate.
rcbGet a Free personalized Career Roadmap from