Академический Документы
Профессиональный Документы
Культура Документы
http://eee241.gantep.edu.tr/
Lecture 3
Control structures:
Selection
Sayfa 1
Introduction
This lecture covers the following topics:
Relational and logical operators
Boolean expressions
The if structure
The if .. else structure
The if .. else if .. else structure
Example solved problems
Sayfa 2
Relational Operators
Control statements use relation operators to compare two objects.
There are six relational operators as follows:
comparison,
not assignment
Example:
if ( b != 0 ) c = a/b;
b0?
true
= a/b
false
Sayfa 3
Sayfa 4
Logical Operators
Compound relation expressions can be formed by logical operators:
Example:
b0
AND a>0
true
= a/b
false
Sayfa 5
X && Y
X || Y
(AND)
(OR)
true
true
true
false
true
false
true
true
false
false
true
false
false
false
true
false
Sayfa 6
Boolean Expressions
Expressions that evaluate to true or false are called Boolean.
We can form Boolean expressions inside control statements
(previous page) or in the form of assignments as follows:
int x=1, y=2, s=1;
bool u, z = true, t, w;
u = x > 3;
z = x <= y && y > 0;
t = y <= 0 || z;
w = !s;
s = 2 > 1;
Results
u = false
z = true
t = true
w = false
s = 1 = true
Example
bool ok;
ok = y != 0.;
if ( ok ) c = x/y;
<< endl;
<< endl;
cout <<
<< endl;
bad
Screen
output
Sayfa 8
The if structure
The if statement allows conditional execution; the general form is:
if (condition) {
statements
.
.
if (condition)
single-statement
single-statement
if structure
} else {
statements2
.
.
}
c = 1/(x+y);
cout << "c = " << c << endl;
} else {
Sayfa 10
double c, x, y;
cin >> x >> y;
c = 1 / (x+y)
if ( x+y != 0. ) {
c is undefined!
c = 1/(x+y);
cout << "c = " << c;
} else {
cout << "c is undefined!";
}
c = c
1 2
c = 0.333333
3 -3
c is undefined!
Sayfa 11
This is executed if
none of the above
conditions are true.
if (condition1) {
.
statements1
.
} else if (condition2) {
.
statements2
.
} else if (condition3) {
.
statements3
.
} else {
.
statements4
.
}
Sayfa 12
(classCode==1) {
cout << "Freshman." << endl;
} else if (classCode==2) {
cout << "Sophmore." << endl;
} else if (classCode==3) {
cout << "Junior." << endl;
} else if (classCode==4) {
cout << "Graduate." << endl;
} else {
cout << "Illegal code!" << endl;
}
Example outputs:
1
Freshman.
2
Sophmore.
3
Junior.
4
Graduate.
23
Illegal code!
The ? Operator
The ? operator (conditional expression operator) provides a
concise form of the if .. else structure.
The general form is:
( condition ) ? expression1 : expression2;
The value produced by this operation is either expression1 or
expression2 depending on condition being true or false.
Example:
max = ( x > y ) ? x : y;
is equivalent to
if ( x > y )
max = x;
else
max = y;
Sayfa 14
Examples
we can use these results to
validate our program
f(x) = a x2 + b x + c
The roots are the values of x
such that f(x) = 0.
i) (x-4)(x+2) = 0
when x = 4, x = -2
f(x) = x2 - 2 x 8
a = 1, b = -2, c = -8
Analytical solution:
x = 2/2 sqrt(36)/2
= 1 3 = 4 and -2
Three cases for the result b2 > 4ac
i)
ii)
iii)
b2 > 4ac
b2 = 4ac
b2 < 4ac
ii) (x-2)(x-2) = 0
when x=2
f(x) = x2 - 4 x + 4
a = 1, b = -4, c = -4
x = 4/2 sqrt(0)/2 = 2
Sayfa 15
#include <iostream>
#include <cmath>
using namespace std;
int main() {
double a, b, c;
cin >> a >> b >> c;
double Delta = b*b - 4*a*c;
if ( Delta < 0. ) {
cout << "The roots are imaginary!" << endl;
} else if ( Delta == 0. ) {
double x1 = -b / (2*a);
cout << "The root is " << x1 << endl;
} else {
double x1 = ( -b - sqrt(Delta) ) / (2*a);
double x2 = ( -b + sqrt(Delta) ) / (2*a);
cout << "The two roots are "<< x1<< " and " << x2<< endl;
Remember
the results:
}
i) (x-4)(x+2) = 0 when x = 4, x = -2 ii) (x-2)(x-2) = 0 when x=2
}
Sayfa 16
x
f(x) = 2
for
x<3
f(x) = 2x/3 for 3 x 6
f(x) = 4
for
x> 6
Sayfa 17
f(x) = 2
for
x<3
f(x) = 2x/3
for 3 x < 6
f(x) = 4
for
x6
Example outputs
input x: 0
f(0) = 2
#include <iostream>
using namespace std;
input x: 1
f(1) = 2
int main() {
input x: 2
f(2) = 2
double x, f;
cout << "input x: ";
cin >> x;
if
( x < 3. ) f = 2.0;
else if ( x < 6. ) f = 2.0/3.0*x;
else
f = 4.0;
input x: 3
f(3) = 2
input x: 4
f(4) = 2.66667
input x: 5
f(5) = 3.33333
input x: 6
f(6) = 4
return 0;
input x: 7
f(7) = 4
Sayfa 18
Finally
In this lecture we have looked at selection structures.
You can find the lecture slides in the course website:
http://eee241.gantep.edu.tr/
For further reading, search for the relevant sections at:
http://cpp.gantep.edu.tr/
and
http://www.learncpp.com/
Consider this as your course text book.
Sayfa 19
http://www1.gantep.edu.tr/~eee241/lectures.php
Sayfa 20
10