Chapter 3 - Control Structures - Selection
Chapter 3 - Control Structures - Selection
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
false true
?
• Selection
? => conditional
expression
false
?
• Repetition true
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
• if Statement
• if-else Statement
• switch Statement
Simplified Syntax
if (boolean_expression) if (boolean_expression) {
statement; statement1;
…
statement_n;
}
Examples
if (x>0)
++k; //statement executed iff x>0
Syntax
if (boolean_expression) if (boolean_expression) {
statement; …
[else } [else {
statement;] …
}]
Example
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
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.
}