Lecture 4-2: Branch Statement

if ...

#include <iostream>

int main() 
{
    bool flag = true;
    if(flag)
    {
        std::cout << "The flag is true" << std::endl;
    }
    std::cout << "The normal statement" << std::endl;
}
#include <iostream>

int main() 
{
    bool flag = true;
    if(flag)
        std::cout << "The flag is true" << std::endl;
    std::cout << "The normal statement" << std::endl;
}

Notice: different from python

#include <iostream>

int main() 
{
    bool flag = true;
    if(flag)
        std::cout << "The flag is true" << std::endl;
        std::cout << "The normal statement" << std::endl;
}
#include <iostream>

int main() 
{
    int input = -100;
    if(input < 0)
    {
        std::cout << "The input is negative" << std::endl;
    }
}

if ... else

#include <iostream>

int main() 
{
    bool flag = true;
    if(flag)
    {
        std::cout << "The flag is true" << std::endl;
    }
    else // flag == false
    {
        std::cout << "The flag is false" << std::endl;
    }
    std::cout << "The normal statement" << std::endl;
}
#include <iostream>

int main() 
{
    int input = -100;
    if(input < 0)
    {
        std::cout << "The input is negative" << std::endl;
    }
    else // input >= 0
    {
        std::cout << "The input is positive" << std::endl;
    }
}

if ... else if ... else

#include <iostream>

int main() 
{
    int input = -100;
    if(input < 0)
    {
        std::cout << "The input is negative" << std::endl;
    }
    else if(input == 0)
    {
        std::cout << "The input is zero" << std::endl;
    }
    else // input > 0
    {
        std::cout << "The input is positive" << std::endl;
    }
}

nested if ... else

#include <iostream>

int main() 
{
    int input = -100;
    if(input < 0)
    {
        std::cout << "The input is negative" << std::endl;
    }
    else 
    {
        if(input == 0)
        {
            std::cout << "The input is zero" << std::endl;
        }
        else // input > 0
        {
            std::cout << "The input is positive" << std::endl;
        }
    }
}

switch ... case

#include <iostream>

int main() 
{
    int input = -100;
    switch(input)
    {
        case -1:
            std::cout << "The input is -1" << std::endl;
            //break;
        case 0:
            std::cout << "The input is 0" << std::endl;
            //break;
        case 1:
            std::cout << "The input is 1" << std::endl;
            //break;
        default:
            std::cout << "The input is others" << std::endl;
    }
}

return 0 to end main function

#include <iostream>

int main() 
{
    int input = -100;
    if(input < 0)
    {
        std::cout << "The input is negative" << std::endl;
    }
    else 
    {
        // return 0;
        if(input == 0)
        {
            std::cout << "The input is zero" << std::endl;
        }
        else // input > 0
        {
            std::cout << "The input is positive" << std::endl;
        }
    }
    return 0;
}

Reference: