Skip to main content
Engineering LibreTexts

6.2: If - Else

  • Page ID
  • \( \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}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    Conditional Execution

    We are going to introduce the control structure from the selection category that is available in every high level language. It is called the if - else structure. Asking a question that has a true or false answer controls the if statement. It looks like this:

    if the answer to the question is true
      then do this

    The questions, called the condition, is a Boolean expression. The Boolean data type has two values – TRUE and FALSE. Let's rewrite the structure to consider this:

    if expression is true
      then do this

    Some languages use reserved words of: if, then and else. Many eliminate the then. Additionally the "do this" can be tied to true and false. You might see it as:

    if expression is true
      action true

    And most languages infer the "is true" you might see it as:

    if expression
      action true

    The above four forms of the control structure are saying the same thing. The else word is often not used in our English speaking today. However, consider the following conversation between a mother and her child.

    Child asks, "Mommy, may I go out side and play?"

    Mother answers, "If your room is clean then you may go outside and play or else you may go sit on a chair for five minutes as punishment for asking me the question when you knew your room was dirty."

    Let's note that all of the elements are present to determine the action (or flow) that the child will be doing. Because the question (your room is clean) has only two possible answers (true or false) the actions are mutually exclusive. Either the child 1) goes outside and plays or 2) sits on a chair for five minutes. One of the actions is executed; never both of the actions.

    if statement in C/C++

    The if statement is the most simple decision making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not. If you remember, anything enclosed in the curly brackets is considered a block of code.

       // Statements to execute if
       // condition is true

    Here, condition after evaluation will be either true or false. If the value is true then it will execute the block of statements below it otherwise not.

    TAKE NOTE: If we do not provide the curly braces ‘{‘ and ‘}’ after if (condition) then by default ONLY the first statement is part of the if statement.

    For example - the code below will see only statement1 as part of the if statement. In this case statement2 will get executed whether the condition is TRUE or FALSE. Just because a line of code is indented DOES NOT mean it is considered part of the if statement

    // Here if the condition is true
    // if considers statement1 to be inside its block.

    A very simple piece of code shows an if statement. In this case the condition ( myNum > 10) is FALSE, so we would not see the "10 is less than 15" output to the terminal.

    #include <iostream>
    using namespace std; 
    int main() 
    	int myNum = 10; 
    	if (myNum > 15) 
    		cout << "10 is less than 15" << endl; 
    	cout << "I am Not in if" << endl; 

    Two Way Selection within C++

    The syntax for the if then else control structure within the C++ programming language is:

    if (expression)

    Note: The test expression is within the parentheses, but this is not a function call. The parentheses are part of the control structure. Additionally, there is no semicolon after the parenthesis following the expression.

    Adapted from:
    "If Then Else" by Kenneth Busbee, Download for free at 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: If - Else is shared under a CC BY-SA license and was authored, remixed, and/or curated by Patrick McClanahan.