Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Lecture 3 Relational Operators

The document covers various types of operators in computer programming, including relational, logical, equality, conditional, and bitwise operators, along with their examples and usage. It also discusses control structures like if, if-else, and switch statements, explaining their syntax and functionality in C++. Additionally, it touches on type conversion, operator precedence, and escape sequences.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 3 Relational Operators

The document covers various types of operators in computer programming, including relational, logical, equality, conditional, and bitwise operators, along with their examples and usage. It also discusses control structures like if, if-else, and switch statements, explaining their syntax and functionality in C++. Additionally, it touches on type conversion, operator precedence, and escape sequences.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Computer Programming

Operators

Week 03 Sp 23

Acknowledgement – Ms. Nabia Khalid worked on the slides


Relational Operators
• Examples
 (7 == 5) would return false
 (3 != 2) would return true
 (6 >= 6) would return true
• If a=2, b=3 and c=6
 (a*b >= c) would return true since it is (2*3 >= 6)
 (b+4 > a*c) would return false since it is (3+4 > 2*6)
 ((b=2) == a) would return true
Relational Operators – Characters
(char)
• C++ allows character comparison using relational and equality
operators.

• During comparison alphabetical order is followed. (ASCII: American


Standard Code for Information Interchange ).

• Examples
• ‘a’ < ‘e’ // True
Equality (==) and Assg (=)
Operators
• Common error : Does not cause syntax errors
• Example if ( payCode == 4 )
cout << "You get a bonus!“;

• If == was replaced
if with =
( payCode = 4 )
cout << "You get a bonus!“;

• PayCode set to 4 (no matter what it was before)


• Statement is true (since 4 is non-zero)
• Bonus given in every case
Logical Operators
• Logical expressions - expressions that use conditional statements and
logical operators.
• && (And)
• A && B is true if and only if both A and B are true
• || (Or)
• A || B is true if either A or B are true
• ! (Not)
• !(condition) is true if condition is false, and false if condition is true
• This is called the logical complement or negation
• Examples
• (salary < 10000) || (dependants > 5)
• (temperature > 40.0) && (humidity > 90)
• !(temperature > 90.0)
Logical Operators - Exercises

• NOT, AND, OR : ( !, &&, || )


• Operator ! is equivalent to Boolean operation NOT
• ! (5 == 5) returns false
• ! (6 <= 4) returns true
• ! true returns false.
• ! false returns true.

• ((5 == 5) && (3 > 6)) returns false (true && false)


• ((5 == 5) || (3 > 6)) returns true ( true || false ).
Truth table (AND)
Truth table (OR)
Truth table (NOT)
Points to Ponder!!

• && operator yields a true result only when both its operands are true.

• || operator yields a false result only when both its operands are false.
Conditional Operators

• condition ? result1 : result2


 if condition is true the expression will return result1, if not it will return
result2
• Examples
 7==5 ? 4 : 3 returns 3 since 7 is not equal to 5.
 7==5+2 ? 4 : 3 returns 4 since 7 is equal to 5+2
 a>b ? a : b returns the greater one, a or b

int res = a>b ? a : b;


Bitwise Operators
• Bitwise Operators ( &, |, ^, ~, <<, >> )

& AND Logical AND


| OR Logical OR
^ XOR Logical exclusive OR
~ NOT Complement to one (bit inversion)
<< SHL Shift Left
>> SHR Shift Right
XOR

0
Type conversion
• Automatic Type Conversion
• Casting
Automatic Type Conversion

void main(void)
{
int number = 2;
float factor = 1.5;
double result = number * factor;
cout << "Result is : " <<
result;
}
void main(void)
{
short x = 2;
int y = x;
}
Type conversion
Casting
void main(void)
{
short number = 30000;//(2^16 – 1)/2 (-32768 to
32767)
short result = (number * 10) / 10;
cout << "Result is : " << result;//Result
Incorrect
number = 30000;
result = (long(number) * 10) / 10; //Casting
cout << "Result is : " << result;
}
Operator Precedence!
Example!
int x = 1, y = 2;
int result = y++ + ++x;
cout << result << endl;
cout << x << endl;
cout << y << endl;

• Unary operator has higher precedence and is evaluated right to left


• x gets the value 2.
• y++ is postfix form so y is incremented after the execution of this
statement
• 2+2 = 4
Escape Sequences

