The document discusses control structures in programming, specifically selection structures using if, if/else, and switch statements. It provides examples of pseudocode and flowcharts to illustrate basic selection statements. Key concepts covered include:
- Using if, if/else, and switch statements to choose between alternative actions.
- Examples of pseudocode for if/else statements and switch structures.
- Flowcharts as a graphical representation of algorithms using basic shapes.
- Examples of if, if/else, and switch statements in C++ code.
2. LEARNING OUTCOME
Understanding basic problem solving
techniques.
To be able to develop algorithms through
the process of top – down, stepwise
refinement.
To be able to use if, if/else and switch
selection structures to choose among
alternative actions.
3. ALGORITHM and PSEUDOCODE
Algorithm: A procedure for solving a problem in terms of the
actions to be executed and the order in which these actions are
to be executed (recipe).
Pseudocode: an artificial and informal language that helps
programmer develop algorithms that will latter be converted to
structured C++ program.
A pseudocode is similar to everyday English: convenient and
user friendly although not actual computer programming
language.
A pseudocode consists of executable statements (those that are
executed when the program has been converted from
pseudocode to C++.
4. Examples of PSEUDOCODE
1. if (score is greater than or equal to 90)
grade is A
2. if (hours worked are less than or equal to 40)
wages = rate * hours
Otherwise
wages = (rate * 40) + 1.5 * (rate * (hours -40))
3. if (temperature is greater than 20 degrees and it is not raining)
go to play golf!
Equivalent C++ code:
1. if (score >= 90)
cout << “Grade: “ << ‘A’ << endl;
2. if (Hours <= 40)
wages = rate * Hours;
else if (Hours > 40)
wages = (rate*40) + 1.5 * (rate * (Hours -40));
3. if ((temp > 20) && (!(raining))
cout << “go out to play golf! “;
5. Flowchart
Flowchart: A graphical representation of an algorithm
Drawn using certain special – purpose symbols such
as rectangles, diamonds, ovals and small circles.
These symbols are connected by arrows called flow-
lines
7. Example of flowchart:
The Basic If Statement
Syntax
if (Expression)
Action
If the Expression is true then
execute Action
Action is either a single
statement or a group of
statements within braces
Expression
Action
true false
8. Control Structures
A computer can process a program in one of the following
ways:
a) simple sequence – starts at the beginning and follows and
executes the statements in order. The simple input/output
examples we saw so far are simple sequence operation
*** However, C++ offers the use of the following control
structures for selection (decision making) and repetition:
b) selection – the program executes particular statements
depending on some conditions
c) repetition – the program repeats particular statements a
certain number of times based on some conditions
(discussed in detail in chapter 6)
10. Conditional Constructs
Provide
Ability to control whether a statement list is executed
Two constructs
If statement
if
if-else
if-else-if
Switch statement
Reading assignment : You will be required to present to the class
the concept of the switch control structure and demonstrate your
understanding using a C++ program example. Presentation will
be done in a group of 4-5 students. Presentation day will be the
first laboratory class after your semester break.
11. Example of if statement
What will be the output when the following
statements run?
int m = 5;
int n = 10;
if (m < n)
++m;
++n;
cout << " m = " << m << " n = " n << endl;
12. Example: Absolute value of a number
if (Value < 0) {
Value = -Value;
}
Value < 0
Value = -Value
true f alse
Is our number negative?
If Value is not less
than zero then our
number is fine as is
If Value is less than
zero then we need to
update its value to
that of its additive
inverse
Our number is
now definitely
nonnegative
13.
14. Sorting Two Numbers
cout << "Enter two integers: ";
int Value1;
int Value2;
cin >> Value1 >> Value2;
if (Value1 > Value2) {
int RememberValue1 = Value1;
Value1 = Value2;
Value2 = RememberValue1;
}
cout << "The input in sorted order: "
<< Value1 << " " << Value2 << endl;
15. Semantics
value2 < value1
int rememberValue1 = value1
value1 = value2
value2 = rememberValue1
true f alse
Are the numbers
out of order
Rearrange value1
and value2 to
put their values
in the proper
order
The numbers were
initially in order
The numbers were
rearranged into the
proper order
The numbers are in
order
16.
17. The If-Else Statement
Syntax
if (Expression)
Action1
else
Action2
If Expression is true then execute
Action1 otherwise execute Action2
if (v == 0) {
cout << "v is 0";
}
else {
cout << "v is not 0";
}
Expression
Action1 Action2
true false
18. Example: Finding the Max
cout << "Enter two integers: ";
int Value1;
int Value2;
cin >> Value1 >> Value2;
int Max;
if (Value1 < Value2) {
Max = Value2;
}
else {
Max = Value1;
}
cout << "Maximum of inputs is: " << Max << endl;
19. Finding the Max
Value1 < Value2
Max = Value2 Max = Value1
true f alse
Is Value2 larger than Value1
Yes, it is . So Value2 is
larger than Value1. In
this case, Max is set
to Value2
No, its not. So Value
is at least as large as
Value2. In this case
Max is set to Value1
Either case, Max is set
correctly
20. if-else-if Statement
if ( nbr < 0 )
{
cout << nbr << " is negative" << endl;
}
else if ( nbr > 0 )
{
cout << nbr << " is positive" << endl;
}
else
{
cout << nbr << " is zero" << endl;
}
25. Example of a Switch structure
Practice by converting the above switch … case statements to if else
statements.
26. Exercise
Write a C++ program, which will input the marks for quiz, test and final
examination. Assume the full mark for each one is 100. Calculate the
final mark that the student receive using the following formula:
Final mark = 0.6 * final examination + 0.2* (test + quiz).
Calculate the grade that the student obtains based on the following table.
Display the mark, grade and the appropriate message to the student.
Mark Grade Message
0-49
50-64
65-74
75-100
F
C
B
A
You fail the course. Try again next semester.
You pass the course, but more exercises are needed.
You pass the course, Good luck in the next level.
Congratulations, you are excellent in this course.