3.2: Selection Statement- if-statement

Introduction to Programming with C++ · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY-NC-SA 4.0, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    3.2: Selection Statement- if-statement

    Selection Statement: if-Statement

    A C++ program consists of sequence of statements, and normally they will be executed one after the other sequentially. However, it is not always the case. Sometimes, a statement or a group of statements may or may not be executed depending on certain condition.

    For example, following program contains a group of statements (line 10 to line 12):

    Image 3-2-1.png

    If we run this program as the way it is, we will see the messages on the output screen regardless what the score we entered. If we only want the students with the test score below 60 to see those messages, we will use a selection statement in this program as shown below:

    Image 3-2-2.png

    In this program, we used an "if" statement, which is one of the selection statements, through line 10 to line 14. This selection statement starts at line 10 with a key word "if" followed by a relational statement score<60 inside of a pair of parentheses. And a pair of braces { and } specifies a group of statements from line 11 to line 13. This group of statements will be executed if the the value of the relational statement score<60 is true, which means the score a user enter is less than 60. Otherwise, nothing will be displayed on the output screen, which means the statements in line 11 to 13 will be skipped by the execution of the program.

    "if" Statement

    The one we were using in above program is one of the selection statement in C++ language. Its structure is shown as following:

    General structure of an "if" statement in C++:

    if (relational statement) {

    statement1;
    statement2; <----------- body of the if statement
    .........
    .........

    }

    Statement (s)

    Following flow chart demonstrates the logic of an "if" statement:

    Image 3-2-3.png

    When a computer executes an if statement in a program, it will first evaluate the value of the relational statement in the if statement, if the value of the vlaue of the relational statement is true, then the computer will execute the statements in the body of the if statement, otherwise, the computer will skip the statements in the body of the if statement and move on the next statement that immediately follows the close brace } in the program.

    Example

    Write a program that asks a user to enter two integers and save them into two variables. Then switch the values of the two variables if the value of the first variable is greater than the value of the second variable, so when we display the values of those two variables in the order of first variable and the second variable, the numbers show up are always in an ascending order.

    When we first see this programming problem, it seems intimidating. We just don't know what it is talking about and, of course, we don't know how to get started.

    Don't worry, at least we can create an empty project and open a blank source file and that is a good start!

    preview_ila5

    Read the question, the first statement of the question says: "Write a program that asks a user to enter two integers and save them into two variables". So, that means we will need two variables in this program. Since all the variables used in C++ program need to be declared, we should put a line of code that declares two integer variables. Yes, that is exactly what we need to do in this program:

    preview_cp61

    We made some progress. It may not be perfect correct, but it is better than simply stare at screen and do nothing! In the first statement of the question, it also says: "Ask a user to enter two integers", this tells us we should put a line of code like following:

    cout<<"Please enter two integer:"<<endl;

    because this line of code will display the message "Please enter two integers:" on the output screen and it can be seen by a user.

    The first statement of the question also says to save the data a user entered into those variables we declared in line 6. Recall the statement we used in the example of calculating the area of a rectangle. There, we use

    cin>>length;
    cin>>width;

    to store the data user entered in the variables length and width. Now, we use similar statements to store the integer user entered into the variable a and b:

    cin>>a;
    cin>>b;

    Alternatively, those two statements can be written equivalently as following: (which uses a single line of the code instead of two)

    cin>>a>>b;

    we can use either one of them in our program to store the integers the user entered into the variable a and b:

    preview

    We are making more progress!

    Now, continue to read the question. Next phrase is "Then switch the values of two variables" Well, this may be a little bit difficult for you to grasp.

    There are two data storage used in this program, one is represented by the variable a and the other is represented by the variable b, they are current used for storing the first and second integer entered by the user. We can't use following two lines of code for switching the values:

    b = a;
    a = b;

    The reason is when the program executes the first statement, the the value of b is replaced by the value of a and the original value of b is gone. The second statement just put the same data that was in a back to a. Thus, after above two statements, the values of both variables are the same. In other words, after above two lines of code, both a and b are holding the the data that user entered into a.

    To switch the values between the variables a and b, we will need a third variable, say t. We will need to copy the data in a and save it in the variable t by following statement:

    t = a;

    then copy the value of b and put it into a:

    a = b;

    Now, the data in a is gone and replaced by a copy of the data in b. Since the data in the variable a has been saved in the variable t, we now copy the data in t, which is original data in a, and put it into the variable b by using following statement:

    b = t;

    So, by following three statements:

    t = a; //copy the data in a to t

    a = b; //copy the data in b to a

    b = t; //copy the data in t to b

    we successfully swapped the values of the variables a and b. However, when you read the question carefully, you will realize that we don't want to do the swapping for any integers user enter unconditionally. We only do the swapping when the first number a user entered is greater than the second number the user entered. That being said, we only need to execute above three statements under the condition: (a > b). Therefore, we will use an if statement to control the execution flow as following:

    preview_autm

    The code shown above controls the execution of the switching under the condition (a > b) with an if statement beginning at line 10. Since we added an additional variable t in the program, we will need to go back to the line 7 and add it to the declaration statement. Now, the last thing we need to do in this program is to use a "cout" statement at line 15 to display the values of a and b.

    preview_kfge

    Now, we have completed the coding of this program. When we run this program, for any two numbers a user entered, the program will always display those two numbers in an ascending order. Please test run this program on your computer.


    Exercise

    Write a program that asks a user to enter 6 integers. Among those integers, some of them are odd integers and some of them are even integers. The program uses only two two variables to calculate and display the sum of those even integers user entered.

    Download the solution


    This page titled 3.2: Selection Statement- if-statement was last modified on Wed, 24 Dec 2025 20:37:21 GMT and is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Jin He.