C If...Else Statement

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

Learn via video course

C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
By Prateek Narang
Free
star5
Enrolled: 1000
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
Prateek Narang
Free
5
icon_usercirclecheck-01Enrolled: 1000
Start Learning

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

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:

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:

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:

Output:

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

Output:

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:

Output:

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:

Else-if Ladder

Illustration of Else-if Ladder Statement:

Output:

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.