Programming: Statements & Relational Operators

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 19

Programming

If Statements &
Relational
Operators
COMP102 Prog Fundamentals 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
COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 3

If statement

 Decision making structures require that


the programmer specify one or more
conditions to be evaluated or tested by the
program, along with a statement or
statements to be executed if the condition
is determined to be true, and optionally,
other statements to be executed if the
condition is determined to be false.

COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 4
COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 5

Nested if
 In case the body of the if statement lies in
entirity inside another if statemt the structure
is called nest if.
if(condition/s)
{
if(condition)
{
// body of internal if
}
}
COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 6

The Basic if Statement

 Syntax
if(condition)
false
action condition

 if the condition is true then


execute the action. true

 action is either a single action


statement or a group of
statements within braces.
COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 7

Choice (if)

 Put multiple action statements


within braces

if (it's raining){
<take umbrella>
<wear raincoat>
}
COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 8

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;
}
COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 9

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
COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 10

Conditions
Examples:

Number_of_Students < 200

10 > 20

20 * j == 10 + i
COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 11

Operator Precedence
Which comes first?

Answer: * / %
+ -
< <= >= >
== !=
=
COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 12

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 !
COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 13

The Boolean Type


 Truth table for "&&" (AND):
Operand1 Operand2 Operand1 &&
Operand2
true true true

true false false

false true false

false false false


COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 14

The Boolean Type


 Truth table for “||" (OR):
Operand1 Operand2 Operand1 ||
Operand2
true true true

true false true

false true true

false false false


COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 15

The Boolean Type


 Truth table for "!" (NOT):

Operand !Operand

true false

false true
COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 16

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);
COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 17

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 =
COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 18

More Operator Precedence

 Examples
5 != 6 || 7 <= 3

(5 !=6) || (7 <= 3)

5 * 15 + 4 == 13 && 12 < 19 || !false == 5 < 24


COMP102 Prog Fundamentals I: If Statements & Relational Operators /Slide 19

Sorting Two Numbers


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: "
<< value1 << " " << value2 << endl;

You might also like