Conditional Statements (if, else, else if, switch) in C++
Conditional statements allow you to execute certain sections of code based on whether a condition is true or false. In C++, the primary conditional statements are if
, else
, else if
, and switch
. These statements enable decision-making in your programs. In this article, we will explore each of these conditional statements with examples.
1. The if Statement
The if
statement is used to execute a block of code only if the specified condition is true. If the condition evaluates to false, the block is skipped.
Example of if Statement
#include <iostream> int main() { int num = 10; if (num > 5) { std::cout << "The number is greater than 5." << std::endl; } return 0; }
In this example, the condition num > 5
is true, so the message "The number is greater than 5." is printed to the console.
2. The if-else Statement
The if-else
statement allows you to execute one block of code if the condition is true, and a different block of code if the condition is false.
Example of if-else Statement
#include <iostream> int main() { int num = 3; if (num > 5) { std::cout << "The number is greater than 5." << std::endl; } else { std::cout << "The number is not greater than 5." << std::endl; } return 0; }
In this example, the condition num > 5
is false, so the else block is executed, and the message "The number is not greater than 5." is printed to the console.
3. The else if Statement
The else if
statement is used to check multiple conditions. If the initial if
condition is false, the program checks the conditions in the else if
block(s). You can have multiple else if
statements to check several conditions in sequence.
Example of else if Statement
#include <iostream> int main() { int num = 5; if (num > 10) { std::cout << "The number is greater than 10." << std::endl; } else if (num == 5) { std::cout << "The number is 5." << std::endl; } else { std::cout << "The number is less than 5." << std::endl; } return 0; }
Here, the first condition num > 10
is false, so the program checks the else if
condition num == 5
, which is true, so it prints "The number is 5." to the console.
4. The switch Statement
The switch
statement is another type of conditional statement used to test the value of a variable or expression against multiple possible values. Unlike if-else
statements, switch
allows you to check for equality against several constants, making the code more readable when dealing with multiple conditions.
Syntax of switch Statement
switch (expression) { case value1: // Code to be executed if expression == value1 break; case value2: // Code to be executed if expression == value2 break; default: // Code to be executed if no case matches }
Example of switch Statement
#include <iostream> int main() { int day = 3; switch (day) { case 1: std::cout << "Monday" << std::endl; break; case 2: std::cout << "Tuesday" << std::endl; break; case 3: std::cout << "Wednesday" << std::endl; break; case 4: std::cout << "Thursday" << std::endl; break; case 5: std::cout << "Friday" << std::endl; break; default: std::cout << "Invalid day" << std::endl; } return 0; }
In this example, the switch
statement checks the value of day
. Since day = 3
, the program executes the code in the case 3
block, printing "Wednesday" to the console. The break
statement ensures that the program exits the switch statement after executing the matched case.
5. Conclusion
Conditional statements like if
, else
, else if
, and switch
allow you to control the flow of your C++ programs by making decisions based on conditions. The if
and else
statements handle simple conditional checks, while the switch
statement is useful when you need to check against multiple possible values. Understanding these constructs will help you write more flexible and responsive programs.