Decision-making is a fundamental aspect of programming, allowing programs to execute different code paths based on certain conditions. In C++, decision-making is implemented using constructs like if, else, and switch.
The if
statement is used to execute a block of code if a specified condition is true. It can be followed by an optional else
statement to execute a block of code if the condition is false.
#include
int main() {
int x = 10;
if (x > 5) {
std::cout << "x is greater than 5" << std::endl;
} else {
std::cout << "x is not greater than 5" << std::endl;
}
return 0;
}
// output //
x is greater than 5
x > 5
is evaluated.x
is 10, which is greater than 5, the code block inside the if
statement is executed.The else-if
statement allows for multiple conditions to be checked sequentially after an initial if
statement.
#include
int main() {
int x = 10;
if (x > 10) {
std::cout << "x is greater than 10" << std::endl;
} else if (x < 10) {
std::cout << "x is less than 10" << std::endl;
} else {
std::cout << "x is equal to 10" << std::endl;
}
return 0;
}
// output //
x is equal to 10
x > 10
is false, so the program checks the next condition.x < 10
is also false, so the program executes the code block inside the else
statement.You can nest if
statements within other if
or else
statements to create more complex decision-making structures.
#include
int main() {
int x = 10;
if (x >= 0) {
if (x % 2 == 0) {
std::cout << "x is a positive even number" << std::endl;
} else {
std::cout << "x is a positive odd number" << std::endl;
}
} else {
std::cout << "x is negative" << std::endl;
}
return 0;
}
// output //
x is a positive even number
if
statement checks if x
is non-negative.x
is non-negative, the inner if
statement checks if it is even or odd.The else-if ladder allows for multiple conditions to be checked sequentially after an initial if
statement. It provides a structured way to handle multiple possible outcomes based on different conditions.
#include
int main() {
int x = 10;
if (x > 10) {
std::cout << "x is greater than 10" << std::endl;
} else if (x < 10) {
std::cout << "x is less than 10" << std::endl;
} else {
std::cout << "x is equal to 10" << std::endl;
}
return 0;
}
// output //
x is equal to 10
x > 10
is false, so the program checks the next condition.x < 10
is also false, so the program executes the code block inside the else
statement.The switch
statement in C++ provides a convenient way to perform multi-way branching based on the value of an expression. It allows you to execute different code blocks depending on the value of a variable or an expression.
switch (expression) {
case value1:
// code block for value1
break;
case value2:
// code block for value2
break;
// more case statements
default:
// default code block
}
expression
: A variable or an expression whose value is to be compared with the values specified in the case
labels.value1
, value2
, etc.: Constants or integer values against which the expression
is compared.case
: Label specifying a particular value to match.default
: Optional label specifying the code block to execute if no case
label matches the expression
.break
: Keyword used to exit the switch
statement after executing a code block.
#include
int main() {
int choice;
std::cout << "Enter a number (1-3): ";
std::cin >> choice;
switch (choice) {
case 1:
std::cout << "You chose option 1" << std::endl;
break;
case 2:
std::cout << "You chose option 2" << std::endl;
break;
case 3:
std::cout << "You chose option 3" << std::endl;
break;
default:
std::cout << "Invalid choice" << std::endl;
}
return 0;
}
// output //
Enter a number (1-3): 2
You chose option 2
choice
variable.switch
statement evaluates the value of choice
.choice
matches any of the case
labels (1, 2, or 3), the corresponding code block is executed.case
label matches the value of choice
, the code block under the default
label is executed.break
statement is used to exit the switch
statement after executing the corresponding code block, preventing fall-through to subsequent case
labels.switch
statement provides a concise and readable way to handle multiple possible outcomes based on the value of an expression.case
label must be followed by a break
statement to prevent fall-through to subsequent case
labels.Decision-making constructs such as if, else, and switch are essential tools in C++ programming for controlling the flow of execution based on conditions. By understanding how to use these constructs effectively, you can write more versatile and dynamic programs to meet various requirements.Happy coding! ❤️