CP1 - Unit 9 - Conditional Statements
CP1 - Unit 9 - Conditional Statements
Introduction
Programs are executed from top to bottom (linear sequence). But as our problem or
requirements become complex, our programs more often than not will have to make decisions
to skip some of its parts and jump to some part depending on the answer after evaluating the
conditional statement that we provide. This is the part where we can apply the relational and
logical operators we discussed in the previous lesson.
Learning Objectives
1. Apply the use of relational and logical operators in writing conditional statements.
2. Define the format of if, if...else, switch, nested if and nested
switch statement.
3. Determine what conditional statement to use to satisfy all the requirements of the
program.
Course Materials
63
Conditional Statements
9.1 if statement Use the if statement to specify a block of C++ code to be executed if a
condition is true.
condition
True
False
Conditional
codes
Syntax:
if (condition) {
// block of code to be executed if the condition is true
}
Examples:
int age =0 ;
:
cin >> age;
if (age >= 60){
cout << “SENIOR CITIZEN”;
}
64
Conditional Statements
int age =0 ;
float price = 0.0;
9.2 if … else statement Use the else statement to specify a block of code to be executed if
the condition is false.
False
Conditional
condition codes
True
Conditional
codes
Syntax:
if (condition) {
// block of code to be executed if the condition is true
} else {
// block of code to be executed if the condition is false
}
65
Conditional Statements
Examples:
int age =0 ;
:
cin >> age;
if (age < 18){
cout << “MINOR”;
} else {
cout << “ADULT”;
}
Note: if there is only ONE statement for true or for false it is ok not to include the braces. Use
indentions to make your program easier to read and debug. Usual error – ‘else without if’
Also note that conditions are written inside the parentheses () and there is no semicolon
after the close parenthesis.
66
Conditional Statements
9.3 if else if ladder Several if + else structures can be concatenated with the intention of
checking a range of values.
True
Conditional
condition codes
False
True
Conditional
condition codes
False
Conditional
codes
Syntax:
if (condition) {
// block of code to be executed if the condition is true
} else if (condition){
// block of code to be executed if the condition is false
} else
// block of code to be executed if the condition is false
}
67
Conditional Statements
Examples:
cin >>x;
if (x >0)
cout << “x is positive”;
else if (x < 0)
cout << “x is negative”;
else
cout << “x is zero”;
cin>> grade;
68
Conditional Statements
Syntax:
if (condition1) {
// block of code to be executed if the condition is true
if (condition2){
// block of code to be executed if the condition is false
} else
// block of code to be executed if the condition is false
}
Examples
69
Conditional Statements
70
Conditional Statements
Why?
Remedy
9.5 switch statement - A switch statement allows an expression to be tested for equality
against a list of values. Each value is called a case, and the expression being switched
on is checked for each case.
Syntax:
switch(expression) {
case constant-expression :
statement(s);
break; //optional
case constant-expression :
statement(s);
break; //optional
71
Conditional Statements
Examples:
int number=0;
switch (number % 2) {
case 0: cout << "\n EVEN number";
break;
case 1: cout << "\n ODD number";
}
72
Conditional Statements
switch (plan)
{
case 'a':
case 'A': mfee = 700.00;
freecallSameNet = 20;
callRate = 7.00;
break;
case 'b':
case 'B': mfee = 900.00;
freecallSameNet = 40;
callRate = 6.50;
break;
case 'c':
case 'C': mfee = 1200.00;
freecallSameNet = 60;
callRate = 6.00;
break;
73
Conditional Statements
Input course #, year level, number of units enrolled, then compute and display the amount
to pay.
# include<iostream>
using namespace std;
int main()
{
int cNo = 0, Ylevel = 0, units =0;
float tuitionfee = 0.0, miscFee= 0.0, amt2Pay=0.0, error=0;
switch (cNo)
{
case 1: switch(Ylevel)
{
case 1: tuitionfee = units* 324.65;
miscFee = 4545.77;
break;
case 2: tuitionfee = units* 456.32;
miscFee = 5664.65;
break;
74
Conditional Statements
case 2: switch(Ylevel)
{
case 1: tuitionfee = units* 546.76;
miscFee = 3453.67;
break;
case 2: tuitionfee = units* 657.89;
miscFee = 5656.78;
break;
case 3: tuitionfee = units* 767.90;
miscFee = 6577.78;
break;
case 4: tuitionfee = units* 665.87;
miscFee = 4564.78;
break;
default: cout << "\n\nWrong year level for BSIT";
error =1;
} break;
75
Conditional Statements
Activities
Write C++ statements for the following conditions: (declare and initialize variables that you
will be using before writing the if statement)
1. Write an if statement that displays the string “Firetree” if the user enter the letter F (in any
case).
2. Write an if statement that displays the string “Entry error” if the user enters a number that is
less than 0; otherwise, display the string “Valid number”
3. Write an if statement that displays the string “Reorder” if the user a number less than 10;
otherwise display the string “OK”
4. Write an if statement that assigns the number 10 to the bonus variable if the user enters a
sales amount that is less than or equal to $250. If the user enters a sales amount that is
greater than $250, prompt the user to enter the bonus rate, and then multiply the user’s
response by the sales amount and assign the result to the bonus variable.
5. Write an if statement that displays the string “Valid entry” when the user enters either the
integer 1, the integer 2, or the integer 3; otherwise, display the string “Error entry”
6. Write an if statement to test if the input data (two meter readings) is valid. To be valid, both
meter readings must be greater than zero, and the current meter reading must be greater
that the previous reading. If the data is not valid, display an appropriate error message.
7. Write an if statement to test if the number of registrants is greater than zero but less than 50.
Display an appropriate error message if the number of registrants is invalid.
8. Write an if statement that will display the shipping charge based on the province entered by
the user. If the user enters any other state, the result should be an “Incorrect state” message.
Laguna 130
Cavite 130
9. 9. Write an if statement that will display the shipping charge based on the province entered
by the user. If the user enters any other state, the shipping charge should be 0..
Batangas 250
Quezon 300
76
Conditional Statements
Programming Exercises
1. Write a C program the will determine the total amount the company owed using an if.
The seminar fee per person is based on the number of people the company registers, as
showed in the table below. (For example, if the company registers seven people, then
the total amount owed by the company is Php 12600.00
5 – 10 Php 1800.00
2. Write a C program that will ask the user to input the following: total monthly income,
total monthly expenses, preferred model house (A,B or C) and mode of payment (C –
cash or I – installment). Please refer to the table below to see if loan will be granted or
not and how much will a customer pay if cash payment was chosen.
For a loan to be granted monthly amortization must be less than (total monthly income
less total monthly expenses). A 5% discount from the TCP price is given if mode is cash
basis.
Examples:
77
Conditional Statements
Discount: 178,194.50
Balance: 3,385,695.50
--------------------------------------------
--------------------------------------------
78
Conditional Statements
Online References
https://www.w3schools.com/cpp/cpp_conditions.asp/
https://ecomputernotes.com/cpp/control-structures/conditional-statements
https://www.programiz.com/cpp-programming/if-else
79