0% found this document useful (0 votes)
17 views34 pages

Chapter 3 - Control Structures - Selection

Uploaded by

johnmf1011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views34 pages

Chapter 3 - Control Structures - Selection

Uploaded by

johnmf1011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 34

©2017 Pearson Education, Inc.

Hoboken,
NJ. All Rights Reserved.

Chapter 3
Control Structures:
Selection
Outline
Objectives
1. Algorithm Development
2. Structured Programming
3. Conditional Expressions
4. Selection Statements: if Statement
5. Numerical Technique: Linear Interpolation
6. Problem Solving Applied: Freezing Temperature of
Seawater
7. Selection Statements: switch Statement
8. Defining Operators for Programmer-defined data types

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Objectives
Develop problem-solving solutions in
C++ containing:
• Conditional expressions that evaluate to
true or false.
• Selection structures that allow us to provide
alternative paths in a program.
• Algorithm development and descriptions of
algorithms using flowcharts and
pseudocode.
©2017 Pearson Education, Inc. Hoboken, NJ. All
Rights Reserved.
Algorithm Development
• An algorithm is a sequence of steps for
solving a problem.
• Engineering problem solutions to real
world problems require complex
algorithms.
• Development of a good algorithm
increases the quality and maintainability of
a solution, and reduces the overall time
required to implement a correct solution.
©2017 Pearson Education, Inc. Hoboken, NJ. All
Rights Reserved.
Top-Down Design
• Top-down design begins with a "big
picture" description of a problem solution
in sequential steps.
• The sequential steps are refined until the
steps are detailed enough to translate to
language statements.
• The refined steps, or algorithm, can be
described using pseudo code or
flowcharts.
©2017 Pearson Education, Inc. Hoboken, NJ. All
Rights Reserved.
Pseudocode
Notation
and
Flowchart
Symbols

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Structured Programming
• A structured program is written using
simple control structures, including:
– Sequence – steps are performed one after
another.
– Selection – one set of statements is executed if
a given condition is true, a different set of
statements, or no statements at all, is executed if
the condition is false.
– Repetition –A set of statements is executed
repeatedly as long as a given condition is true.
©2017 Pearson Education, Inc. Hoboken, NJ. All
Rights Reserved.
Structured Programming
• Sequence

false true
?
• Selection
? => conditional
expression
false
?
• Repetition true

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Conditional Expressions
• A conditional expression is a
Boolean expression that evaluates to true
or false.
• Selection structures and repetition
structures rely on conditional expressions.
• Relational operators and logical
operators are used to form conditional
expressions.
©2017 Pearson Education, Inc. Hoboken, NJ. All
Rights Reserved.
Relational Operators
 == equality
 != non equality
< less than
> greater than
 <= less than equal to
 >= greater than equal to
©2017 Pearson Education, Inc. Hoboken, NJ. All
Rights Reserved.
Logical Operators
! not
 && and
 || or

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Logical Operators

A B A&&B A||B !A !B
false false false false true true
false true false true true false
true false false true false true
true true true true false false

Truth table for conditional expressions


©2017 Pearson Education, Inc. Hoboken, NJ. All
Rights Reserved.
Short Circuiting
• C++ only evaluates as much of an
expression as necessary to evaluate it.
– E.g. if A is false, A && B is always false,
regardless of the value of B.
– E.g. if A is true, A || B is always true,
regardless of the value of B.

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Operator Precedence
Precedence Operator Associativity
1 () Innermost First
2 Unary operators: + - ++ -- ! (type) Right to left
3 * / % Left to right
4 + - Left to right
5 < <= > => Left to right
6 == != Left to right
7 && Left to right
8 || Left to right
9 = += -= *= /= %= Right to left

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Selection Statements:

• if Statement
• if-else Statement
• switch Statement

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
The if Statement

Simplified Syntax
if (boolean_expression) if (boolean_expression) {
statement; statement1;

statement_n;
}
Examples
if (x>0)
++k; //statement executed iff x>0

if (x>0) { //statement block executed iff x>0


x = sqrt(x);
++k;
}
©2017 Pearson Education, Inc. Hoboken, NJ. All
Rights Reserved.
The if-else Statement

Syntax
if (boolean_expression) if (boolean_expression) {
statement; …
[else } [else {
statement;] …
}]
Example

