C++ Flow Control if else switch case syntax – steal our code

Spread the love

There are a number of flow control syntax constructs in C++. They are basically the same as those in C, so if you are a C programmer you can skip this section.

The c++ flow control constructs involve variations of :

  • if,
  • if else if syntax,
  • while(isTrue) …,
  • do … while(isTrue) and my favorite …
  • c++ switch case syntax …
  • with variations of continue, break and default


c++ Flow Control Code Example

#include <iostream>
 
int main(){
    bool isGreatWeather = true;
    if( isGreatWeather)
        std::cout << "We go to swim today" << std::endl;

Line 5, you can see the first flow control statement

If statement“. If isGreatWeather is true then we execute the std::cout << “We go to swim today” << std::endl;


C++ Flow Control if else

// Flow control constructs for C++
#include <iostream>

int main(){
    bool isGreatWeather = true;
    if( isGreatWeather)
        std::cout << "We go to swim today" << std::endl;

    if( isGreatWeather){
        std::cout << "Great Weather we go to swim today" << std::endl;
    } else { 
        std::cout << "We can't possibly swim today\n"; 
    }
}

Line 9 and 11 show the power of if then else but there is no then in the code structure.

In c and c++ the structure is if ( isTrue ){ do this part} else { do this then }


C++ Flow Control If (isTrue)… else if … else …

// Flow control constructs for C++
#include <iostream>
 
int main(){
    bool isGreatWeather = true;
    if( isGreatWeather)
        std::cout << "We go to swim today" << std::endl;
 
    if( isGreatWeather){
        std::cout << "Great Weather we go to swim today" << std::endl;
    } else { 
        std::cout << "We can't possibly swim today\n"; 
    }
 
    bool isFastSwimmer = false;
    bool isSlowRace = true;
 
    if(isGreatWeather &amp;&amp; isSlowRace){
        std::cout << "Slow race n great weather - we go 2 win today" << std::endl;
    } else if(  isSlowRace &amp;&amp; isFastSwimmer ) { 
        std::cout << "We should try to win today\n"; 
    } else { 
        std::cout << "Why bother try?" ; 
    }
}

Lines 18, 20 and 22 highlight the if else if else structure.

It sounds complex but it’s not really

If isGreatWeather and isSlowRace is also true, then maybe we should go and try to win.
Else if isSlowRace and we are fast swimmer, maybe we should try to win today. Else Why Bother ?


Switch Case Statement – Use this instead of half a dozen if else’s

The code below is a bit more complicated but switch case statements usually are. In fact, the reason we use a switch case is not because the If else if… else if… else if… can’t grow to accommodate it, its because any more than 2 sets of else if… else if can be a bit hairy to debug

Line 4 below has 8 days of increasing temperatures, starting with 56 degrees. The array temp[8] is holding the information in 8 slots.

Line 5 start a for loop in which the switch will investigate 8 times to see if the temperatures match the cases which are conveniently set at one of the 8 temperature settings.


#include <iostream>
 
int main(){
    int temp[8] = {56,64,78,79,83,90,94,99};
    for(int i=0; i < 8 ; i++){
        switch (temp[i]){
            case 56:
               std::cout << "Is this cold weather " << temp[i] << std::endl;
               break;
           case 64:
               std::cout << "Is this still cold weather " << temp[i] << std::endl;       
               break;
           case 78:
               std::cout << "Is this a bit warmer weather yet " << temp[i] << std::endl;
               break;
            case 79:
            std::cout << "Is this cold weather " << temp[i] << std::endl;
               break;            
           case 83:
               std::cout << "Is this still cold weather " << temp[i] << std::endl;       
               break;               
           case 90:
               std::cout << "Is this a bit warmer weather yet " << temp[i] << std::endl;
               break;                        
           default:
               std::cout << "It must be above 90 degrees " << temp[i] << std::endl;
               break;               
        }
    }
 
}

The code above may have a bug in it. Copy it and run it in your own compiler editor to see whether it does or not. If it does not, we suggest that you comment out one or two of the break; statement(s) just to see what will happen and report in the comment section below


Sample c++ Flow Control Code ( with a problem )

// Flow control constructs for C++
#include <iostream>

int main(){
    bool isGreatWeather = true;
    if( isGreatWeather)
        std::cout << "We go to swim today" << std::endl;

    if( isGreatWeather){
        std::cout << "Great Weather we go to swim today" << std::endl;
    } else { 
        std::cout << "We can't possibly swim today\n"; 
    }

    bool isFastSwimmer = false;
    bool isSlowRace = true;

    if(isGreatWeather &amp;&amp; isSlowRace){
        std::cout << "Slow race n great weather - we go 2 win today" << std::endl;
    } else if(  isSlowRace &amp;&amp; isFastSwimmer ) { 
        std::cout << "We should try to win today\n"; 
    } else { 
        std::cout << "Why bother try?" ; 
    }


    int temp[8] = {56,64,78,79,83,90,94,99};

    for(int i=0; i < 8 ; i++){
        switch (temp[i]){
            case 56:
               std::cout << "Is this cold weather " << temp[i] << std::endl;
           case 64:
               std::cout << "Is this still cold weather " << temp[i] << std::endl;       
           case 78:
               std::cout << "Is this a bit warmer weather yet " << temp[i] << std::endl;
            case 79:
            std::cout << "Is this cold weather " << temp[i] << std::endl;
           case 83:
               std::cout << "Is this still cold weather " << temp[i] << std::endl;       
           case 90:
               std::cout << "Is this a bit warmer weather yet " << temp[i] << std::endl;
               
           default:
               std::cout << "It must be above 90 degrees " << temp[i] << std::endl;
        }
    }

}

Take a few moments and have a real good look at the code immediately above. There may be a problem with it.

Have you figured out what that problem is yet?

C++ Flow Control. Have you figured it out yet? Look at the 56 Degree temperature. Why is it repeating?


-> Lesson 7 C++ While Loop Example


Top