If-else Statement in C-Programming
If-else Statement in C-Programming
else Statement
Last Updated : 16 Jun, 2023
The if-else statement in C is a flow control statement used for decision-making in the C program.
It is one of the core concepts of C programming. It is an extension of the if in C that includes an else
block along with the already existing if block.
C if Statement
The -jfstatem-en-t in C is used to execute a block of code based on a specified condition.
if (condition) {
// code to be executed if the condition is true
C if-else Statement
The if-else statement is a decision-making statement that is used to decide whether the part of the
code will be executed or not based on the specified condition (test expression). If the given
condition is true, then the code inside the if block is executed, otherwise the code inside the else
block is executed.
Syntax of if-else
if (condition) {
// code executed whenthe condition is true
c
Program to demonstrate the use of if-else statement
# include
int main ()
returnO;
Output
Note: Any non-zero and non-null values are assumed to be true, and zero or null values are
assumedto be false.
How if-else Statement works?
Working of the if-else statement in C is explained below:
1. When the program control first comes to the if-else block, the test condition is checked.
2. If the test condition is true:
• The if block is executed.
4. After that, the program control continues to the statements below the if-else statement.
if (condition)
// execute code statements of if block when
// condition is true
else
We can understand the working of the if-else statement in C with the help of the flowchart.
If Cm di tio n
ElseBody
Flowchart Ofif-else in C
For a given number to be even, it should be perfectly divisible by 2. We will use the if-else
statement to check for this condition and execute different statements for when it is true and when
it is false.
c
Program to Demonstrate the working of if-else statement
# include
int main ( )
// else block
else [
// executed when the number is odd
print f ('Number is Odd");
return 0;
Output
Number is even
We know that a person is eligible to vote after he/sheis at least 18 years old. Now we use this
condition in the if-else statement to check the eligibility of the person.
c
Program to check whether the person is eligible to vote
// or not
# include
int main ( )
return 0;
Output
You may notice that in the second example, we did not enclose the body of the if and else
statement in the braces and still the code is running without error.This is because the C language
allows the skipping of the braces around the body of the if-else statement when there is only one
statement in the body.
Advantages of if-else Statement
• The if-else statement enables the user to execute different statements based on different
conditions.
• It can evaluate test expressions of type int, char, boolean, and more.
• It helps in modifying the flow of the program.
• It is simple, efficient, and easier to read when there is less number of conditions.
Conclusion
In this article, we discussed how to use the if-else statement in C for making decisions in our
program based on the specified conditions. Being the core concept of C programming, it is
frequently used in almost all C programs.
FAQs on if-else Statement in C
Answer:
We can skip the braces of the body of the if or else block as long as there is only a single
statement inside their body. We will get an error if there is more than one statement in the
body without braces.
Answer:
int main ()
if (1)
else {
return O;
Output
Theif block is executed.
3. What are the types of if-else statements in C?
Answer:
1. if Statement
2. if-else Statement
3. if-else-if Ladder
Answer:
if (test expression)
{
// if body
else {
// else body