if (x>0) { //statement block executed iff x>0

} else { //statement block executed iff x<=0

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Avoiding Bugs
• To avoid confusion and possible
errors when using if/else statements, you
should use {} to clearly define statement
blocks.
• Do not use == with real values
– Instead of x==3.14, use
fabs(x-3.14)<0.0001
• Do not confuse the equality operator with the
assignment operator!
©2017 Pearson Education, Inc. Hoboken, NJ. All
Rights Reserved.
Numerical Technique:
Linear Interpolation

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Why Linear Interpolation?
• Collecting data in a computer usually
results in a collection of samples.
• Often, we are interested in what happens
‘in between’ the sample points.
– i.e. we need to interpolate between the
samples.
• Linear and cubic spline interpolation
common techniques.

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Interpolation Example

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Linear Interpolation

f(a), a, f(c), and c


are known. We
wish to estimate
f(b) where
a < b < c.

Linear
interpolation is a
straight-line
approximation
between the
known points.
©2017 Pearson Education, Inc. Hoboken, NJ. All
Rights Reserved.
Problem Solving Applied:
Freezing Temperature
of Seawater

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
©2017 Pearson Education, Inc. Hoboken, NJ. All
Rights Reserved.
©2017 Pearson Education, Inc. Hoboken, NJ. All
Rights Reserved.
©2017 Pearson Education, Inc. Hoboken, NJ. All
Rights Reserved.
The switch Statement

Syntax
switch (control_expression) {
• Control expression must
case constant: be an integral type (e.g.
statement(s); char, int, etc…)
break;
[ case constant:
• Break statements are not
statement(s); required; without a break
break; statement execution ‘runs
[…] ]
[ default:
through’ other case
statement(s); ] labels.
}

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Example
char ch;
int ecount=0, vowels=0, other=0;
cin.get(ch);
while(!cin.eof()) {
switch(ch) {
case ‘e’: ecount++;
case ‘a’:
case ‘i’:
case ‘o’:
case ‘u’: vowels++;
break;
default: other++;
} //end switch
cin.get(ch);
} //end while
cout << ecount << ‘,’ << vowels << ‘,’ << other << endl

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Currency Conversion
#include <stdlib.h>
#include <iostream>
using namespace std;
/*-----------------------------------------------------------------*/
/* Program chapter3 _3
*
* This program performs currency conversion from dollars to
* E => euros
* P => pesos
* S => pounds sterling
*/
int main(int argc, char** argv)
{
double dollars, equivalentCurr;
char currencyCode;
const double ECONVERSION{0.7041}, PCONVERSION{11.6325},
SCONVERSION{0.6144};
//Prompt user for input
cout << "enter dollar amount" << endl;
cin >> dollars;
cout << "enter currency code:\n"
<< "E => Euros\nP => Mexican Pesos\nS => British Pounds Sterling\n" ;
cin >> currencyCode;

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
switch(toupper(currencyCode))
{
case 'E':
cout << "converting dollars to euros..\n" ;
equivalentCurr = dollars*ECONVERSION
break;
case 'P':
cout << "converting dollars to pesos..\n" ;
equivalentCurr = dollars*PCONVERSION;
break;
case 'S':
cout << "converting dollars to pounds sterling..\n" ;
equivalentCurr = dollars*SCONVERSION;
break;
default:
cout << currencyCode << "not supported at this time\n" ;
equivalentCurr = dollars;
}//end switch
cout << "Equivalent amount: "<< equivalentCurr << endl;
return (EXIT _SUCCESS);
}//end main

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Defining Operators for
Programmer-defined
Data Types

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Operators and Programmer-
defined Types
• Only the assignment operation is
defined for programmer-defined types.
• The programmer may overload operators
by providing a new definition of an existing
operator to work with programmer-defined
types.
– Operators may not be overloaded for
standard C++ data types.

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
Overloaded Operators for
the Point Class
//Point class declaration: Point.h
double operator-(const Point& rhs) const;
bool operator ==(const Point& rhs) const;

©2017 Pearson Education, Inc. Hoboken, NJ. All


Rights Reserved.
//Point class implementation: Point.cpp
double Point::operator -(const Point& rhs) const
{
double diffX, diffY, distance;
diffX = rhs.xCoord - xCoord; //(x2-x1)
diffY = rhs.yCoord - yCoord; //(y2-y1)
distance = sqrt( pow(diffX,2) + pow(diffY,2) );
return distance;
}

bool Point::operator ==(const Point& rhs) const


{
if(rhs.xCoord == xCoord &&
rhs.yCoord == yCoord )
{
return true;
}
else
{
return false;
}
}
©2017 Pearson Education, Inc. Hoboken, NJ. All
Rights Reserved.

You might also like