Selection Control Structure in C++ PDF

Title Selection Control Structure in C++
Course Fundamentals of Programming
Institution Pangasinan State University
Pages 20
File Size 1.4 MB
File Type PDF
Total Downloads 87
Total Views 139

Summary

FM AA CIA 15 MOD6...


Description

FM-AA-CIA-15 Rev. 0 10-July-2020 Study Guide in CC102 Fundamentals of Programming

Module No. 6

STUDY GUIDE FOR MODULE NO. 6

SELECTION CONTROL STRUCTURE MODULE OVERVIEW

Welcome to this module! By the time that you are reading this, you have already written several programs that execute a linear sequence of instructions. But programs are not limited to a linear sequence of statements. During its process, a program may repeat segments of code, or take decisions and branch off. Thus, we need to be able to alter the order in which a program’s statements are executed. For that purpose, C++ provides control structures that serve to specify what has to be done by our program, when and under which circumstances. Typically, there are two kinds of control structures: selection and repetition. In this module, you will be introduced to the different selection structures in C++. So let’s get started!

MODULE LEARNING OBJECTIVES By the end of this module you should be able to:  Include a selection structure in pseudocode and in a flowchart  Code the if and if/else forms of the selection structure  Write code that uses comparison operators and logical operators  Include a nested selection structure in pseudocode and in a flowchart  Code a nested selection structure  Recognize common logic errors in selection structures  Include the switch form of the selection structure in pseudocode and in a flowchart  Code the switch form of the selection structures in C++

LEARNING CONTENTS (if…else Statement)

Introduction The basic attribute of a control structure is to be able to select between two or more alternate paths. This can be described as either two-way selection or multi-way selection. In order for a program to change its behavior depending on the input, there should be a way for us to test that input. A question using Boolean concepts usually controls which path is selected for execution. C++ has if and if/else, switch, and nested selection control structures. A selection structure, also called the decision structure, is used when you want a program to make decision or comparison and then, based on the result of that decision or comparison, select one of two paths. Let’s take a look at the two examples of selection structures that you might use today.

Example 1 if (it is raining) wear a rain coat bring an umbrella

Example 2 if (you have a test tomorrow) study tonight else watch a movie

In the examples above, the portion in parentheses, called the condition, specifies the decision you are making and is phrased so that it results in either a true or false answer only. For example, either it’s raining (true) or it’s not raining (false); either you have a test tomorrow (true) or you don’t have a test tomorrow (false).

PANGASINAN STATE UNIVERSITY

1

FM-AA-CIA-15 Rev. 0 10-July-2020 Study Guide in CC102 Fundamentals of Programming

Module No. 6

If the condition is true, you will perform a specific set of tasks. If the condition is false, on the other hand, you might or might not need to perform a different set of tasks. We use different forms of if…else statement to implement such decision making in our C++ program. However, if the program should decide among multiple conditions, we use multiple-alternative selection control structures such as nested if and switch statements. The figure below presents a summary of selection control structures you can use in C++.

Source: https://www.geeksforgeeks.org/decision-making-c-c-else-nested-else/ 1.1 if…else Statement There are three forms of if…else statements in C++: 

if statement



if…else statement



if…else if…else statement

Before we discuss how we can implement these forms of selection control structures in your program, let’s take a look at how the selection control structure is included in the pseudocode. if selection structure 1. enter the part number and price condition 2.

true path

if (the part number is “AB203”) calculate price by multiplying price by 1.1 display “Price increase” message end if 3. display the part number and price if/else selection structure 1. enter the sales amount

condition 2.

if (the sales amount is greater than 1500)

true path PANGASINAN STATE UNIVERSITY

2

FM-AA-CIA-15 Rev. 0 10-July-2020 Study Guide in CC102 Fundamentals of Programming

false path 3.

Module No. 6

calculate the commission by multiplying the sales amount by .02 else calculate the commission by multiplying the sales amount by .01 end if display the commission

The example above shows how the selection control structure is included in the pseudocode. The comparison using the if statement is used to check whether a condition is true or false. The true path is the statement that is executed when the condition is true. The true path ends when you come to the else or, if there is no else, when you come to the end of the selection structure (the end if). After the true path instructions are processed the instruction following the end if is processed. In the examples shown above, the display instructions are processed after the instructions in the true path. The instruction that is processed when the if structure’s condition is false depend on whether the selection structure contains an else. When there is no else, as in the first example shown in Fig. 2, the if structure ends when its condition is false, and processing continues with the instruction following the end if. In the first example, for instance, the “display the part number and price” instruction is processed when the part number is not “AB203.” In cases where the selection structure contains an else, as in the second example in the example, the instructions between the else and the end if—referred to as the false path—are processed before the instruction after the end if is processed. In the second example, the “calculate the commission by multiplying the sales amount by .01” instruction is processed first, followed by the “display commission” instruction. The corresponding flowchart of the previously presented pseudocode of the if and if…else statements is presented below. if selection structure

if/else selection structure

start

start

Enter the part number and price

Enter the sales F

F

T

Part number equals “AB203”

price=price * 1.1

Display “Price increase” message

sales > 1500

commission = .01 * sales

T

commission = .02 * sales

Display the stop

Display the part number and price

stop

PANGASINAN STATE UNIVERSITY

3

FM-AA-CIA-15 Rev. 0 10-July-2020 Study Guide in CC102 Fundamentals of Programming

Module No. 6

Now that we know where and the selection control structure is included in the pseudocode and flowchart, we now discuss how to code the the if…else statements in C++.

1.1 Coding the if statement The most basic kind of conditional branch in C++ is the if statement. The if keyword is used to execute a statement or block, if, and only if, a condition is fulfilled. An if statement takes the form: if (condition) statement Here, condition is the expression that is being evaluated. If this condition is true, statement is executed. If it is false, statement is not executed (it is simply ignored), and the program continues right after the entire selection statement. For example, the following code fragment prints the message (The number is 1000), only if the value stored in the num variable is indeed 1000: if (num == 1000) cout...


Similar Free PDFs