Conditional statements are fundamental constructs in programming that allow you to execute certain blocks of code based on specific conditions. They are essential for controlling the flow of execution in a program. Here’s a detailed overview of conditional statements in C++.
if StatementThe if statement evaluates a condition and executes a block of code if the condition is true.
Syntax:
if (condition) {
// Code to execute if condition is true
}
Example:
#include <iostream>
using namespace std;
int main() {
int age;
cout << "Enter your age: ";
cin >> age;
if (age >= 18) {
cout << "You are an adult." << endl;
}
return 0;
}
if-else StatementThe if-else statement allows for an alternative block of code to be executed 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 main() {
int age;
cout << "Enter your age: ";
cin >> age;
if (age >= 18) {
cout << "You are an adult." << endl;
} else {
cout << "You are not an adult." << endl;
}
return 0;
}
else if StatementThe else if statement allows for multiple conditions to be checked in sequence.
Syntax:
if (condition1) {
// Code for condition1
} else if (condition2) {
// Code for condition2
} else {
// Code if neither condition is true
}
Example:
int main() {
int score;
cout << "Enter your score: ";
cin >> score;
if (score >= 90) {
cout << "Grade: A" << endl;
} else if (score >= 80) {
cout << "Grade: B" << endl;
} else if (score >= 70) {
cout << "Grade: C" << endl;
} else {
cout << "Grade: D" << endl;
}
return 0;
}
switch StatementThe switch statement provides a way to execute one block of code from multiple options based on the value of a variable.
Syntax:
switch (expression) {
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
default:
// Code to execute if no case matches
}
Example:
int main() {
int day;
cout << "Enter day number (1-7): ";
cin >> day;
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;
case 6:
cout << "Saturday" << endl;
break;
case 7:
cout << "Sunday" << endl;
break;
default:
cout << "Invalid day!" << endl;
}
return 0;
}
The execution flow of a program is determined by the sequence of statements executed. Conditional statements alter this flow by allowing the program to choose different paths based on conditions.
Flow Control:
if statement evaluates to true, the corresponding block of code is executed, and control moves to the next statement after the block.else block if it exists, or to the next else if statement, if applicable.switch statement, control jumps to the case that matches the value of the expression, executing all code in that case until a break statement is encountered or the switch ends.Conditional statements are vital for controlling the execution flow of a program. By using if, else if, else, and switch statements, you can make decisions in your code and execute different actions based on varying conditions. This allows for more complex and dynamic programming, enabling programs to respond to user input and other conditions effectively.
Open this section to load past papers