0% found this document useful (0 votes)
3 views17 pages

If Statement

The document explains the 'if' statement in C programming, detailing its syntax, functionality, and variations such as 'if-else' and 'if-else-if' ladders. It includes examples to illustrate how these statements work and discusses the advantages and disadvantages of using 'if' statements. The content serves as a guide for understanding decision-making in C programming.

Uploaded by

mskr860631
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)
3 views17 pages

If Statement

The document explains the 'if' statement in C programming, detailing its syntax, functionality, and variations such as 'if-else' and 'if-else-if' ladders. It includes examples to illustrate how these statements work and discusses the advantages and disadvantages of using 'if' statements. The content serves as a guide for understanding decision-making in C programming.

Uploaded by

mskr860631
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/ 17

If Statement

Md Shafiuzzaman
Lecturer, Dept. of CSE, NBIU
If Statement
The if in C is a decision-making statement that is used to execute a block
of code based on the value of the given expression. It is one of the core
concepts of C programming and is used to include conditional code in our
program.

Syntax of if Statement in C:
Relational operators
if(condition) are used here
{
// if body
// Statements to execute if condition is true
}
How if in C works?
The working of the if statement in C is as
follows:

STEP 1: When the program control comes


to the if statement, the test expression is
evaluated.
STEP 2A: If the condition is true, the
statements inside the if block are executed.
STEP 2B: If the expression is false, the
statements inside the if body are not
executed.
STEP 3: Program control moves out of the if
block and the code after the if block is
executed.
Flowchart If Body
Example of If
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
if (number > 0)
{
printf("%d is a positive number.",number);
}

return 0;
}
if...else statement
An if statement can be followed by
an optional else statement, which
executes when the Boolean
expression is false.
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
if (number%2 == 0) {
printf("%d is an even integer.",number);
}
else {
printf("%d is an odd integer.",number);
}
return 0;
}
if-else-if Ladder
• The if else if statements are used when the user
has to decide among multiple options.
• The C if statements are executed from the top if (condition)
down. As soon as one of the conditions controlling statement;
the if is true, the statement associated with that if else if (condition)
is executed, and the rest of the C else-if ladder is statement;
bypassed. .
• If none of the conditions is true, then the final else .
statement will be executed. else
• if-else-if ladder is similar to the switch statement. statement;
if….
else if
Ladder
if-else-if Ladder
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number > 0) {
printf("The number is positive.\n");
}
else if (number < 0) {
printf("The number is negative.\n"); }
else {
printf("The number is zero.\n"); }
return 0;
}
Start

This is a nested concept: Condition inside


other conditions.
Nested if….else
#include <stdio.h>
A nested if in C is an if int main()
statement that is the target of {
another if statement. Nested if int i = 10;
if (i > 5) {
statements mean an if
if (i == 10)
statement inside another if printf(“Equal to 10\n");
statement. Yes, C allow us to if (i < 10)
nested if statements within if printf(“Smaller than 10 too\n");
statements, i.e, we can place else
an if statement inside another printf(“Greater than 10");
}
if statement.
return 0;
}
Nested
if….
else
Advantage & Disadvantage
Advantages of if Statement
➢ It is the simplest decision-making statement.
➢ It is easy to use and understand.
➢ It can evaluate expressions of all types such as int, char, bool, etc.

Disadvantages of if Statement
➢ It contains only a single block. In case when there are multiply related if
blocks, all the blocks will be tested even when the matching if block is found
at the start.
➢ When there are a large number of expressions, the code of the if block gets
complex and unreadable.
➢ It is slower for a large number of conditions.

You might also like