C++ Decision Making
While coding you have a number of situations when you may have to change the order of execution based on some conditions. This involves a kind of decision making to see whether a particular condition has occured or not and then direct to the program to execute certain statements accordingly. C++ language includes the decision making condition by the following statements:
- if
- if...else
- switch
- Conditional operator
The if Statement
The syntax of simple if statement is given below:
Syntax
if (test_expression) {
statement-block;
}
Here a simple if expression is evaluated. If the resulting value is true, the given statement-block are executed. If the expression is false, then no statement would be not executed. The simple if statement is often use for the counting purposes.
The if...else Statement
The if...else is the extension of simple if statement. The syntax is:
Syntax
if (test_expression) {
true-block statement(s);
}
else {
false-block statement(s);
}
If the expression is true then the true-block statement(s) will be executed otherwise the false-block statement(s) will be executed.
The Nested if...else Statement
When you have to take a series of decisions you can use more than oneif...else statement in nested form. The syntax is given below:
Syntax
if (test_condition_1) {
if (test_condition_2) {
statement_1;
}
else {
statement_2;
}
}
else {
statement_3;
}
If the test_condition_1 is false then the statement_3 will be executed and if it is true then it goes for the test_condition_2. If the test_condition_2 is true the statement_1 will be executed otherwise the statement_2 will be executed.
The switch Statement
The switch statement is used when there are one of the many alternatives for the if...else statement. The complexity of a program increases dramatically when the number of possibilities increases. The switch statement tests the value of a given variable against a list of case values. When the match is found a block of statements associated with that case is evaluated. The syntax of switch statement is given below:
Syntax
switch (expression) {
case value 1:
block 1
break;
case value 2:
block 2
break;
..........
..........
default:
default_block
break;
}
The expression is an integer expression and value1, value2... are constant expressions. The block 1, block 2... are statement lists. When the switch statement is executed the expression is compared with the values(value1, value2,... ). If a case value is matches with the value of the expression then the associated block statements are executed. The break statement at the end of each block specifies the end of the particular case and exit from the switch statement. The default is an optional case. If the value of the expression does not match with any case values then the default case will be executed.
The Conditional Operator Statement
The conditional operator is a combination of ? and : and takes three operand. It is useful for making two way decisions. It can be use instead of if...else. The general form is given below:
Syntax
conditional expression ? expression1 : expression2
The conditional expression is first evaluated. If it is true the expression1 will be executed otherwise expression2 will be executed. It is better to use if...else when more than a single nesting of conditional operator is required.