12 - Control Structure
12 - Control Structure
I- Review
A- Variables declaration
What is required? Three rules to declare a variable Good habits for a variable name
B- Variables assignment
How can we get a value into this variable 1variable = expression; it means: variable expression
(value of the expression assigned to the variable)
C- Types of expression
Provide the different types of expression: - numeric constant
- another variable - arithmetic expression operand operator operand
A- Relational operators
Goal: Compare or Write conditions
Relational expression: operand operator operand
Different operators < less than > greater than <= less than or equal to >= greater than or equal to == equal to != not equal to
B- Logical operators
Goal: Create more complex conditions && and || or ! not
C- Operators precedence
left to right
*
<
||
1 true
0 false
(6 * 3 ==36 / 2) ||(13 < 3 * 3 +4 ) && !( 6- 2 <5 ) (18 ==18) ||(13 < 9 +4 ) && !( 4 <5 ) 1 ||(13 < 13 ) && !1 1 || 0 && 0 1
Lecture 4
A- Syntax form
// Syntax form of a C++ program # include <iostream.h> definitions, declarations and functions void main() { declarations and statements }
statements
How to control the order in which those
statements execute?
C- Sequences
# include <iostream.h>
void main() { float a, b, c; a = 12.0; b = 15.0; c = (a + b) / (a - b);
cout << c;
# include <iostream.h>
void main() { float a, b, c; b = 15.0; c = (a + b) / (a - b); a = 12.0; cout << c; }
C- Sequences
Does the order of statements matter ?
C- Selection
- Sometimes we only want to execute a statement under certain conditions. - That is, we want to selectively execute a statement or a series of statements.
C- Selection
-We informally think of this as an "if...then" situation:
if (condition)
statements to execute if condition is true
else
statements to execute if condition is false
C- Selection
- Flow diagrams are good for capturing this idea: N
Statement to be executed when condition is false
condition
Y
Statement to be executed when condition is true
C- Selection: example
if hours worked is greater than 40 compute regular hours as 40 compute overtime hours as hours worked - 40 compute earnings as regular hours * wage rate plus overtime hours * 1.5 * wage rate otherwise compute earnings as hours worked * wage rate
C- Selection: example
N
hrsworked > 40
?
earnings hrsworked * wageRate reghours 40 othours hrsworked -40 earning reghours *wageRate + othours*1.5* wageRate
C- Selection: example
Scenario: Three pollution levels called reading1, reading2, and reading3. An air pollution index is defined as the average of these readings. Any air pollution index less than the cutoff means air quality is safe; otherwise it is hazardous. Assume values already in all four variables: reading1,reading2, reading3, and cutoff.
C- Selection: example
Example: float Index; Index = (reading1+ reading2+ reading3)/3; if (Index < cutoff) cout << Safe; else cout << Hazardous;
condition
Statements
{
statements; }
condition
if (condition)
{ statements;
Statements
Statements
} else { statements;
}
1 4 9 16 25 36 49 64 81 100
<< << << << << << << << << <<
endl; endl; endl; endl; endl; endl; endl; endl; endl; endl;
Observations?
x*x x*x x*x x*x x*x x*x x*x x*x x*x x*x
<< << << << << << << << << <<
endl; endl; endl; endl; endl; endl; endl; endl; endl; endl;
Alternative?
Questions?