Coditional Statement
Conditional Statements
Conditional statements allow a program to make decisions based on certain conditions. They help control the flow of execution by executing different blocks of code depending on whether a condition is true or false.
Video Explanationâ

Types of Conditional Statementsâ
1. if Statementâ
The if statement executes a block of code if the specified condition evaluates to true.
Syntax:
if (condition) {
// Code to execute if condition is true
}
Example:
int age = 18;
if (age >= 18) {
cout << "You are eligible to vote." << endl;
}
2. if-else Statementâ
The if-else statement provides an alternative block of code to execute if the condition is false.
Syntax:
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
Example:
int age = 16;
if (age >= 18) {
cout << "You are eligible to vote." << endl;
} else {
cout << "You are not eligible to vote." << endl;
}
3. if-else if-else Ladderâ
This structure allows you to test multiple conditions sequentially. The first true condition will be executed.
Syntax:
if (condition1) {
// Code for condition1
} else if (condition2) {
// Code for condition2
} else {
// Code if none of the conditions are true
}
Example:
int marks = 75;
if (marks >= 90) {
cout << "Grade: A" << endl;
} else if (marks >= 75) {
cout << "Grade: B" << endl;
} else {
cout << "Grade: C" << endl;
}
4. Nested if Statementâ
An if statement inside another if statement. This is useful for checking multiple related conditions.
Syntax:
if (condition1) {
if (condition2) {
// Code if both conditions are true
}
}
Example:
int age = 20;
bool hasID = true;
if (age >= 18) {
if (hasID) {
cout << "You are allowed entry." << endl;
}
}
5. switch Statementâ
The switch statement is used when you have multiple conditions based on a single variable.
Syntax:
switch (expression) {
case value1:
// Code for value1
break;
case value2:
// Code for value2
break;
default:
// Code if no case matches
}
Example:
int day = 3;
switch (day) {
case 1:
cout << "Monday" << endl;
break;
case 2:
cout << "Tuesday" << endl;
break;
case 3:
cout << "Wednesday" << endl;
break;
default:
cout << "Invalid day" << endl;
}
When to Use Each Conditional Statementâ
- if Statement: When you need to check a single condition.
- if-else Statement: When there are two possibilities (true/false).
- if-else if-else Ladder: When there are multiple conditions to evaluate sequentially.
- Nested if: When conditions depend on each other.
- switch Statement: When you have multiple values for a single variable.
Conclusionâ
Conditional statements are essential for controlling the flow of a program. Understanding when and how to use each type of conditional statement is crucial for writing efficient and logical code.
Done with this topic? Mark it as complete to track your progress.
đŦ Discuss this page
Have a question or spot something confusing in "Coditional Statement"? Ask below â it's backed by GitHub Discussions, so maintainers get notified like any other GitHub activity.