CCIS1102 Computer Programming 1 Week 10 Selection Structure
CCIS1102 Computer Programming 1 Week 10 Selection Structure
COMPUTER
PROGRAMMING 1
WEEK 10 – SELECTION STRUCTURES
CONTROL STRUCTURES
Control Structures can be considered as the building blocks of computer
programs. They are commands that enable a program to “take
decisions”, following one path or another. A program is usually not
limited to a linear sequence of instructions since during its process it
may bifurcate, repeat code or bypass sections. Control Structures are
the blocks that analyze variables and choose directions in which to go
based on given parameters. The three control structures in processing
programs are Sequential, Selection, and Repetition structure.
CONTROL STRUCTURES
• Sequence Structure is defined as the straightforward execution of one
processing step after another
• Selection Structure can be defined as the presentation of a condition,
and the choice between two actions depending on whether the
condition is true or false.
• Repetition Control Structure can be defined as the presentation of a
set of instructions to be performed repeatedly, as long as the condition
is true
SELECTION CONTROL
STRUCTURES
• Selection (conditional) structure in which it is used to
execute one or more statements if a condition is met
• “Conditionals” are at the very core of programming. The
idea behind them is that they allow you to control the flow
of the code that is executed based on different conditions
in the program
• Conditionals Control Structures in C++:
• If statements
• If-Else statements
• If-Else If statements
• Nested If statements
• Switch statements
IF Statements
• IF statement execute one or more statements when a
condition is met. If the testing of that condition is TRUE,
the statement gets executed. But if it’s FALSE (the
condition is not met), then nothing happens.
The syntax of “If statements” is:
if (<boolean expression>)
<statement>
OR
if (<boolean expression>) {
<statement1>
<statement2>
}
IF Statements
IF Statements
• Example: Let’s say you want to verify if the value of a
variable (x) is positive:
int x =10;
if(x >= 0){
cout<< “variable x is a positive number”;
}
Output:
variable x is a positive number
In this example, first we assign the value of 4 to the
variable (x) and use the “If statement” to verify if that
value is equal or greater than 0. If the test results
TRUE (as in this case), the function will print the
sentence: “variable x is a positive number”.
IF Statements
• Example: Suppose that you want to compute the
absolute value of an int value x.
if (x < 0)
x = -x;
switch (oper) {
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
SWITCH Statements else{
cout<<"Invalid Input. Please try again."<<endl;
/* }
This will convert length (cm to inches, inches to cm) and mass
break;
(pounds to Kg, Kg to pounds)
*/ case 'B':
#include<iostream> case 'b':
using namespace std; cout<<"[1] Pounds to Kilogram"<<endl;
int main() cout<<"[2] Kilogram to Pounds"<<endl;
{ cout<<"Select your choice: ";
char option1; cin>>option2;
int option2; if(option2==1){
float length,weight, cm, inch, kg, pound;
cout<<"Enter weight(in pounds): ";
cout<<"Conversion"<<endl;
cout<<"[A] Length"<<endl; cin>>weight;
cout<<"[B] Mass"<<endl; kg = weight/2.20462262185 ;
cout<<"Select your choice: "; cout<<weight<<" pounds is equivalent to "<<kg<<"
cin>>option1; kilogram"<<endl;
switch(option1){ }else if(option2==2){
case 'A': cout<<"Enter weight(in kilogram): ";
case 'a': cin>>weight;
cout<<"[1] Centimeter to Inches"<<endl;
pound = weight*2.20462262185;
cout<<"[2] Inches to Centimeter"<<endl;
cout<<"Select your choice: ";
cout<<weight<<" kilogram is equivalent to "<<pound<<"
cin>>option2; pounds"<<endl;
if(option2==1){ }
cout<<"Enter length(in Centimeter): "; else{
cin>>length; cout<<"Invalid Input. Please try again."<<endl;
inch = length/2.54 ; }
cout<<length<<" centimeter is equivalent to "<<inch<<" inches"<<endl; break;
}else if(option2==2){
default:
cout<<"Enter length(in Inches): ";
cin>>length;
cout<<"Invalid Input. Please try again."<<endl;
cm = length*2.54; }
cout<<length<<" inches is equivalent to "<<cm<<" centimeter"<<endl; return 0;
} }
SWITCH VS IF-ELSE
The switch and if-else both are selection statements and they both let you select an
alternative out of given many alternatives by testing an expression. However, there are
some differences in their operations. These are given below:
• The switch statement differs from the if statement in that switch can only test for
equality whereas if can evaluate a relational or logical expression i.e., multiple
conditions.
• The switch statement selects its branches by testing the value of same variable
(against a set of constants) whereas the if-else construction lets you use a series of
expressions that may involve unrelated variables and complex expressions.
• The if-else is more versatile of the two statements. For example, if-else can handle
ranges whereas switch cannot. Each switch case label must be a single value.
• The if-else statement can handle floating-point tests also apart from handling integer
and character tests whereas a switch cannot handle floating-point tests. The case
labels of switch must be an integer (which includes char also).
• The switch case label value must be a constant. So, if two or more variables are to be
compared, use if-else.
• The switch statement is more efficient choice in terms of code used in a situation that
supports the nature of switch operation (testing a value against a set of constants).
SUGGESTED ACTIVITIES
1. Write a C++ program to enter a number then display the message "Even
Number" if the entered number is even and the message "ODD Number" if it is
odd2.
2. Write a C++ program to enter a string then display the message boxes "Greater
than 6 characters", "Less than 6 characters", and "Equal to 6 characters" if this
number was greater, less, and equal to six characters respectively
3. Write a C++ program to find W from the equations:
4. Suppose the random bank offers 9% interest on balances of less than $5000,
12% for balances of $5000 or more but less than $10000, and 15% for balances
of $10000 or more. Write a C++ program to enter a person balance then
calculates a customer’s new balance after one year.
THANK YOU!