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.
Syntax of if statement:
if
(
condition
) {
//
}
Example of if statement in C++:
#include
<
stdio.h
>
Copy Code
using namespace std;
int
main
() {
int
number;
cout << "
Enter a number:
";
cin >> number;
if
(number > 0) {
cout << "
The number is positive.
" << endl;
}
return
0
;
}
In this
C++
example, the program checks if a
user-inputted
number is positive. If the number is greater than
0
, it outputs "The number is positive." The if statement allows the program to
conditionally
execute this message based on the user's input.
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 else statement
if
(
condition
) {
//
}
else
{
//
}
Example of else statement in C:
#include
<
stdio.h
>
Copy Code
using namespace std;
int
main
() {
int
number;
cout << "
Enter a number:
";
cin >> number;
if
(number > 0) {
cout << "
The number is positive.
" << endl;
}
else
{
cout << "
The number is non-positive (zero or negative).
" << endl;
}
return
0
;
}
In above C++ example, the program checks if a
user-inputted
number is positive. If the number is greater than 0, it outputs "The number is positive." Otherwise, it outputs "The number is
non-positive
(zero or negative)" using an
if-else
statement. This
illustrates
how the program takes different actions based on whether a condition is true or false.
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
if
(
condition1
) {
//
}
else if
(
condition2
) {
//
}
else if
(
condition3
) {
//
}
else
{
//
}
Example of if...else ladder statement
#include
<
stdio.h
>
Copy Code
using namespace std;
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
if
(
condition1
) {
//
if
(
condition2
) {
//
}
else
{
//
}
}
else
{
//
}
Example of Nested if...else statement
#include
<
stdio.h
>
Copy Code
using namespace std;
int
main
() {
int
age;
cout << "
Enter your age:
";
cin >>
age
;
if
(
age >= 18
) {
cout << "
You are eligible to vote.
" << endl;
if
(age >= 60) {
cout <<"
You are also eligible for senior citizen benefits.
"<<endl;
}
else
{
cout << "
You are not eligible for senior citizen benefits.
"<<endl;
}
}
else
{
cout << "
You are not eligible to vote.
" <<endl;
}
return
0
;
}