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

  1. if (test_expression) {
  2. statement-block;
  3. }

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

  1. if (test_expression) {
  2. true-block statement(s);
  3. }
  4. else {
  5. false-block statement(s);
  6. }

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

  1. if (test_condition_1) {
  2. if (test_condition_2) {
  3. statement_1;
  4. }
  5. else {
  6. statement_2;
  7. }
  8. }
  9. else {
  10. statement_3;
  11. }

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

  1. switch (expression) {
  2. case value 1:
  3. block 1
  4. break;
  5. case value 2:
  6. block 2
  7. break;
  8. ..........
  9. ..........
  10. default:
  11. default_block
  12. break;
  13. }

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

  1. 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.