2. ndamentals I: If Statements & Relational Operators /Slide 2
Conditional Statements
A conditional statement allows us to control
whether a program segment is executed or
not.
Two constructs
if statement
– if
– if-else
– if-else-if
switch statement
3. ndamentals I: If Statements & Relational Operators /Slide 3
The Basic if Statement
Syntax
if(condition)
action
if the condition is true then
execute the action.
action is either a single
statement or a group of
statements within braces.
condition
action
true
false
4. ndamentals I: If Statements & Relational Operators /Slide 4
Choice (if)
Put multiple action statements
within braces
if (it's raining){
<take umbrella>
<wear raincoat>
}
5. ndamentals I: If Statements & Relational Operators /Slide 5
Absolute Value
// program to read number & print its absolute value
#include <iostream>
using namespace std;
int main(){
int value;
cout << "Enter integer: ";
cin >> value;
if(value < 0)
value = -value;
cout << "The absolute value is " << value << endl;
return 0;
}
6. ndamentals I: If Statements & Relational Operators /Slide 6
Relational Operators
Relational operators are used to compare two values to
form a condition.
Math C++ Plain English
= == equals [example: if(a==b) ]
[ (a=b) means put the value of b into a ]
< < less than
≤ <= less than or equal to
> > greater than
≥ >= greater than or equal to
≠ != not equal to
8. ndamentals I: If Statements & Relational Operators /Slide 8
Operator Precedence
Which comes first?
* / %
+ -
< <= >= >
== !=
=
Answer:
9. ndamentals I: If Statements & Relational Operators /Slide 9
The Boolean Type
C++ contains a type named bool for conditions.
A condition can have one of two values:
true (corresponds to a non-zero value)
false (corresponds to zero value)
Boolean operators can be used to form more complex conditional
expressions.
The and operator is &&
The or operator is ||
The not operator is !
10. ndamentals I: If Statements & Relational Operators /Slide 10
The Boolean Type
Truth table for "&&" (AND):
Operand1 Operand2 Operand1 &&
Operand2
true true true
true false false
false true false
false false false
11. ndamentals I: If Statements & Relational Operators /Slide 11
The Boolean Type
Truth table for “||" (OR):
Operand1 Operand2 Operand1 ||
Operand2
true true true
true false true
false true true
false false false
12. ndamentals I: If Statements & Relational Operators /Slide 12
The Boolean Type
Truth table for "!" (NOT):
Operand !Operand
true false
false true
13. ndamentals I: If Statements & Relational Operators /Slide 13
A Boolean Type
Assignments to bool type variables
bool P = true;
bool Q = false;
bool R = true;
bool S = P && Q;
bool T = !Q || R;
bool U = !(R && !Q);
14. ndamentals I: If Statements & Relational Operators /Slide 14
More Operator Precedence
Precedence of operators (from highest to lowest)
Parentheses ( … )
Unary operators !
Multiplicative operators * / %
Additive operators + -
Relational ordering < <= >= >
Relational equality == !=
Logical and &&
Logical or ||
Assignment =