Lecture 10 - Control Structures Pt 2.pdf
Transcript of Lecture 10 - Control Structures Pt 2.pdf
-
8/14/2019 Lecture 10 - Control Structures Pt 2.pdf
1/11
1992-2012 by Pearson Education, Inc. & John Wiley & SonsSome portions are adopted from C++ f or Everyone by Horstmann
ENGR 1200U Introduction to Programming
Lecture 10
Control Structures: Selection (Chapter 3) (contd)
Dr. Eyhab Al-Masri
ENGR 1200U
Winter 2013 - UOIT
What is a top down design?
Begin with a big picture description of a problem solutionin sequential steps.
Sequential steps are refined until the steps are detailed
enough to translate to language statements
Refined steps can be described using pseudocode orflowcharts
-
8/14/2019 Lecture 10 - Control Structures Pt 2.pdf
2/11
ENGR 1200U
Winter 2013 - UOIT
What is a structured program?
A program that is written using simple control structuresincluding:
Sequence Selection
Repetition
ENGR 1200U
Winter 2013 - UOIT
What is a ConditionalExpression?
A Boolean expression that evaluates to true or false
Example
if (x>y)
{...
}
conditional expression
-
8/14/2019 Lecture 10 - Control Structures Pt 2.pdf
3/11
ENGR 1200U
Winter 2013 - UOIT
What is the differencebetween = and == operators?
= is called an assignment operator. It is used to assignvalues to identifiers.
== denotes equality testing (tests whether an identifier isequal to a given value or expression)
intc(10);
if (c
=
5){...}
intc(10);
if (c
==
5){...}
ENGR 1200U
Winter 2013 - UOIT
What does the following expression mean
(a > 5) && (y < 10)
This is an example of logical expressions. They are used tomake assertions. Also called Boolean expressions.
Three logical operators: ! && ||
A B A&&B A||B A Bfalse false false false true true
false true false true true false
true false false true false true
true true true true false false
Short-circuit evaluation?
-
8/14/2019 Lecture 10 - Control Structures Pt 2.pdf
4/11
ENGR 1200U
Winter 2013 - UOIT
Can we combinemultiple if
statements toevaluate complex
decisions?
Yes, we can.
ENGR 1200U
Winter 2013 - UOIT
Consider the following UOIT GPA Grade Scale
How manybranches
do wehave?
5 or 6? For simplicity and to minimize number of branches inthis example, we will assume that
grade < 73 is passing but marginal
-
8/14/2019 Lecture 10 - Control Structures Pt 2.pdf
5/11
ENGR 1200U
Winter 2013 - UOIT
Grade>= 90
Grade>= 85
Grade>= 80
Grade>= 77
Grade>= 73
A+(4.3 Grade Points)
A(4.0 Grade Points)
A-(3.7 Grade Points)
B+(3.3 Grade Points)
B(3.0 Grade Points)
Pass, Marginal
true
true
true
true
true
false
false
false
false
false
ENGR 1200U
Winter 2013 - UOIT
if (grade>=90)
{
cout
-
8/14/2019 Lecture 10 - Control Structures Pt 2.pdf
6/11
ENGR 1200U
Winter 2013 - UOIT
if (grade>=90)
{
cout=73)
{
cout
-
8/14/2019 Lecture 10 - Control Structures Pt 2.pdf
7/11
ENGR 1200U
Winter 2013 - UOIT
Nested if statements: if statements inside other ifstatements
Exampleif (grade>=80)
{
if (grade>=90)
{
cout
-
8/14/2019 Lecture 10 - Control Structures Pt 2.pdf
8/11
ENGR 1200U
Winter 2013 - UOIT
Exampledoubleship_rate=5.00;//standardshippingacrossCanada
if (Country==Canada)
if (Province==PEI)
ship_rate=9.00;
else
ship_rate=30.00;
This is actually how thecode is executed
This is not what we want.This is thedangling else problem
Sowhatisthesolution?
ENGR 1200U
Winter 2013 - UOIT
Exampledoubleship_rate=5.00;//standardshippingacrossCanada
if (Country==Canada)
{
if (Province==PEI)
ship_rate=9.00;
}
elseship_rate=30.00;
Solution:Simple,putonestatementinablock{}
-
8/14/2019 Lecture 10 - Control Structures Pt 2.pdf
9/11
ENGR 1200U
Winter 2013 - UOIT
The switch statement is used for multipleselection decision making
Exampleint digit;
...
if (digit==1){digit_name="one";}
else if (digit==2){digit_name="two";}
else if (digit==3){digit_name="three";}
else if (digit==4){digit_name="four";}
else if (digit==5){digit_name="five";}
else if (digit==6){digit_name="six";}
else if (digit==7){digit_name="seven";}
else if (digit
==
8)
{
digit_name
=
"eight";
}else if (digit==9){digit_name="nine";}
else {digit_name="";}
Thisisabitofamess!
ENGR 1200U
Winter 2013 - UOIT
Exampleint digit;
...
switch (digit)
{
case 1:digit_name="one";break;
case 2:digit_name="two";break;
case 3:digit_name="three";break;
case 4:digit_name="four";break;
case 5:digit_name="five";break;
case 6:digit_name="six";break;
case 7:
digit_name
=
"seven";
break;case 8:digit_name="eight";break;
case 9:digit_name="nine";break;
default:digit_name="";break;
}
Moreorganized
-
8/14/2019 Lecture 10 - Control Structures Pt 2.pdf
10/111
ENGR 1200U
Winter 2013 - UOIT
Syntaxswitch (control_expression) {
case constant:statement(s);break;
[ case constant:statement(s);break;
[] ][ default:
statement(s); ]
}
Control expression must
be an integral type (e.g.
char, int, etc)
Break statements are not
required; without a break
statement execution runs
through other case labels.
ENGR 1200U
Winter 2013 - UOIT
Every branch of the switch must be terminated by abreak statement
If the break is missing, execution falls through to thenext branch, and so on, until finally a break or the
end of the switch is reached
If you accidentally forget the break statement, your
program compiles but executes unwanted code
-
8/14/2019 Lecture 10 - Control Structures Pt 2.pdf
11/111
ENGR 1200U
Winter 2013 - UOIT
Activity TemperatureSwimming Temperature > 85
Tennis 70 < temperature