Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

4 SELECTION STATEMENTS

Module By: Engr. Neil P. Magloyuan

Overview

To process a program, the computer begins at the first executable statement and
executes the statements in order until it comes to the end. This module, you will
learn how to tell a computer that it does not have to follow a simple sequential order
of statements; it can also make decisions and are used to alter the sequential flow of
execution. The two most common control structures are selection and repetition but
this module tackles the selection structure which programs executes particular
statements depending on some condition(s).

Selection Statements 1
Activity

Try this out!


1. Copy the following codes on your editor.

#include <iostream>

#include <iomanip>

using namespace std;

const double interest_rate = 0.015;

int main( )

double creditCardBalance;

double payment;

double balance;

double penalty = 0.0;

cout << fixed << showpoint;

cout << "Line 12: Enter credit card balance: ";

cin >> creditCardBalance;

cout << endl;

cout << "Line 15: Enter the payment: ";

cin >> payment;

cout << endl;

balance = creditCardBalance - payment;

if (balance > 0)

penalty = balance * interest_rate;

cout << "Line 21: The balance is: Php " << balance<< endl;

cout << "Line 22: The penalty to be added to your "<< "next month bill is: Php "<<
penalty << endl;

return 0;

Selection Statements 1
Selection Statements 1
3.2 If Else Then Statement

Learning Outcomes

At the end of the lesson, you are expected to:


• explain the if…else selection statement properly in the selection of alternative
actions ; and
• use an if..else selection statement in the program.

Time Frame: 3 hours

Introduction

There are many programming situations in which you must choose between two
alternatives. For example, if a part-time employee works overtime, the paycheck is
calculated using the overtime payment formula; otherwise, the paycheck is calculated
using the regular formula. In this way, you choose between two alternatives-that is, to
implement two-way selections and that is using the if…else statement. This lesson
provides you with these learnings.

Activity

Try this out!


1. Copy the following codes on your editor.

#include <iostream>
#include <iomanip>

using namespace std;

int main( )
{ int score;

cout << "Enter your score: "; cin >> score;

if (score >= 90)


cout << "The grade is A." << endl;
else if (score >= 80)
cout << "The grade is B." << endl;
else if (score >= 70)
cout << "The grade is C." << endl;
else if (score >= 60)
cout << "The grade is D." << endl; else

Selection Statements 1
cout << "The grade is F." << endl;
}

Selection Statements 1
4.3 Switch Statement

Learning Outcomes

At the end of the lesson, you are expected to:


• explain the use of a switch statement, and
• create a program using a switch

Time Frame: 3 hours

Introduction

Recall that there are two selection or branch structures in C++. The first selection
structure, which is implemented with if and if…else statements, usually requires the
evaluation of a logical expression. The second selection structure, which does not
require the evaluation of a logical expression, is called a switch structure. This lesson
will teach you how to use this structure

Acti vity

Try this out!

1. Copy the following codes on your editor.

#include <iostream>

using namespace std;

int main ()

int num;

cout << "Enter an integer between 0 and 7: ";

cin >> num;

cout << endl;

switch (num) {

case 0:

case 1:

cout << "Learning to use ";

Selection Statements 1
break;

case 2:

cout << "C++’s ";

break;

case 3:

cout << "Switch structure. " << endl;

break;

case 4:

break;

case 5:

cout << "This program shows the effect " ;

break;

case 6:

case 7:

cout << "of the break statement." << endl;

break;

default:

cout << "The number is out of range." << endl;

cout << "Out of the switch structure." << endl;

return 0;

Selection Statements 1
Selection Statements 1

You might also like