Conditional and Iterative Statements

Other Conditional and Iterative Statements

The if/else and while statements are sufficient to implement any algorithms that involve conditional execution and looping. The break and continue statements are convenient but are not necessary. C++ provides some additional conditional and iterative statements that are more convenient to use in some circumstances. These additional statements include

  • switch: an alternative to some multi-way if/else statements
  • the conditional operator: an expression that exhibits the behavior of an if/else statement
  • do/while: a loop that checks its condition after its body is executed
  • for: a loop convenient for counting

The switch Statement

The switch statement provides a convenient alternative for some multi-way if/else statements like the one in a new implementation of that uses a switch statement instead of a multi-way if/else statement.

Flow of Switch Statemant

flow of switch statement in c++ AndroWep-Tutorials
flow of switch statement in c++ AndroWep-Tutorials

Code Example

#include <iostream>

int main() {
    int value;
    std::cout << "Please enter an integer in the range 0...5: ";
    std::cin >> value;
    switch (value) {
    case 0:
        std::cout << "zero";
        break;
    case 1:
        std::cout << "one";
        break;
    case 2:
        std::cout << "two";
        break;
    case 3:
        std::cout << "three";
        break;
    case 4:
        std::cout << "four";
        break;
    case 5:
        std::cout << "five";
        break;
    default:
        if (value < 0)
            std::cout << "Too small";
        else
            std::cout << "Too large";
        break;
    }
    std::cout << '\n';
}

The corresponding switch statement is:

switch (x) {
case 1:
    // Do 1 stuff here . . .
    break;
case 2:
    // Do 2 stuff here . . .
    break;
case 3:
    // Do 3 stuff here . . .
    break;
}

Now consider the following if/else:

if (x == y) {
    // Do "y" stuff here . . .
} else if (x > 2) {
    // Do "> 2" stuff here . . .
} else if (z == 3) {
    // Do 3 stuff here . . .
}

The Conditional Operator

As purely a syntactical convenience, C++ provides an alternative to the if/else construct called the conditional operator.

  • the result of y/z, if z is nonzero, or
  • zero, if z is zero; we wish to avoid the run-time error of division by zero.
// Assign a value to x:
if (z != 0)
x = y/z; // Division is possible
else
x = 0;

The conditional operator makes for a more compact statement:

// Assign a value to x:
x = (z != 0) ? y/z : 0;

The general form of a conditional expression is:

conditional opearator in cpp language
conditional opearator in cpp language

What is conditional operator with example?

An Example of Conditional Operators

The conditional operator “&&” first evaluates whether its first operand (i.e., number % 2 == 0) is true and then evaluates whether its second operand (i.e., number % 4 == 0) is true. As both are true, the logical AND condition is true.

How does a conditional operator work?

The conditional operator (? ūüôā is a ternary operator (it takes three operands). The conditional operator works as follows: … If the first operand evaluates to true (1), the second operand is evaluated. If the first operand evaluates to false (0), the third operand is evaluated.