If, If Else
If, If Else
I'll
explain each type of control structure and provide easy-to-understand examples.
Control structures allow us to control the flow of our program by making decisions. In other
words, we can tell the computer, “If something happens, do this; otherwise, do that.” This is very
useful when our program needs to behave differently based on certain conditions.
1. if
2. if-else
3. else if
4. if-else-if
5. switch
1. if Statement
The if statement checks if a condition is true. If it's true, the code inside the if block runs.
Syntax:
cpp
Copy code
if (condition) {
// Code that runs if condition is true
}
Example:
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
int age = 18;
return 0;
}
Explanation:
Here, we check if age is greater than or equal to 18. If it is, the message "You are eligible
to vote" is printed.
2. if-else Statement
The if-else statement provides an alternative. If the condition is true, the if block runs; if it’s
false, the else block runs.
Syntax:
cpp
Copy code
if (condition) {
// Code that runs if condition is true
} else {
// Code that runs if condition is false
}
Example:
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
int age = 16;
return 0;
}
Explanation:
Here, age is 16, so the condition age >= 18 is false, and the message "You are not
eligible to vote" is printed.
3. else if Statement
The else if statement allows us to check multiple conditions. If the first condition is false, the
next else if condition is checked, and so on.
Syntax:
cpp
Copy code
if (condition1) {
// Code for condition1
} else if (condition2) {
// Code for condition2
}
Example:
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
int marks = 85;
return 0;
}
Explanation:
This example checks the student's marks. If marks are 85, it matches the second condition
marks >= 75, so "Grade: B" is printed.
4. if-else-if Ladder
The if-else-if ladder is a combination of multiple else if statements, allowing us to check
many conditions one after another. Only one of these conditions will be true, and the
corresponding block of code will run.
Syntax:
cpp
Copy code
if (condition1) {
// Code if condition1 is true
} else if (condition2) {
// Code if condition2 is true
} else if (condition3) {
// Code if condition3 is true
} else {
// Code if no conditions are true
}
Example:
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
int temperature = 30;
return 0;
}
Explanation:
This example checks the temperature and gives different messages depending on the
value. Since the temperature is 30, the message "It's warm." is printed.
5. switch Statement
The switch statement is used when you want to compare a single value against many fixed
cases. It's cleaner when you're checking one variable against multiple possible values.
Syntax:
cpp
Copy code
switch (variable) {
case constant1:
// Code for constant1
break;
case constant2:
// Code for constant2
break;
// More cases
default:
// Code if no cases match
}
Example:
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
int day = 3;
switch (day) {
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
case 4:
cout << "Thursday" << endl;
break;
case 5:
cout << "Friday" << endl;
break;
default:
cout << "Weekend" << endl;
}
return 0;
}
Explanation:
Here, the variable day is checked against different cases (1, 2, 3, etc.). Since day is 3, the
message "Wednesday" is printed.
Summary of Control Structures:
1. Write a program that asks the user to enter their marks and prints their grade based on the
following rules:
o Marks >= 90: Grade A
o Marks >= 75: Grade B
o Marks >= 50: Grade C
o Marks >= 40: Grade D
o Marks < 40: Grade F
2. Write a program that takes an input number (1 to 7) and prints the day of the week using
a switch statement (e.g., 1 = Monday, 2 = Tuesday, etc.).
1. if Statement
The if statement checks if a condition is true, and if it is, it executes a block of code.
Syntax:
cpp
Copy code
if (condition) {
// Code to execute if the condition is true
}
Example:
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
int number = 10;
if (number > 5) {
cout << "The number is greater than 5!" << endl;
}
return 0;
}
In this example, since number is 10 (which is greater than 5), the condition is true, and the
message is printed.
2. if-else Statement
The if-else statement lets you specify an alternative block of code to execute if the condition is
false.
Syntax:
cpp
Copy code
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
Example:
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
int number = 3;
if (number > 5) {
cout << "The number is greater than 5!" << endl;
} else {
cout << "The number is less than or equal to 5!" << endl;
}
return 0;
}
In this example, since number is 3 (which is less than 5), the else block is executed, and the
second message is printed.
3. else if Statement
The else if statement lets you check multiple conditions one after the other. If the first
condition is false, it checks the next one.
Syntax:
cpp
Copy code
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if none of the conditions are true
}
Example:
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
int number = 7;
return 0;
}
In this example, number is 7, so the else if condition number > 5 is true, and the second
message is printed.
4. if-else-if Ladder
An if-else-if ladder is useful when you have multiple conditions to check in sequence.
Syntax:
cpp
Copy code
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else if (condition3) {
// Code to execute if condition3 is true
} else {
// Code to execute if none of the conditions are true
}
Example:
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
int marks = 75;
return 0;
}
In this example, since marks is 75, the second condition (marks >= 75) is true, and "Grade: B"
is printed.
5. switch Statement
The switch statement is used when you want to check a variable against different constant
values. It's a cleaner alternative to using multiple else if statements when you're comparing the
same variable.
Syntax:
cpp
Copy code
switch (expression) {
case constant1:
// Code to execute if expression equals constant1
break;
case constant2:
// Code to execute if expression equals constant2
break;
// You can add more cases here
default:
// Code to execute if no cases match
}
Example:
cpp
Copy code
#include <iostream>
using namespace std;
int main() {
int day = 3;
switch (day) {
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
case 4:
cout << "Thursday" << endl;
break;
case 5:
cout << "Friday" << endl;
break;
default:
cout << "Weekend!" << endl;
}
return 0;
}
In this example, day is 3, so the case 3 block is executed, and "Wednesday" is printed.
Summary:
These control structures are very useful for making decisions in your programs. The if
statements are versatile and flexible, while switch is good for specific, fixed choices.