Decision: Statements & Relational Operators
Decision: Statements & Relational Operators
Decision: Statements & Relational Operators
IF
STATEMENTS
& RELATIONAL
OPERATORS
Control Structures
Two constructs
if statement
if
if-else
if-else-if
switch statement
Flow Chart Symbols
Start or stop
Process
Flow line
Continuation mark
Decision
The Basic if Statement
One Way Selection
Syntax
if(condition)
action false
condition
action is either a
action
statement
single or a group of
statements within braces.
If Statement
One Way Selection
If condition is true
statements
IF
Condition
Then
Process
If (condition)
statement ;
Choice (if)
If ( condition )
{
statement1 ;
statement2 ;
:
}
Compound (Block of) Statements
Syntax:
{
statement1
statement2
.
.
.
statementn
}
12
If statement in C++
Continued…
if Statement in Program 4-
2
Relational Operators
a != b;
X = 0;
X == 0;
Conditions
Examples:
10 > 20
20 * j == 10 + i
Example
#include <iostream>
using namespace std;
int main ( )
{
int AmirAge, AmaraAge;
AmirAge = 0;
AmaraAge = 0;
cout<<“Please enter Amir’s age”;
cin >> AmirAge;
cout<<“Please enter Amara’s age”;
cin >> AmaraAge;
if AmirAge > AmaraAge)
{
cout << “\n”<< “Amir’s age is
greater then Amara’s age” ;
}
return 0;
}
Operator Precedence
Answer: / %
*
+ -
< <= >= >
== !=
=
Logical Expressions
Associativity
Right to Left
Highest
Left to Right
Left to Right
Left to Right
Order of Precedence Left to Right
Left to Right
Lowest Left to Right
Right to Left
The Boolean Type
Binary operators
If a is greater than b
AND c is greater than d
In C++
if(a > b && c> d)
if(age > 18 || height > 5)
The Boolean Type
Operand !Operand
true false
false true
Logical Operators-Examples
(x <= z) || (y == z) false
(x <= z) || (y != z) true
Examples
5 != 6 || 7 <= 3
(5 !=6) || (7 <= 3)
int value1;
int
value2;
int temp;
cout << "Enter two integers: ";
cin >> value1 >> value2;
if(value1 > value2){
temp = value1;
value1 = value2;
value2 = temp;
}
cout << "The
input in sorted
order: "
Short-circuit Evaluation
if(value == 0)
cout << "value is 0";
else
cout << "value is not 0";
if-else
Two Way Selection
if (condition)
{
statement ;
-
-
}
else
{
statement ;
-
-
}
if-else
Entry point for IF-Else block
IF
Condition
Then
Process 1
Else
Process 2
int value1;
int
value2;
int larger;
cout << "Enter two integers: ";
cin >> value1 >> value2;
if(value1 > value2)
larger = value1;
else
larger =
value2;
cout << "Larger
of inputs is: "
<< larger <<
Finding the Big One
int value1;
bool even;
cout << "Enter a integer : ";
cin >> value;
if(value%2 == 0)
even = true;
else
even =
false;
// even = !
( value%2);
The if/else statement and Modulus Operator in
Program
Flowchart for Program Lines 14 through 18
Testing the Divisor in Program
Continued…
Testing the Divisor in Program
Example
If (AmirAge != AmaraAge)
cout << “Amir and Amara’s Ages
are not equal”;
Unary Not operator !
!true = false
!false = true
If (!(AmirAge > AmaraAge))
?
Example
if ((interMarks > 45) && (testMarks >= passMarks))
{
cout << “ Welcome to Virtual University”;
}
Example
?
The if/else if Statement
if (expression)
statement1; //
or block
else if (expression)
statement2; //
or block .
.
. // other else ifs
else if (expression)
statementn;
// or block
if-else-if
Statements
if <condition 1 exists>{
<do Q>
}
else if <condition 2 exists>{
Q
<do R>
}
R
else if <condition 3 exists>{
<do S>
S T
}
else{
<do T>
}
if-else-if
Statements
if <1PM or 7PM>{
<eat>
}
else if <Mon, Wed or Fri>{
<goto C++ Class>
}
else if <Tues or Thurs AM>{
<goto MATH Class>
}
else{
<sleep>
}
The if/else if Statement in Program
if-else-if
Statement
int people, apples, difference;
cout << "How many people do you have?\n";
cin >> people;
cout << "How many apples do you have?\n";
cin >> apples;
if(apples == people)
cout << "Everybody gets one apple.\n";
else if(apples > people){
difference = apples - people;
cout << "Everybody gets one apple,
& there are "
<< difference << " extra
apples.\n";}
else{
difference = people - apples;
cout << "Buy " << difference << " more
apples so that everyone gets one apple.\n";}
if-else-if
Example
int score;
cout << "Please enter a score: ";
cin >> score;
if (score >= 90)
cout << "Grade = A" << endl;
else if (score >= 80)
cout << "Grade = B" << endl;
else if (score >= 70)
cout << "Grade = C" << endl;
else if (score >= 60)
cout << "Grade = D" << endl;
else // totalscore < 59
cout << "Grade = F" << endl;
Nested if Statements
Multiple selections (nested if)
if ( grade ==‘A’ )
cout << “ Excellent ” ;
if ( grade ==‘B’ )
cout << “ Very
Good ” ;
if ( grade ==‘C’ )
cout << “ Good ” ;
if ( grade ==‘D’ )
cout << “ Poor ” ;
if ( grade ==‘F’ )
cout << “ Fail
”;
if else if
if ( grade ==‘A’ )
cout << “ Excellent ” ;
else
if ( grade ==‘B’ )
cout << “ Very
Good ” ;
else
if ( grade ==‘C’ )
cout << “ Good
”;
else
if ( grade ==‘D’ )
cout << “ Poor ” ;
if else if
if ( grade == ‘A’ )
cout << “ Excellent ” ;
else if ( grade == ‘B’ )
…
else if …
…
else …
switch statement
switch statements
multiway
expression
case 2
action
case 3
action
default
action
switch Statement: Example
1
• If you have a 95, what grade will you get?
switch(int(score)/10){
case 10:
case 9: cout << "Grade = A" << endl;
case 8: cout << "Grade = B" << endl;
case 7: cout << "Grade = C" << endl;
case 6: cout << "Grade = D" << endl;
default:cout << "Grade = F" << endl;
}
switch Statement: Example
2
switch(int(score)/10){
case 10:
case 9: cout << "Grade = A" << endl;
break;
case 8: cout << "Grade = B" << endl;
break;
case 7: cout << "Grade = C" << endl;
break;
case 6: cout << "Grade = D" << endl;
break;
default:cout << "Grade = F" << endl;
}
switch Statement: Example
2
is equivalent to:
if (score >= 90)
cout << "Grade = A" << endl;
else if (score >= 80)
cout << "Grade = B" << endl;
else if (score >= 70)
cout << "Grade = C" << endl;
else if (score >= 60)
cout << "Grade = D" <<
endl;
else // score < 59
cout << "Grade = F" <<
switch Statement: Example
2
#include <iostream>
using namespace std;
int main()
{ char answer;
cout << "Is Programing an easy course? (y/n): ";
cin >> answer;
switch (answer){
case 'Y':
case 'y': cout << "I think so too!" << endl;
break;
case 'N':
case 'n': cout << "Are you kidding?" << endl;
break;
default:
cout << "Is that a yes or no?" << endl;
}
return 0;
}
Points to Remember
switch (grade)
case ‘A’ :
Display
“Excellent”
case ‘B’ :
Display
“Very Good”
…
Default :
“……..”
The switch Statement in
Program
switch Statement
Requirements
1) expression must be an integer variable or an expression
that evaluates to an integer value
2) exp1 through expn must be constant integer expressions
or literals, and must be unique in the switch statement
3) default is optional but recommended
switch Statement-How it
Works
1) expression is evaluated
2) The value of expression is compared against exp1 through
expn.
3) If expression matches value expi, the program branches to
the statement following expi and continues to the end of the
switch
4) If no matching value is found, the program branches to the statement
after default:
break
Statement
Used to exit a switch statement
If it is left out, the program "falls through" the remaining statements in
the switch statement
break and default statements in
Program
Continued…
break and default statements in
Program
Using switch in Menu
Systems
switch statement is a natural choice for menu-driven
program:
display the menu
then, get the user's menu selection
use user input as expression in switch statement
use menu choices as expr in case statements
Converting if/else to a
switch
switch (rank)
{
if (rank == JACK) case JACK:
cout << "Jack"; cout << "Jack";
break;
else if (rank == QUEEN) case QUEEN:
cout << "Queen"; cout << "Queen";
break;
else if (rank == KING; case KING:
cout << "King"; cout << "King";
break;
else if (rank == ACE) case ACE:
cout << "Ace"; cout << "Ace";
break;
else default:
cout << rank; cout << rank;
}