C++ Conditional Statement


Conditional statements in C++ are used to control the flow of a program based on certain conditions. They allow the program to make decisions and execute specific blocks of code depending on whether a given condition is true or false. The main types of conditional statements in C++ are:

1. if Statement

The if statement is used to execute a block of code only if a specified condition evaluates to true.

Syntax:

if (condition) { // Code to execute if condition is true }

Example:

int num = 10; if (num > 5) { std::cout << "Number is greater than 5" << std::endl; // Output: Number is greater than 5 }

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 num = 3; if (num > 5) { std::cout << "Number is greater than 5" << std::endl; } else { std::cout << "Number is 5 or less" << std::endl; // Output: Number is 5 or less }

3. if-else if-else Statement

The if-else if-else statement is used when there are multiple conditions to check. It allows for more complex decision-making by evaluating several conditions in sequence.

Syntax:

if (condition1) { // Code to execute if condition1 is true } else if (condition2) { // Code to execute if condition1 is false and condition2 is true } else { // Code to execute if none of the conditions are true }

Example:

int num = 7; if (num > 10) { std::cout << "Number is greater than 10" << std::endl; } else if (num > 5) { std::cout << "Number is greater than 5 but 10 or less" << std::endl; // Output: Number is greater than 5 but 10 or less } else { std::cout << "Number is 5 or less" << std::endl; }

4. switch Statement

The switch statement is used when you need to choose between several options. It compares the value of an expression to multiple possible case labels and executes the corresponding block of code.

Syntax:

switch (expression) { case value1: // Code to execute if expression == value1 break; case value2: // Code to execute if expression == value2 break; // More cases... default: // Code to execute if none of the cases match }

Example:

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; // Output: Wednesday break; default: std::cout << "Invalid day" << std::endl; }
  • The break statement is used to exit the switch block. Without break, the program continues executing the following cases, which is called "fall-through."
  • The default block executes if none of the case values match the expression. It is similar to the else block in an if-else statement.

5. Nested Conditional Statements

Conditional statements can also be nested, meaning you can have an if, else, or switch statement inside another conditional statement. This is used to create more complex decision-making logic.

Example:

int num = 10; if (num > 5) { if (num < 15) { std::cout << "Number is between 5 and 15" << std::endl; // Output: Number is between 5 and 15 } else { std::cout << "Number is 15 or greater" << std::endl; } } else { std::cout << "Number is 5 or less" << std::endl; }

Summary

  • if Statement: Executes code if the condition is true.
  • if-else Statement: Executes one block if the condition is true, otherwise executes another block.
  • if-else if-else Statement: Evaluates multiple conditions in sequence.
  • switch Statement: Executes code based on the value of an expression, suitable for multiple discrete options.
  • Nested Conditionals: Allows for more complex decision-making by placing conditional statements inside each other.

These conditional statements help control the flow of the program, enabling decisions and branching logic depending on the data and conditions at runtime.