0% found this document useful (0 votes)
31 views37 pages

12 - Control Structure

Operators and Control Structures discusses relational and logical operators, and control structures like selection and iteration. Relational operators compare values, logical operators combine conditions, and precedence determines order of operations. Selection structures like if statements allow conditional execution of code. Iteration structures like loops allow repetitive execution of code. Loops are useful when the same operations need to be repeated on changing values to avoid duplicating code.
Copyright
© Attribution Non-Commercial (BY-NC)
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)
31 views37 pages

12 - Control Structure

Operators and Control Structures discusses relational and logical operators, and control structures like selection and iteration. Relational operators compare values, logical operators combine conditions, and precedence determines order of operations. Selection structures like if statements allow conditional execution of code. Iteration structures like loops allow repetitive execution of code. Loops are useful when the same operations need to be repeated on changing values to avoid duplicating code.
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 37

Operators and Control Structures

On the menu today


I- Quick review II- Relational & logical operators III- 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

II- Relational & logical operators

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

A- Relational operators: example


Relational expressions
- (4 < 3) - (firstInitial != 'A') - (hoursWorked > 40) - (pollutionIndex >= cutoff) - (age <= 50) - (classLevel == 'F') less than not equal greater than greater than or equal less than or equal equal

B- Logical operators
Goal: Create more complex conditions && and || or ! not

B- Logical operators: Example


Logic expressions
(age > 40) && (height > 1.75) (age > 40) || (height > 1.75) || (weight > 80) ! ((age > 40) && (height > 1.75))

C- Operators precedence
left to right

*
<

+ <= > >= == != &&

||

C- Expression value and interpretation


relational and logical expression take
value interpreted in

1 true

0 false

C- Expression value and


interpretation: Example
int i = 5, j = 7, k = 12; Expression equivalent value interpretation expression i+1==k+1 (i+1)==(k+1) 0 false 3*1-j<22 ((3*1)-j)<22 1 true i+2*j>k (i+(2*j))>k 1 true k+3<=-j+3*i (k+3)<=(-j)+(3*i) 0 false

C- Expression value and interpretation: Example

(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

II- Program Control Structure

Lecture 4

A- Syntax form
// Syntax form of a C++ program # include <iostream.h> definitions, declarations and functions void main() { declarations and statements }

B- Control Structure: what for?


We can think of a program as a set of statements that manipulate variables.

Goal: Control the order of the

statements
How to control the order in which those
statements execute?

B- Control Structure: 3 Types



Sequences: By default, they execute sequentially Selection : Condition - Decision We can force them to execute selectively conditionally Iteration: Loop We can force them to execute iteratively

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 ?

Yes, the statements are executed in sequence.

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

- The condition states a question whose answer is Yes/True or No/False.

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;

C- Selection - if Statement : two


types
N Y
if (condition)

condition

Statements

{
statements; }

C- Selection - if Statement : two types


N

condition

if (condition)

{ statements;
Statements

Statements

} else { statements;
}

D- Iteration: why we need loops


Let's write a program to print out a chart of the first 10 positive integers and their squares.

D- Iteration: why we need loops


#include <iostream.h> void main() { cout << 1 << cout << 2 << cout << 3 << cout << 4 << cout << 5 << cout << 6 << cout << 7 << cout << 8 << cout << 9 << cout << 10 << Lecture 4 }
We are not making the computer works

1 4 9 16 25 36 49 64 81 100

<< << << << << << << << << <<

endl; endl; endl; endl; endl; endl; endl; endl; endl; endl;

D- Iteration: why we need loops

Observations?

D- Iteration: why we need loops


Modified version:
#include <iostream.h> void main() { int x = 1; cout << x << cout << x << cout << x << cout << x << cout << x << cout << x << cout << x << cout << x << cout << x << cout << x << }

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;

D- Iteration: why we need loops


Observations?
We are repeating exactly the same two statements ten times.

x 0; loop while x < 10 add 1 to x report x and x2 end loop

Alternative?

D- Iteration: why we need loops


Observations? a) we have found the pattern and only written that b) we have let the computer do the work

Questions?

You might also like