Introduction to Programming
(C++)
Gliceria S. Tan
Instructor
Session 9 Topics
➢ Control Structures
➢ Decision Structures
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
Control Structures
A control structure is a logical design that
controls the order in which a set of
statements executes.
The type of control structure that we have
used so far in programming is the sequence
structure where the statements execute
from top to bottom.
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
Decision Structure
We now move on from the simple sequence
structure to the decision structure.
The decision structure allows us to select
and execute specific blocks of code while
skipping other sections. (A block is a group
of statements enclosed in braces {}).
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
The if statement
Syntax:
if (expression)
statement;
or
if (expression) {
statement;
Block or statement block
…
}
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
For example:
int grade = 68;
if( grade > 60 )
cout<<"Congratulations!";
or
int grade = 68;
if( grade > 60 ) {
cout<<"Congratulations! “<<endl;
cout<<"You passed!";
}
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
The if-else statement
Syntax:
if(expression)
statement1;
else
statement2;
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
For example:
int grade = 68;
if( grade > 60 )
cout<<"Congratulations!";
else
cout<<"Sorry you failed";
or
int grade = 68;
if( grade > 60 ){
cout<<"Congratulations!“<<endl;
cout<<"You passed!";
}
else
cout<<"Sorry you failed";
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
The if-else-if statement
Syntax:
if(expression)
statement;
else if(expression)
statement;
else if(expression)
statement;
.
.
.
else
statement;
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
For example:
int grade = 92;
if( grade >= 90 )
cout<<"Excellent!";
else if( grade<90 && grade>=80 )
cout<<"Good job!";
else if( grade<80 && grade>=60 )
cout<<"Study harder!";
else
cout<<"Sorry, you failed.";
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
The nested if
A nested if is an if statement that is the
object of either an if or else.
Example:
if(x)
if(y)
cout<<“1”;
else
cout<<“2”;
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
In C++, else is linked to the closest preceding if
that does not already have an else associated
with it. In this case, else is associated with if(y).
if(x)
if(y)
cout<<“1”;
else
cout<<“2”;
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
To make else associate with if(x) you must use
curly braces to override its normal association, as
shown below:
if(x) {
if (y)
cout<<“1”;
}
else
cout<<“2”;
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
Recall this sample problem:
Draw a flowchart to enter the age of a person. If
the person is 18 years old or above display
“QUALIFIED”, otherwise display “UNDERAGE”.
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
#include <iostream>
using namespace std;
int main(){
int age;
cout<<“Enter a person’s age: “;
cin>>age;
if(age>=18)
cout<<“QUALIFIED”;
else
cout<<“UNDERAGE”;
}
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
Recall this sample problem:
Draw a flowchart to enter two numbers. If the first
number is greater than the second number display
their difference, otherwise display their sum.
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
#include <iostream>
using namespace std;
int main(){
int a, b, sum, dif;
cout<<“Enter two numbers: “;
cin>>a>>b;
if ( a > b ) {
dif = a - b;
cout<<“The difference is ”<<dif;
}
else {
sum = a + b;
cout<<“The sum is ”<<sum;
}
}
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
The switch statement
Syntax:
switch( expression ){
case value1:
statement/s
break;
case value2:
statement/s
break;
.
.
.
default: statement/s
}
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
The value of the switch expression is compared
with the value of each case. When a match is
found, the statement or statements are executed
until the break statement is reached. The value of
the expression can either be an integer or a
character.
The break statement is used to terminate a case in
the switch statement.
The default statement is executed if no match is
found. This statement is optional and, if not
present, no action takes place if all matches fail.
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
For example:
int number = 3;
switch(number){
case 1:
cout<<“One";
break;
case 2:
cout<<“Two";
break;
case 3:
cout<<“Three";
break;
default:
cout<<"Sorry, wrong number.";
}
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
Sample problem:
Draw a flowchart and write a program to accept a
student’s year-entry number and display its equivalent high
school level. Here are the criteria:
Year-entry Number High School Level
1 Freshman
2 Sophomore
3 Junior
4 Senior
Other entry no. Out-of-school
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
START
#include <iostream> INPUT year
main(){
int year; T
year==1 OUTPUT “Freshman”
cout<<“Enter the year entry number: “; F
cin>>year; year==2
T
OUTPUT “Sophomore”
switch(year) {
F
case 1: cout<<“Freshman”; break; T
year==3 OUTPUT “Junior”
case 2: cout<<“Sophomore”; break;
case 3: cout<< “Junior”; break; F
T
case 4: cout<< “Senior”; break; year==4 OUTPUT “Senior”
default: cout<< “Out-of-school”; F
} OUTPUT “Out-of-school”
}
STOP
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR
“QUOTE OF THE DAY!”
GLICERIA S. TAN, MIS COMPUTER PROGRAMMING 1
INSTRUCTOR