Selection1

advertisement
Rational Expressions
and selection structures
Relational operators
 Logical operators
 Selection structures

We have seen normal expressions
Any combination of variables, constants
and functions that can be evaluated to
yield a result
Typically involve operators
Relational Expressions
compare operands
 used in decision making, in
controlling the flow of your programs
 evaluate to 1 (true) or 0 (false)
 Note any expression x = 2 evaluates
to value returned.
 Any non zero value is deemed true

 Show example if (0.000001)
Relational Operators
less than
greater than
less than or equal to
greater than or equal to
a-b<0
is equivalent to
<
>
<=
>=
(a - b) < 0
Relational Expressions
Operand
price
Relational
operator
<
Operand
expression
34.98
Values of Relational
Expressions
a-b
a<b
a>b
a <=b
a >=b
Positive
0
1
0
1
Zero
0
0
1
1
Negative
1
0
1
0
Relational Operators Examples
Valid
a<3
a>b
-1.1 >= (2.2 * x + 3.3)
a<b<c
// syntactically correct but confusing
a =< b
a<=b
a >> b
Not Valid
// out of order
// space not allowed
// shift operation
Equality Operators
Equal to
==
Not Equal to
!=
Note this!
Values of Equality Expressions
a-b
a == b
a !=b
Zero
1
0
Non zero
0
1
Equality Operators Examples
c == 'A'
k != -2
y == 2 * z - 5
a=b
a==b-1
y =! z
Valid
Not Correct
// assignment statement
// space not allowed
// this is equivalent to y = (!z)
Programming Technique
Allowing for numerical Accuracy
Many decimal numbers cannot be exactly
represented in binary by a finite
number of bits. Thus testing for exact
equality can fail.
Use the technique:
|operand1 - operand2| < epsilon
Ex.
x/y == 17
fabs(x/y - 17) < 0.000001
More Logical Operators
Negation (unary)
!
Logical and
&&
Logical or
||
Logical Operators: Examples
a && b
a || b && c
!(a < b) && c
3 && (-2 * a + 7)
a &&
a||b
a&b
&b
//
//
//
//
Valid
Not Valid
one operand missing
extra space not allowed
this is a bitwise operation
the address of b
Logical Operators: Examples
int a = 0, b = 3, c = 1, d =4;
a && !c || d
F
F
1
2
3
T
Logical Operators: Examples
int a = 0, b = 3, c = 1, d =4;
a && b || !c || d
F
F
1
2
F
3
4
T
Truth Tables for &&, ||, !
a
b
a || b
a && b
!a
0
0
0
0
1
0
1
1
0
1
1
0
1
0
0
1
1
1
1
0
Common Errors!
==
means equality
=
used for assignment
FALSE is zero
TRUE is nonzero
Boolean operators give a Boolean result
Control Structures
Decisions or Selections or Branches
if
switch
? operator
Two types of IF
Simple single alternative if
 Compound multi-alternative
if...else

Single Alternative Decision
An action is taken if the condition is true,
otherwise the control goes to the next
statement.
Single Alternative Decision
no ;
Syntax
if (expression) {
statement
}
If expression is true, statement is executed;
otherwise statement is skipped.
note: 2 = signs
Example:
if (stomach == empty) {
eat a mars bar;
}
Single Alternative Decision
Syntax
if (expression) {
statement
}
Recall that an expression is any combination of
variables, constants, or function calls that
evaluate to a value.
e.g.
N
5
x+y a=3+j
n++
f(12.3, a, “Yvonne”)
Single Alternative Decision
Example:
if (grade
cout <<
cout <<
cout <<
}
>= 90){
Congratulations!\n”;
“Your grade is “;
grade << “.\n";
Good Practice:
Always add braces to if
control structures
note braces
The nested if Statement
Syntax
if (expression) {
statement;
statement;
if (expression){
statement;
statement;
}
}
Example:
if (u > v) {
a = 1;
b = 2;
if ( u > z) {
x =11;
y = 12;
}
}
The nested if statement is itself an if statement.
Nested if Example
if (number == secretnumber){
cout << “You guessed it!”;
}
if (number != secretnumber){
cout << “that’s not the number.\n”;
if (number > secretnumber){
cout << “You guessed too high.\n”;
}
else {
cout << “You guessed too low.\n”;
}
}
Examples
Valid:
Not Valid:
if (y != 0.0)
z = x/y;
if b == a
area = a
if (a < b && b < c) {
d = a + b + c;
cout << "All OK\n";
}
*
a;
if (a < b) && (b < c)
if (a < b) ;
Valid But... semi
colon!
if (a < b) ;
if Problems
Using = in place of ==
What is the difference between these two?
if (toss == 7)
cout << “You win the bet.”;
if (toss = 7)
cout << “You win the bet.”;
Chris demonstrated using debugger
If problems
not adding braces.
Demonstrate this Chris
if (grade > 70)
cout << “Well done << endl;
cout << “You got a first”;
This line will always be executed
Double Alternative Decision
using if … else structure
An action (or set of actions) is taken if the
condition is true, another action (or set
of actions) is taken if the condition is
false, then the control goes to the next
statement.
The if-else Statement
Syntax
if (expression) {
statement(s)1
}
else {
statement(s)2
}
If expression is nonzero then statement1 is executed
and statement2 is skipped. If expression is zero
statement1 is skipped and statement2 is executed.
if ... else Examples
if (stomach == empty){
eat a pizza;
eat a mars bar;
}
else {
eat a salad;
}
if … else example
if ( j
min =
k = k
}
else {
min =
j = j
}
< k ){
j;
* 3;
k;
* 3;
Application:
three values
Finding the minimum of
Finding the Minimum of Three Values
int x, y, z, min;
cout << “Input three integers:
“;
cin >> x >> y >> z;
if (x < y){
min = x;
}
else{
min = y;
Demonstrate this Chris
}
if (z < min){
min = z;
}
cout << “The minimum value is “ << min <<‘\n’;
Chained if...else Example
Syntax
if (expression1){
statement1
}
else if (expression2) {
statement2
}
. . .
else if (expressionN) {
statementN
}
else {
last statement
}
next statement
Application of Chained if...else statements
if (total >=70) {
grade = ‘A’;
}
else if (total >= 60){
grade = ‘B’;
}
else if (total >= 50){
grade = ‘C’;
{
else if (total >= 40){
grade = ‘D’;
{
else {
grade = ‘F’;
}
next statement
The Dangling else which if does the
else belong to?
if (avg >= 40.0)
if (avg < 50.0)
cout << “Passing, but marginal”;
else
cout << “Failing”;
if (avg >= 40.0) {
if (avg < 50.0)
cout << “Passing,but marginal”;
}
else
cout << “Failing”;
Note good indentation will help you and use braces
even when only one statement!
Review
Relational expressions are used to compare


if result is true expression evaluates to 1
if result is false expression evaluates to 0
other expression that evaluate to non zero are
deemed true
More complex conditions using && || and !
use if…else to choose between two alternatives
nested if statements contain another if in body
get into habit of always using braces even when only
one statement is to be executed.
multi-way selection using if-else chains.
watch out for = and == confusion
Download