Intro to C++ Programming http://www.c-jump.com/bcc/c155c/c155syllabus.htm

C++ flow control


  1. Structured programming and C++ flow control
  2. if-else statement
  3. if-else statement example
  4. else-if construct
  5. else-if example
  6. while loop
  7. for loop
  8. for loop timeline
  9. for loop
  10. for loop example
  11. forever
  12. do while loop
  13. switch statement
  14. switch statement considerations
  15. break statement
  16. continue statement
  17. Conclusion

Structured programming and

C++ flow control


if-else statement



        if ( condition ) { 

            statement true; 

        } else { 

            statement false; 

        } 

if-else statement example



#include <iostream>
int main()
{
    int x = -1;
    
    if ( x > 0 ) { 
    
        std::cout << "x is a positive number"; 
        
    } else { 
    
        std::cout << "x is a negative or zero"; 
    } 
    return 0;
}


else-if construct

(else-if ladder)



    if ( condition-1 ) {

        statement; // condition-1 is true

    } else if ( condition-2 ) {

        statement; // condition-2 is true

    } else if ( condition-3 ) {

        statement; // condition-3 is true

    } else {

        // default case:
        statement; // all above conditions were false

    }

else-if ladder example



#include <iostream>
int main()
{
    int x = -1;
    
    if ( x > 0 ) { 
    
        std::cout << "x is positive"; 
        
    } else if ( x < 0 ) { 
    
        std::cout << "x is negative"; 

    } else { 
    
        std::cout << "x is zero"; 
    } 
    return 0;
}

while loop



    while ( expression ) {
        statement;
    }
        int main()
        {
            int x = 0;
            while ( x < 5 ) { // condition
                // loop body:
                x = x + 1;
            }  
            return 0;
        }
while loop logic

for loop


for loop timeline



        for ( init; condition; increment ) {
            statement;
        }
        ...
    

for loop


#include <iostream>
int main()
{
    int x = 0;
    for ( x = 0; x < 5; ++x ) {
        std::cout << x;
    }
    return 0;
}
for loop logic

for loop example


#include <iostream>
int main()
{
    int x = 0;
    for ( x = 0; x < 5; ++x ) {
        std::cout << x;
    }
    return 0;
}
for loop logic

forever



#include <iostream>
int main()
{
    for ( ;; ) // forever
    {
        // "endless" loop:
        char answer;
        std::cout << "Exit program? Y/N:";
        std::cin >> answer;
        if ( answer == 'Y' || answer == 'y' ) {
            return 0;
        }
    }
    return 0;
}

do...while loop


switch statement


#include <iostream>
int main()
{
    std::cout << "Do you want to proceed (y or n)?\n";
    char answer = 0;
    std::cin >> answer; // get answer from user

    switch ( answer ) {
    case 'y':
        return true;

    case 'n':
        return false;

    default:
        std::cout << "Sorry, I don't understand\n";
        std::cout << "I'll take that for a no";
        return false;
    }
}

switch statement considerations


#include <iostream>
int main()
{
    bool proceed = false ;
    std::cout << "Do you want to proceed (y or n)? "; // display question
    char answer = 0;
    std::cin >> answer; // read answer
    switch ( answer ) {
        case 'Y':
        case 'y':
            proceed = true;
            break;
        default:
            std::cout << "Sorry, I don't understand\n";
            std::cout << "I'll take that for a no\n";
        case 'N':
        case 'n':
            proceed = false;
            break;
    }
    return 0;
}

break statement


continue statement


Conclusion