CPCS301_Topic5_Operators - updated - part 1
CPCS301_Topic5_Operators - updated - part 1
Objective
Learning operators in C, PHP, Python, MATLAB languages
Lab Description
What does “Operator” mean?
An operator is a symbol that states the compiler to perform specific mathematical or logical manipulations.
The operators in C
1|Page
All related materials was taken from: C (https://www.tutorialspoint.com/cprogramming/c_operators.htm) Python
(https://www.programiz.com/python-programming/operators) PHP (https://www.w3schools.com/php/php_operators.asp)
Code output
#include <stdio.h> Line 1 - Value of c is 31
void main() { Line 2 - Value of c is 11
int a = 21;
int b = 10; Line 3 - Value of c is 210
int c ; Line 4 - Value of c is 2
c = a + b; Line 5 - Value of c is 1
printf("Line 1 - Value of c is %d\n", c ); Line 6 - Value of c is 21
c = a - b;
printf("Line 2 - Value of c is %d\n", c );
Line 7 - Value of c is 22
c = a * b;
printf("Line 3 - Value of c is %d\n", c );
c = a / b;
printf("Line 4 - Value of c is %d\n", c );
c = a % b;
printf("Line 5 - Value of c is %d\n", c );
c = a++;
printf("Line 6 - Value of c is %d\n", c );
c = a--;
printf("Line 7 - Value of c is %d\n", c );
}
The following table shows all the relational operators supported by C. Assume variable A holds 10 and variable B
holds 20 then:
Operator Description Example
Checks if the values of two operands are equal or not. If yes, then the condition
== (A == B) is not true.
becomes true.
Checks if the values of two operands are equal or not. If the values are not equal,
!= (A != B) is true.
then the condition becomes true.
Checks if the value of left operand is greater than the value of right operand. If yes,
> (A > B) is not true.
then the condition becomes true.
Checks if the value of left operand is less than the value of right operand. If yes,
< (A < B) is true.
then the condition becomes true.
Checks if the value of left operand is greater than or equal to the value of right
>= (A >= B) is not true.
operand. If yes, then the condition becomes true.
Checks if the value of left operand is less than or equal to the value of right
<= (A <= B) is true.
operand. If yes, then the condition becomes true.
2|Page
All related materials was taken from: C (https://www.tutorialspoint.com/cprogramming/c_operators.htm) Python
(https://www.programiz.com/python-programming/operators) PHP (https://www.w3schools.com/php/php_operators.asp)
Code output
#include <stdio.h> Line 1 - a is not equal to b
void main() { Line 2 - a is not less than b
int a = 21;
int b = 10; Line 3 - a is greater than b
int c ; Line 4 - a is either less than or equal to b
if( a == b ) { Line 5 - b is either greater than or equal to b
printf("Line 1 - a is equal to b\n" );
} else {
printf("Line 1 - a is not equal to b\n" );
}
if ( a < b ) {
printf("Line 2 - a is less than b\n" );
} else {
printf("Line 2 - a is not less than b\n" );
}
if ( a > b ) {
printf("Line 3 - a is greater than b\n" );
} else {
printf("Line 3 - a is not greater than b\n" );
}
/* Lets change value of a and b */
a = 5;
b = 20;
if ( a <= b ) {
printf("Line 4 - a is either less than or equal
to b\n" );
}
if ( b >= a ) {
printf("Line 5 - b is either greater than or
equal to b\n" );
}
}
Hint: There is no Boolean data type in C. The result from the relational condition. (e.g. the value of variable c
in this statement c=(a==b) is 1 if the condition true and 0 otherwise).
3|Page
All related materials was taken from: C (https://www.tutorialspoint.com/cprogramming/c_operators.htm) Python
(https://www.programiz.com/python-programming/operators) PHP (https://www.w3schools.com/php/php_operators.asp)
Code output
#include <stdio.h> Line 1 - Condition is true
void main() { Line 2 - Condition is true
int a = 5;
int b = 20; Line 3 - Condition is not true
if ( a && b ) { Line 4 - Condition is true
printf("Line 1 - Condition is true\n" );
}
if ( a || b ) {
printf("Line 2 - Condition is true\n" );
}
a = 0;
b = 10;
if ( a && b ) {
printf("Line 3 - Condition is true\n" );
} else {
printf("Line 3 - Condition is not true\n" );
}
if ( !(a && b) ) {
printf("Line 4 - Condition is true\n" );
} }
5|Page
All related materials was taken from: C (https://www.tutorialspoint.com/cprogramming/c_operators.htm) Python
(https://www.programiz.com/python-programming/operators) PHP (https://www.w3schools.com/php/php_operators.asp)
Code output
#include <stdio.h> Line 1 - = Operator Example, Value of c = 21
void main() { Line 2 - += Operator Example, Value of c = 42
int a = 21;
int c ;
Line 3 - -= Operator Example, Value of c = 21
c = a; Line 4 - *= Operator Example, Value of c = 441
printf("Line 1 - = Operator Example, Value Line 5 - /= Operator Example, Value of c = 21
of c = %d\n", c ); Line 6 - %= Operator Example, Value of c = 11
c += a; Line 7 - <<= Operator Example, Value of c = 44
printf("Line 2 - += Operator Example, Value Line 8 - >>= Operator Example, Value of c = 11
of c = %d\n", c );
c -= a; Line 9 - &= Operator Example, Value of c = 2
printf("Line 3 - -= Operator Example, Value Line 10 - ^= Operator Example, Value of c = 0
of c = %d\n", c ); Line 11 - |= Operator Example, Value of c = 2
c *= a;
printf("Line 4 - *= Operator Example, Value
of c = %d\n", c );
c /= a;
printf("Line 5 - /= Operator Example, Value
of c = %d\n", c );
c = 200;
c %= a;
printf("Line 6 - %%= Operator Example, Value
of c = %d\n", c );
c <<= 2;
printf("Line 7 - <<= Operator Example, Value
of c = %d\n", c );
c >>= 2;
printf("Line 8 - >>= Operator Example, Value
of c = %d\n", c );
c &= 2;
printf("Line 9 - &= Operator Example, Value
of c = %d\n", c );
c ^= 2;
printf("Line 10 - ^= Operator Example, Value
of c = %d\n", c );
c |= 2;
printf("Line 11 - |= Operator Example, Value
of c = %d\n", c );
}
6|Page
All related materials was taken from: C (https://www.tutorialspoint.com/cprogramming/c_operators.htm) Python
(https://www.programiz.com/python-programming/operators) PHP (https://www.w3schools.com/php/php_operators.asp)
Code output
#include <stdio.h> Value of b is 30
void main() { Value of b is 20
int a = 10;
int b;
double c;
/* example of ternary operator */
b = (a == 1) ? 20: 30;
printf( "Value of b is %d\n", b );
b = (a == 10) ? 20: 30;
printf( "Value of b is %d\n", b );
}
Operators in Python
Python language supports the following types of operators −
• Arithmetic Operators • Bitwise Operators
• Comparison (Relational) Operators • Membership Operators
• Assignment Operators • Identity Operator
• Logical Operators
1-Python’s Arithmetic Operators: (Assume variable a holds the value 10 and variable b holds the value 21)
- Subtraction Subtracts right hand operand from left hand operand. a – b = -11
% Modulus Divides left hand operand by right hand operand and returns b%a=1
remainder
** Exponent Performs exponential (power) calculation on operators a**b =10 to the power 20
// Floor The division of operands where the result is the quotient in which the 9//2 = 4 and 9.0//2.0 =
Division digits after the decimal point are removed. But if one of the operands 4.0, -11//3 = -4, -11.0//3 =
is negative, the result is floored, i.e., rounded away from zero -4.0
(towards negative infinity)
7|Page
All related materials was taken from: C (https://www.tutorialspoint.com/cprogramming/c_operators.htm) Python
(https://www.programiz.com/python-programming/operators) PHP (https://www.w3schools.com/php/php_operators.asp)
Example:
Code output
x = 15 x + y = 19
y=4 x - y = 11
print('x + y =',x+y) x * y = 60
print('x - y =',x-y) x / y = 3.75
print('x * y =',x*y) x % y = 3
print('x / y =',x/y) x // y = 3
print('x % y =',x%y) x ** y = 50625
print('x // y =',x//y)
print('x ** y =',x**y)
2-Python’s Comparison operators (Assume variable a holds the value 10 and variable b holds the value 20)
Operator Meaning Example
== If the values of two operands are equal, then the condition becomes true. (a == b) is not true.
!= If values of two operands are not equal, then condition becomes true. (a!= b) is true.
> If the value of left operand is greater than the value of right operand, then (a > b) is not true.
condition becomes true.
< If the value of left operand is less than the value of right operand, then (a < b) is true.
condition becomes true.
>= If the value of left operand is greater than or equal to the value of right (a >= b) is not true.
operand, then condition becomes true.
<= If the value of left operand is less than or equal to the value of right operand, (a <= b) is true.
then condition becomes true.
Code output
x = 10 x > y is False
y = 12 x < y is True
x == y is False
print('x > y is',x>y) x != y is True
print('x < y is',x<y) x >= y is False
print('x == y is',x==y) x <= y is True
print('x != y is',x!=y)
print('x >= y is',x>=y)
print('x <= y is',x<=y)
8|Page
All related materials was taken from: C (https://www.tutorialspoint.com/cprogramming/c_operators.htm) Python
(https://www.programiz.com/python-programming/operators) PHP (https://www.w3schools.com/php/php_operators.asp)
3-Python’s Logical operators
Operator Meaning Example
Code output
x = True x and y is False
y = False x or y is True
not x is False
print('x and y is',x and y)
print('x or y is',x or y)
print('not x is',not x)
not in Evaluates to true if it does not finds x not in y, here not in results in a 1 if x is not a
a variable in the specified sequence member of sequence y.
and false otherwise. Ex: x=5 print(x not in[1,2.5,4,5,”K”]) à False
9|Page
All related materials was taken from: C (https://www.tutorialspoint.com/cprogramming/c_operators.htm) Python
(https://www.programiz.com/python-programming/operators) PHP (https://www.w3schools.com/php/php_operators.asp)
7-Python Identity Operators
Identity operators compare the memory locations of two objects. There are two Identity operators as explained
below
Operator Description Example
is Evaluates to true if the variables on either side of the x is y, here is results in 1 if id(x)
operator point to the same object and false otherwise. equals id(y).
is not Evaluates to false if the variables on either side of the x is not y, here is not results in 1 if
operator point to the same object and true otherwise. id(x) is not equal to id(y).
Code Output
a=[1,2,3] True
b=a False
print( b is a)
c=[1,2,3]
print ( c is a)
x = 22
if x < 15 :
print "less than 15"
elif x < 25 :
print "less than 25 and more than or equal 15"
else :
print "greater than or equal 25”
10 | P a g e
All related materials was taken from: C (https://www.tutorialspoint.com/cprogramming/c_operators.htm) Python
(https://www.programiz.com/python-programming/operators) PHP (https://www.w3schools.com/php/php_operators.asp)
Operators in PHP:
1-PHP’s Arithmetic operators
The same set of operators as in C languages plus the exponentiation operator “**”:
Code output
<?php 16
$x = 10; 4
$y = 6; 60
echo $x + $y,"\n"; 1.6666666666667
echo $x - $y,"\n";
4
echo $x * $y,"\n";
10
echo $x / $y,"\n";
echo $x % $y,"\n"; 11
echo $x++,"\n"; 6
echo $x,"\n"; 5
echo $y--,"\n";
echo $y,"\n";
?>
=== Identical $x === $y Returns true if $x is equal to $y, and they are of the same type
!== Not identical $x !== $y Returns true if $x is not equal to $y, or they are not of the same
type
11 | P a g e
All related materials was taken from: C (https://www.tutorialspoint.com/cprogramming/c_operators.htm) Python
(https://www.programiz.com/python-programming/operators) PHP (https://www.w3schools.com/php/php_operators.asp)
Operator Name Example Result
Code output
<?php bool(true)
$x = 100; bool(false)
$y = "100"; bool(false)
var_dump($x == $y); // returns true because values are equal bool(true)
var_dump($x != $y); // returns false because values are equal bool(false)
var_dump($x === $y); // returns false because values are not
identical
var_dump($x !== $y); // returns true because values are not
identical
var_dump($x <> $y); // returns false because values are equal
?>
12 | P a g e
All related materials was taken from: C (https://www.tutorialspoint.com/cprogramming/c_operators.htm) Python
(https://www.programiz.com/python-programming/operators) PHP (https://www.w3schools.com/php/php_operators.asp)
Code output
<?php 100 == 100 and 50 == 50 is true
$x = 100; 100 == 100 or 50 == 80 is true
$y = 50; 100 == 100 xor 50 == 80 is true
if ($x == 100 and $y == 50) { 100 == 100 && 50 == 50 is true
echo "$x == 100 and $y == 50 is true\n"; 100 == 100 || 50 == 80 is true
} 100 !== 90 is true
if ($x == 100 or $y == 80) {
echo "$x == 100 or $y == 80 is true\n";
}
if ($x == 100 xor $y == 80) {
echo "$x == 100 xor $y == 80 is true\n";
}
if ($x == 100 && $y == 50) {
echo "$x == 100 && $y == 50 is true\n";
}
if ($x == 100 || $y == 80) {
echo "$x == 100 || $y == 80 is true\n";
}
if ($x !== 90) {
echo "$x !== 90 is true";
}
?>
Operators in MATLAB:
1-MATLAB’s Arithmetic Operators
Symbol Role
+ Addition
+ Unary plus
- Subtraction
- Unary minus
* Matrix multiplication
/ Matrix right division
^ Matrix power
% Comment
13 | P a g e
All related materials was taken from: C (https://www.tutorialspoint.com/cprogramming/c_operators.htm) Python
(https://www.programiz.com/python-programming/operators) PHP (https://www.w3schools.com/php/php_operators.asp)
Symbol Role
>= Greater than or equal to
< Less than
<= Less than or equal to
3-MATLAB’s Logical Operators
Symbol Role
& Logical AND
| Logical OR
&& Logical AND (with short-circuiting)
|| Logical OR (with short-circuiting)
~ Logical NOT
14 | P a g e
All related materials was taken from: C (https://www.tutorialspoint.com/cprogramming/c_operators.htm) Python
(https://www.programiz.com/python-programming/operators) PHP (https://www.w3schools.com/php/php_operators.asp)
15 | P a g e
All related materials was taken from: C (https://www.tutorialspoint.com/cprogramming/c_operators.htm) Python
(https://www.programiz.com/python-programming/operators) PHP (https://www.w3schools.com/php/php_operators.asp)
16 | P a g e
All related materials was taken from: C (https://www.tutorialspoint.com/cprogramming/c_operators.htm) Python
(https://www.programiz.com/python-programming/operators) PHP (https://www.w3schools.com/php/php_operators.asp)