Types of If Statement
Types of If Statement
Introduction
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.
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.
Syntax:
if(condition)
{
block of the statement;
}
another block of 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:
o The ‘block of the statement’ will be executed;
o 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 number=”);
scanf(“%d”,&n);
if(n>0)
{
printf(“\n%d is positive number”,n);
}
return 0;
}
Output:
Enter number=7
7 is positive number
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.
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.
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.
3.2 Introduction
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 the 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.
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.
Syntax
if (condition or expression)
{
// statement(s) will execute if the condition or expression is true
}
else
{
// statement(s) will execute if the condition or expression is false
}
How if-else statement in C works?
If-else statement allows one 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:
int main()
{
int n;
printf("Enter a number:");
scanf("%d", &n);
if (n % 2 == 0)
{
printf("%d is even number", n);
}
else
{
printf("%d is a odd number", n);
}
return 0;
}
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.
Output
Enter a number:4
4 is even number
if-else statement helps us to make decision in programming and execute the right code.
It also helps in the debugging of code.
Disadvantages:
Conclusion
3.3 Introduction
If else statements are used for decision making by specifying which block of code is to be
executed when a certain condition is met. Nested if-else statements are just if-else statements
inside other if-else statements to provide better decision making.
For example, if we need to analyse if the number is even or odd, and then if it is even, whether
it is divisible by 4 or not, and if it is odd, whether it is divisible by 3 or not. In such a case, only
one if and else statement wouldn’t suffice.
First, we will check with one if-else statement whether the number is even or odd. Then in the
if block, that means if the number was even, we would have to include another if and else
statement checking if it is divisible by 4 or not, and similarly in the else block, we would have
to include another if and else statement checking if the number is divisible by 3 or not.
Including numerous if-else statements inside an if and else statement is called nesting. The
second if and else statements are said to be nested inside the first if and else statement.
This is why C language allows nesting of if and else statements. These are called nested if else
statements and provide clearer decision making when some extra conditions are needed to be
checked inside the initial conditions like in the previous example.
}
// if the first condition does not hold
else
{
The flowchart for nested if-else statements is shown below in the diagram.
example where we need to analyse if the number is even or odd, and then if it is even,
whether it is divisible by 4 or not, and if it is odd, whether it is divisible by 3 or not will be :
#include <stdio.h>
int main()
{
return 0;
}
Input
14
Output
in above example IF statement which evaluates if n is even. If this n is even, that means the
expression n % 2 == 0 evaluates to true, we enter the if block. Here we have our nested if
statement which evaluates if n is divisible by 4. If the expression n % 4 == 0evaluates to true,
we enter the nested if statement block. Here we print that the number is even and divisible by
4. If the expression n % 4 == 0 was evaluated to be false, we enter the nested else statement
and print that the number is even but not divisible by 4.
Similarly, if the expression n % 2 == 0 evaluates to false, we enter the first else block, skipping
the if part as the condition is false, and we check the condition of the nested if statement. If
the expression n % 3 == 0 evaluates to true, we enter the nested if statement block. Here we
print that the number is odd and divisible by 3. If the expression n % 3 == 0 was evaluated to
be false, we enter the nested else statement and print that the number is even but not
divisible by 3.
Conclusion
In programming languages, if else statements are used for decision making. They
determine the flow of code by specifying different operations in different cases.
Including numerous if-else statements inside an if and else statement is called nesting.
The second if and else statements are said to be nested inside the first if and else
statement.
When we enter the if block, the else block is ignored and if we enter the else block, the
if block is ignored. Nested if else follow the same working.
C language allows nesting of if-else statements to facilitate better decision making.
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
}
Example
Following is the C program to execute else If Ladder −
#include<stdio.h>
void main ()
{
int a,b,c,d;
printf("Enter the values of a,b,c,d: ");
scanf("%d%d%d%d",&a,&b,&c,&d);
if(a>b && a>c && a>d)
{
printf("%d is the largest",a);
}
else if(b>c && b>a && b>d)
{
printf("%d is the largest",b);
}
else if(c>d && c>a && c>b)
{
printf("%d is the largest",c);
}
else
{
printf("%d is the largest",d);
}
}
Output
You will see the following output −
Enter the values of a,b,c,d: 2 4 6 8
8 is the largest
IF statement Important Points Need to Remember