1.\n (New line) – We use it to shift the cursor control to the new line
2.\t (Horizontal tab) – We use it to shift the cursor to a couple of spaces to the right in the same
line.
3.\a (Audible bell) – A beep is generated indicating the execution of the program to alert the user.
4.\r (Carriage Return) – We use it to position the cursor to the beginning of the current line.
5.\\ (Backslash) – We use it to display the backslash character.
6.\’ (Apostrophe or single quotation mark) – We use it to display the single-quotation mark.
7.\” (Double quotation mark)- We use it to display the double-quotation mark.
8.\0 (Null character) – We use it to represent the termination of the string.
9.\? (Question mark) – We use it to display the question mark. (?)
Control Structures:
if, if else and else if Statements

• if statement
• Use of if else
• else-if Statement
• Switch statement
Control Structures
• 3 control structures
• Sequence structure
• Programs executed sequentially by default
• Selection structures
• if, if/else, switch
• Repetition structures
• for, while, do/while
The If Selection Structure
• Selection structure
• Choose among alternative courses of action
• Pseudocode example:
If student’s grade is greater than or equal to 60
Print “Passed”

If the condition is true
Print statement executed, program continues to next statement

If the condition is false
Print statement ignored, program continues
The If Selection Structure
• Translation into C++
If student’s grade is greater than or equal to 60
Print “Passed”

if ( grade >= 60 )
cout << "Passed";
if/else Selection Structure
• if
• Performs action if condition true
• if/else
• Different actions if conditions true or false
• Pseudocode
if student’s grade is greater than or equal to 60
print “Passed”
else
print “Failed”
• C++ code
if ( grade >= 60 )
cout << "Passed";
else
cout << "Failed";
The If Selection Structure
• Ternary conditional operator (?:)
– Three arguments (condition, value if true, value if false)
• Code could be written:
cout << ( grade >= 60 ? “Passed” :
“Failed” );

false true
grade >= 60

print “Failed” print “Passed”


Example
The If Selection Structure

• Compound statement
• Set of statements within a pair of braces

if ( grade >= 60 )
cout << "Passed.\n";
else
{
cout << "Failed.\n";
cout << "You must take this course again.\n";
}
• Without braces,
cout << "You must take this course again.\n";
always executed

• Block
• Set of statements within braces
The If Selection Structure
• Nested if/else structures
• One inside another, test for
if student’s multiple
grade cases
is greater than or equal to 90
Print “A”
• Once condition met,
else
other statements skipped
if student’s grade is greater than or equal to 80
Print “B”
else
if student’s grade is greater than or equal to 70
Print “C”
else
if student’s grade is greater than or equal to 60
Print “D”
else
Print “F”
The If Selection Structure
Example
if ( grade >= 90 ) // 90 and above
cout << "A";
else if ( grade >= 80 ) // 80-89
cout << "B";
else if ( grade >= 70 ) // 70-79
cout << "C";
else if ( grade >= 60 ) // 60-69
cout << "D";
else // less than 60
cout << "F";
The If Selection Structure

• You can assign an int type variable a non zero value for true or zero for
false.
• Example
even = (n%2 == 0);
if(even) { do something }

• Some people prefer following for better readability.


if(even == 0) { do something }
The If Selection Structure

• Beginning programmers sometime prefer to use a sequence of if


statements rather than a single nested if statement

if (x > 0)
num_pos = num_pos + 1;
if (x < 0)
num_neg = num_neg + 1;
if (x == 0)
num_zero = num_zero +1;

• This is less efficient because all three of the conditions


are always tested.
The If Selection Structure
• Nested ‘if’ statements

if(x > 0)
num_pos = num_pos + 1;
else if(x < 0)
num_neg = num_neg + 1;
else
num_zero = num_zero + 1;

• In the nested if statement, only the first condition is tested when x is


positive.
The If Selection Structure
• Nested if statements can become quite complex. If there are more
than three alternatives and indentation is not consistent, it may be
difficult for you to determine the logical structure of the if statement.

• You can code the nested if as the multiple-alternative decision.


if ( condition_1 )
statement_1
else if ( condition_2 )
statement_2
.
.
.
else if ( condition_n )
statement_n
else
statement_e
The If Selection Structure
• Order of Conditions
• When more than one condition in a multiple-alternative decision is true, only
the task following the first true condition executes.

• Therefore, the order of the conditions can affect the outcome.

You might also like