Skip to main content
Engineering LibreTexts

6.2.1: Conditional Execution - If-Else

  • Page ID
    29057
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The if-else Statement

    There is also an if statement that has 2 parts to it. The statements that execute when the condition is TRUE, and the statements that execute when the statements is FALSE. The syntax for the if-else control structure:

    // C++ program to illustrate if-else statement 
    #include<iostream> 
    using namespace std; 
    
    int main() 
    { 
            int myNum = 20; 
    
            if (myNum < 15) 
                cout << "myNum is smaller than 15" << endl;
            else
                cout << "myNum is greater than 15" << endl;
                
        return 0;     
    } 
    

    Notice that we do NOT have the curly braces. This is allowed since we only have a single statement in the if portion and the else portion. You MAY include the brackets if you want to.

    // C++ program to illustrate if-else statement 
    #include<iostream> 
    using namespace std; 
    
    int main() 
    { 
            int myNum = 20; 
    
            if (myNum < 15) 
            {
                cout << "myNum is smaller than 15" << endl;
                myNum = 99;
            }
            else
                cout << "myNum is greater than 15" << endl;
                
        return 0;     
    } 

    This again is valid, you only need the curly brackets if you want to include more than a single statement in your block of code. In this case if you left out the brackets you would get an error on the else statement.

    Adapted from:
    "If Then Else" by Kenneth Busbee, Download for free at http://cnx.org/contents/303800f3-07f...93e8948c5@22.2 is licensed under CC BY 4.0 
    "Decision Making in C / C++ (if , if..else, Nested if, if-else-if )" by Harsh AgarwalGeeks for Geeks is licensed under CC BY-SA 4.0


    This page titled 6.2.1: Conditional Execution - If-Else is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.

    • Was this article helpful?