If Else Statements in C Langauge

If Else statements in C Langauge with Examples

In this article, I am going to discuss If Else Statements in C Langauge with Examples i.e. how if and if-else block gets executed with the help of syntax, flow chart, and multiple examples. Please read our previous articles, where we discussed the basics of Control Statements in C. Before understanding if-else statements, let us first understand the Selection Statements in C Langauge.

What are Selection Statements in C?

Selection statements allow you to control the flow of program execution on the basis of the outcome of an expression or state of a variable known during run time. It executes different sections of code depending on a specific condition or the value of the variable. Selection statements can be divided into the following categories:

  • if-else statements (Will discuss in this article)
  • switch statements (Will discuss in the next article)
If Block in C Programming Language:

It executes a block of instructions (one or more instructions) when the condition in the if block is true and when the condition is false, it will skip the execution of the if block. Following is the syntax to use the if block.

If Block in C Programming Language

Flow Chart of If Block:

Let us see how we will represent the execution flow of the if block using a flow chart. The starting point is represented by the oval symbol. And the flow will be from the condition and the condition is represented by a diamond shape. Here, first, we need to check the condition. And for every condition, two options are there i.e. if conditions are successful (condition is true) and if conditions are failed (condition is false). That means two situations are there i.e. TRUE and FALSE. Suppose, the condition is TRUE, then what all statements are defined inside the if block gets executed. And the statements we are representing in a flow chart with the help of a parallelogram symbol. And after the execution of the statements, the control will come to end. Suppose, the condition is false, then without executing any statement, it will come to the end. For better understanding, please have a look at the below diagram which represents the flow chart of the if conditional statement.

Flow Chart of If Block

Note: Here, the block of statements executes only when the condition is true. And if the condition is false, then it will skip the execution of the statements.

Example: WAP to check whether the number is greater than 10

Here, we will take the number from the user and then we will check whether that number is greater than 10 or not using If Statement in C Language. The following program exactly does the same.

#include<stdio.h>
int main()
{
    int number;
    printf("Enter a Number : ");
    scanf("%d", &number);
    if(number > 10)
    {
        printf("%d is greater than 10 \n", number);
        printf("End of if block \n");
    }

    printf("End of Main Method");
    return 0;
}

In the above program, inside the main method, we are declaring one integer variable i.e. number, and then we are taking the input from the user by using the scanf function and storing it in the number variable.

After reading the input we are checking whether the given number is greater than 10. If the number > 10, then if the condition is true and, in that case, we are executing the two statements that are present inside the block else if the condition is false, then the if block statements will be skipped and the last printf statement gets executed.

For example,

  1. We take 15 as an input, 15 > 10 means the condition is true, then the if block statement gets executed.
  2. We take 5 as an input, 5 > 10 means the condition is false, then the if block statements will be skipped

For a better understand, please have a look at the below image.

WAP to check whether the number is greater than 10

If Statement without Curly Braces in C Language

In the declaration of if block if we do not specify statements using blocks ({}) nothing but braces, then only the first statement will be considered as the if block statement. To understand this point please have a look at the below example.

#include<stdio.h>
int main()
{
    int number;
    printf("Enter a Number : ");
    scanf("%d", &number);
    if(number > 10)
        printf("%d is greater than 10 \n", number);
        printf("End of Main Method");

    return 0;
}

As you can see, in the above example, we have not specified the curly braces to define the if block. In this case, only the first statement will be considered as the if block statement. The second statement will not be a part of the if block. For better understanding, please have a look at the below image. The statement which is in red color will belong to the if block and the statement which is in the black color do not belong to the if block.

If Statement without Curly Braces in C Language

So, when you execute the above program, irrespective of the condition, the black statement is always going to be executed as it is not part of the if block. The red statement is only executed when if the condition is true. For a better understand, please have a look at the below image.

If Statement without Curly Braces in C Language with Examples

If Else Block in Programming Language:

The If-Else block is used to provide some optional information whenever the given condition is FALSE in the if block. That means if the condition is true, then the if block statements will execute, and if the condition is false, then the else block statement will execute. Following is the syntax to use IF ELSE block in most of the programming languages.

If Else Block in Programming Language

Note: The point that you need to remember is, only one block of statement i.e. either if block or else block is going to be executed at a time. So, if the condition is TRUE if block statements get executed and if the condition is FALSE, else block statements get executed.

Is it mandatory to use else block?

No, it is not mandatory to use else block. It is an optional block. You can use only if block also. If you want to provide any information when the condition is failed, then you need to use this optional else block.

Flow Chart of If-Else Block:

The flow chart of the if-else block is almost similar to the if block. In this case, when the condition is true, the if block statements get executed and when the condition is false, the else block statements get executed. For better understanding, please have a look at the below image which shows the flow chart of the if-else block.

Flow Chart of If-Else Block

Points to Remember:

The ‘if’ control statement allows you to check the validity of a certain condition and perform required operations depending on the condition. If the condition followed by the ‘if’ keyword holds True, the code is written inside the braces of the ‘if’ statement will be executed, otherwise the program control will skip the loop execution and continue with the remaining program. ‘if’ statement is generally accompanied by the ‘else’ block which lets the compiler know about actions to be performed if the condition following the ‘if’ statement is False.

Note: In C Programming Langauge, if and else are reserved words. The expressions or conditions specified in the if block can be a Relational or Boolean expression or condition that evaluates to a TRUE(1) or FALSE(0). 

Now let us see some examples to understand the if-else conditional statements.

Example: Write a Program to check whether a number is even or odd.

Here we will take the input number from the user and then we will check whether that number is even or odd using the if-else statement in C Language. The following program exactly does the same.

#include<stdio.h>
int main()
{
    int number;
    printf("Enter a Number : ");
    scanf("%d", &number);
    if(number % 2 == 0)
    {
        printf("%d is an Even Number", number);
    }
    else
    {
        printf("%d is an Odd Number", number);
    }
    return 0;
}

In the above program, inside the main method, we are declaring one integer variable i.e. number and then we are reading input from the user using the scanf function and storing the value in the address of the number variable. After reading the input we are checking whether the given number is even or odd. An Even number is a number that is divisible by 2.

If number % 2 equals 0, then the if condition is true and, in that case, we are printing a message that it is an even number and if the condition is false then we are printing a message that it is an odd number.

For example,

  1. We take 16 as an input, 16%2 == 0 means the condition is true, then the if block statement gets executed. And the output will be 16 is an Even Number.
  2. We take 13 as an input, 13%2 == 0 means condition is false, then the else block statements get executed. And the output will be 13 is an Odd Number.

For a better understand, please have a look at the below image.

Write a Program to check whether a number is even or odd

If and Else Block without Curly Braces in C Programming Language

In the declaration of if block or else block if we do not specify statements using blocks ({}) nothing but braces, then only the first statement will be considered as the if block or else block statement. Let us understand this point with some examples. Please have a look at the below example.

#include<stdio.h>
int main()
{
    if(1 == 1)
        printf("Hi\n");
    else
        printf("Hello\n");
        printf("Bye\n");
    return 0;
}

As you can see, in the above example, while creating the if and else block we have not specified the curly braces. So, in this case, the first printf statement will belong to the if block. After the else statement, we have two printf statements. Here, the printf statement which printing the Hello message will belongs to the else block only. The next printf statement which printing the message bye will not belong to else block. Now, if you execute the above program then you will get the following output.

If and Else Block without Curly Braces in C Programming Language

Now, if we replace the Hello statement in the if block, then an ERROR message will be displayed. The reason is, not more than one statement gets executed without braces. One statement will execute inside the if block. If we want to execute more than one statement then you should use braces and all the statements will be inside the braces. For better understanding, please have a look at the below example.

#include<stdio.h>
int main()
{
    if(1 == 1)
        printf("Hi\n");
        printf("Hello\n");
    else
        printf("Bye\n");
    return 0;
}

Now, while compiling the code, you will get the following error.

If and Else Statement without Curly Braces in C Programming Language

Note: For every if condition statement else block is optional. But for every else block if block is compulsory. The purpose of the ‘if’ statement in a program is to allow multiple execution paths for varying user inputs, making it more interactive!

In the next article, I am going to discuss Nested If-Else Statements in C Language with examples. Here, in this article, I try to explain if-else Statements in C with examples. I hope you enjoy this if-else Statements in C with examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

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