If
is a control statement used to make decisions in your program. It is part of the conditional statements that allow you to execute different blocks of code based on whether a certain condition is true or false.
In real life, the "if" statement in C can be likened to decision-making scenarios, like
If it's a sunny day, then you might decide to wear shorts and a t-shirt.
If it's a rainy day, then you might choose to wear a raincoat and carry an umbrella.
The "if" statement in C is like making choices based on specific conditions. For example, just as you decide whether to wear a jacket if it's cold outside, or wear sunglasses if it's sunny, in C, you use "if" to make the computer perform different actions based on whether a condition is true or false. It's a way to tell the computer what to do in different situations.
Syntax of if statement
Here's the syntax of the "if" statement in C:
if
(
condition
) {
//
}
Example of if statement in C:
#include
<
stdio.h
>
Copy Code
int
main() {
int
x =
5
;
if
(x
3
) {
printf
(
"x is greater than 3
"
);
}
return
0
;
}
else:
The "else" statement is like a backup plan in an "if" statement. When you use "if," you specify what to do when a condition is met. But sometimes, you also want to specify what to do when that condition isn't met. That's where "else" comes in. It's like saying, "If this is true, do this; otherwise, do something else." It allows your program to make two different choices based on whether a condition is true or false.
Syntax of if...else statement
Here's the syntax of the "else" statement in C:
if
(
condition
) {
//
}
else
{
//
}
Example of else statement in C:
#include
<
stdio.h
>
Copy Code
int
main() {
int
x =
5
;
if
(x
10
) {
printf
(
"x is greater than 10
"
);
}
else
{
printf
(
"x is not greater than 10
"
);
}
return
0
;
}
Ladder in if...else:
In C, an "if...else" ladder is like a series of checkpoints you encounter on a path. You have a list of conditions, and you check each one in order. As soon as you find a condition that's true, you take a specific action associated with that condition and then stop checking the rest. It's like finding the first open door in a hallway of many doors. If none of the conditions are met, you have a default action to take, just like a "no entry" sign at the end of the hallway.
Here's the syntax of the "if...else ladder" statement in C:
if
(
condition1
) {
//
}
else if
(
condition2
) {
//
}
else if
(
condition3
) {
//
}
else
{
//
}
Example of if...else ladder statement in C:
#include
<
stdio.h
>
Copy Code
int
main() {
int
score =
85
;
if
(score
>=
90
) {
printf
(
"A grade
"
);
}
else if
(score
>=
80
) {
printf
(
"B grade
"
);
}
else if
(score
>=
70
) {
printf
(
"C grade
"
);
}
else if
(score
>=
60
) {
printf
(
"D grade
"
);
}
else
{
printf
(
"F grade
"
);
}
return
0
;
}
Nested if...else:
Nested "if...else" like making decisions within decisions. It's like having a box inside another box. When you open the outer box (the first "if" or "else"), you may find another box (another "if...else") inside. This lets you make decisions based on conditions within conditions. It's like solving a puzzle where each decision depends on the previous one, allowing you to create more complex choices in your program.
Here's the syntax of the "Nested if...else " statement in C:
if
(
condition1
) {
//
if
(
condition2
) {
//
}
else
{
//
}
}
else
{
//
}
Example of Nested if...else statement in C:
#include
<
stdio.h
>
Copy Code
int
main() {
int
x =
5
;
int
y =
10
;
if
(x
>
0
) {
printf
(
"x is positive.
"
);
if
(y
>
0
) {
printf
(
"y is also positive.
"
);
}
else
{
printf
(
"y is not positive.
"
);
}
}
else
{
printf
(
"x is not positive.
"
);
}
return
0
;
}