if-else Statement in C
Learn about if...else statement in C.
Decisions are always taken based on different conditions, whether it is real-life or programming it applies to both. In C programming language, if-else statement is used to perform the operations based on some specific condition. If the given condition is true, then the code inside if block is executed, otherwise else block code is executed. It specifies an order in which the statements are to be executed. If-else statement controls the flow of a program and hence also termed as control statements.
- This article defines the implementation of the decision-making process in C language, using if-else statement.
- We also discover different programs implemented using if-else statement.
- Advantages and Disadvantages of if-else statement.
What is if-else statement in C
In real life, we come across various situations, where we need to take a decision to choose one of the many options available. For e.g, when we come across a traffic signal, there are three different colors of light which indicate different decisions to be made according to the color of the light. If the color of light is red, then we stop, for yellow, we wait and for green, we are ready to go. So, here we need to make decisions according to different conditions.
In the C programming language, the if-else statement is used for decision-making. If the given condition is true, then the code inside if block is executed, otherwise else block code is executed.
In the C programming language, any non-zero and non-null values are assumed as true, and zero or null, values are assumed as false values.
Flowchart of the if-else statement in C
if-statement in C
In the if-else statement, the else block is not necessary, so the if-else statement can be used as only the if statement also.
The if statement is used to evaluate a particular condition. If the condition is true, then the if block statement is executed else the if block is skipped and further program is executed.
It is generally used when we have to perform a single set of operations for a particular condition.
How if-else statement in C works?
If-else statement allows to make a decision according to the given conditions. If the given condition is true, then the statements inside the body of logical 'if' are executed and the statements inside the body of else are not executed. Similarly, if the condition is false, then the statements inside the body of ‘if’ are ignored and the statements inside the ‘else’ are executed.
For a more clear understanding of the concept, let’s take an example of xyz expression:
If the "xyz expression" is true:
- statements inside the body of if are executed
- statements inside the body of else are ignored
If the "xyz expression" is false:
- statements inside the body of if are ignored
- statements inside the body of else are executed
Basically, the if-else statement controls the flow of a program and is hence also termed as Control Flow statement.
We can print "Hello World" without using a single semicolon in the complete program. This is done with the help of if statement.
Isn't that exciting..? It is possible because printf statement evaluates to True and hence the expression is executed.
From the above example we also learn that, if or else block could be empty and it is not necessary to add statements in if or else block.
Example of if-else statement in C
Program to check whether a given number is even or odd.
We provided 4 as the input number, since 4 is an even number so the condition of if statement evaluates to true and hence the if block code is executed and we get the below output.
Program to check whether a person is eligible to vote or not
We input 18 as the age which is equal to 18, hence the if condition evaluates to true and we get "You are eligible to vote" as output. Input
Advantages and Disadvantages of If else statement in C
- if-else statement helps us to make decision in programming and execute the right code.
- It also helps in debugging of code.
- if-else statements increase the number of code paths to be tested.
- If there are a lot of if statements the code sometimes becomes unreadable and complex, in such cases we use Switch case statement.
- if-else statement is used for decision making in programming.
- If the given condition is true, then the code inside if block is executed, otherwise else block code is executed.
- Since if-else statement control the flow of the program, it is also called as Control Flow statement.