0% found this document useful (0 votes)
25 views15 pages

Types of If Statement

The document discusses nested if-else statements in C programming. Nested if-else statements allow for multiple conditions to be checked within other if-else blocks, enabling more complex decision making than a basic if-else statement. An example is provided to check if a number is even or odd, and if even further check if it is divisible by 4, demonstrating the use of nested conditions.

Uploaded by

vipularunpatil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views15 pages

Types of If Statement

The document discusses nested if-else statements in C programming. Nested if-else statements allow for multiple conditions to be checked within other if-else blocks, enabling more complex decision making than a basic if-else statement. An example is provided to check if a number is even or odd, and if even further check if it is divisible by 4, demonstrating the use of nested conditions.

Uploaded by

vipularunpatil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Tatyasaheb Kore Institute of Engineering & Technology, Warananagar

(An Autonomous Institute)


F.Y.B.Tech. Semester 1
Course Code: FY109 Course Name: Computer Programming in C

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.

3.1 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.
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(1) or false(0), it shifts the control to a particular block of
statement.
If a certain condition is true then it will execute a block of the statement below it otherwise
not.

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.

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 colours of light which indicate different decisions to be made according to the colour
of the light. If the colour 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.

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:

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

Example of if-else statement in C


Program to check whether a given number is even or odd.
#include <stdio.h>
#include <stdlib.h>

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

Advantages and Disadvantages of If else statement in C


Advantages:

 if-else statement helps us to make decision in programming and execute the right code.
 It also helps in the debugging of code.

Disadvantages:

 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.

Conclusion

 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 the if-else statement controls the flow of the program, it is also called as Control
Flow statement.

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.

What is Nested If Else in C


We already saw how useful if and else statements are, but what if we need to check for more
conditions even when one condition is satisfied?

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.

The syntax for Nested If Else Statement in C will be :


//check if the first condition holds
if (condition 1)
{

//if the second condition holds


if (condition 2)
{
do something
}
//if the second condition does not hold
else
{
do something else
}

}
// if the first condition does not hold
else
{

//if the third condition holds


if (condition 3)
{
do something
}
//if the third condition does not hold
else
{
do something 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()
{

// variable to store the given number


int n;

//take input from the user


scanf("%d", &n);

//if else condition to check whether the number is even or odd


if (n % 2 == 0)
{

//the number is even


printf("Even ");

//nested if else condition to check if n is divisible by 4 or not


if (n % 4 == 0)
{
//the number is divisible by 4
printf("and divisible by 4");
}
else
{
//the number is not divisible by 4
printf("and not divisible by 4");
}
}
else
{
//the number is odd
printf("Odd ");

//nested if else condition to check if n is divisible by 3 or not


if (n % 3 == 0)
{
//the number is divisible by 3
printf("and divisible by 3");
}
else
{
//the number is not divisible by 3
printf("and not divisible by 3");
}

return 0;
}

Input

14

Output

Even and not divisible by 4

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.

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

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

 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.
 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.

You might also like