1/118
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Operators
are the symbols that represent a specific mathematical or logical processing in programming.
The following are some of the operators used in C#:
Arithmetic Operators
Logical Operators
Relational Operators
Assignment Operators
Arithmetic Operators
These are operators used in performing mathematical operations on numerical values.
Table 1 shows the basic arithmetic operators on C#.
Assume the following variable declarations:
int a = 11, b = 5;
Operator Description Example Return value
+
Adds two (2) operands
Example: a + b
Return value: 16
-
Subtracts the second operand from the first operand
Example: a – b
Return value: 6
*
Multiplies both operands
Example: a * b
Return value: 55
/
Divides the first operand by second operand
a / b
2
Table 1. C# arithmetic operators (Gaddis, 2016)
Relational Operators
These are used to determine the relationship between operands of numeric values and generate a decision on that base. These return Boolean values true and false.
Assume the following variable declarations:
int a = 11, b = 5;
>
Determines if the value of the first operand is greater than the value of the second operand—if yes, it returns true; otherwise, false. a > b true
<
Determines if the value of the first operand is less than the value of the second operand—if yes, it returns true; otherwise, false. a < b false
=
>=
Determines if the value of the first operand is greater than or equal to the value ofthe second operand—if yes, it returns true; otherwise, false. a >= b true
<=
Determines if the value of the first operand is less than or equal to the value of the second operand—if yes, it returns true; otherwise, false. a <= b false
==
Determines if the two (2) operands are equal or not—if the values are equal, it returns true; otherwise, false. a == b false
!=
Determines if the two (2) operands are equal or not—if the values are not equal, it returns true; otherwise, false.
a != b
true
Table 2. C# relational operators (Gaddis, 2016)
Logical Operators
These are operators used in performing logical operation. These operate on logical expressions and returns a Boolean value.
Table 3 shows the logical operators on C#.
Assume the following variable declarations:
bool A = true, B = false;
&& Logical AND operator
This returns true only if both operands are true; otherwise, false.
Example: A && B
Value: false
^ Logical XOR operator
This returns true if only one (1) operand is true; otherwise, false.
Example: A ^ B
Value: true
! Logical NOT operator.
This reverses the value of a Boolean variable. If the value of the Boolean variable is true, the NOT operator will make it false and vice versa.
Example: !A
Value: false
Assignment Operators
These are used to assign a value or the result of an expression to a variable.
Table 4 shows the assignment operators on C#.
Assume the following variable declarations:
int a = 2, b = 5;
=
This assigns a value of a variable or expression to the variable on its left side.
Example: b = a
Value: 2
+=
This adds left operand to the first operand and assigns the result to the first operand.
Example: b+=a
Value: 7
-=
This subtracts the second operand from the first operand and assigns the result to the first operand.
Example: b-=a
Value: 3
*=
This multiplies both operands and assigns the result to the first operand.
Example: b*=a
Value: 10
/=
This divides the first operand by the second operand and assigns the result to the first operand.
Example: b/=a
Value: 2
%=
This assigns the remainder result to the first operand after dividing the first operand by the second operand. Example: b%=a Value: 1
++
This adds 1 to the first operand and assigns the result to the first operand. Example: b++ Value: 6
--
This subtracts 1 from the first operand and assigns the result to the first operand. Example: b-- Value: 4
Table 4. C# assignment operators (Gaddis, 2016)
Expression
An __________in C# is a combination of operands (or variables) and operators that can be evaluated to a single value.
If all operands in an expression are integers, the expression evaluates to an integer value. For example: int x = 10 + 5 * 2; //evaluates to 20.
If an expression contains a floating-point value, it evaluates to a floating-point value. For example: double y = 10 + 5 * 2.0; //evaluates to 20.0.
Operator precedence and associativity
defines a set of rules indicating the order in which the operator should be evaluated in an expression.
When two (2) or more operators are used in an expression, the operators with the higher precedence are executed first, followed by the operators of lower precedence.
Table 5 displays common C# operators’ precedence and their associativity. The operators in the table are arranged in order of precedence from highest to lowest.
Precedence: Highest
Operators: !, ++, --, ()
Associativity: Left to right
Precedence: Intermediate 1
Operators: *, /, %
Associativity: Left to right
Precedence: Intermediate 2
Operators: +, -
Associativity: Left to right
Precedence: Intermediate 3
Operators: <, <=, >, >=
Associativity: Left to right
Precedence: Intermediate 4
Operators: ==, !=
Associativity: Left to right
Precedence: Intermediate 5
Operators: ^
Associativity: Left to right
Precedence: Intermediate 6
Operators: &&, ||
Associativity: Left to right
Precedence: Lowest
Operators: =, *=, /=, %=, +=, -=
Associativity: Right to left
Table 5. Common C# operators’ precedence and associativity (Harwani, 2015)
Consider the following expression:
int a = 2 + 3 * 4;
The precedence of the multiplication operator is higher than the plus operator, and the assignment operator has the lowest precedence. Therefore, 3 * 4 is evaluated first, and the result is added to 2.
When operators of the same precedence are used in a single expression, they are evaluated from left to right.
System.Math
The ___________ class includes several methods that perform a variety of calculations that can be used in a program.
Some of methods provided by Math class:
Pow()
Raises a number to the given power.
Exp()
Raises the constant e to the given power.
Log()
Returns the natural and 10-based logarithm, respectively.
Sqrt()
Returns the square root of the given number.
Sign()
Returns the sign of the specified number.
Abs()
Returns the absolute value of a given number.
Ceiling()
Returns the smallest integral value larger than or equal to a fractional number.
Floor()
Returns the largest integral value smaller than or equal to the given fractional number.
Round()
Returns the number after rounding it to the nearest integral value.
Truncate()
Returns the number after removing its fractional part.
Sin()
Returns the sine of the specified angle.
Cos()
Returns the cosine of the specified angle.
Tan()
Returns the tangent of the specified angle.
Table 6. C# Math class methods (Harwani, 2015)
static methods
The methods of Math class are ______________, which means they are used with the class name without instantiating an object.
For example:
double answer = Math.Pow(2, 3);
//returns 8
Operators
This specifies which operations to perform on operands
% Modulus operator
returns the remainder after dividing first operand by second operand
1
What is the output of
int result = 10 % 3;
Console.WriteLine(result);
11
What is the result of
int result = 5 + 3 * 2;
True
What is the output of
Console.WriteLine(5 > 3);
False
Evaluate
bool result = (4 == 5);
Console.WriteLine(result);
False
What does
Console.WriteLine(true && false);
output?
True
What is the result of
Console.WriteLine(true || false);
Given
int x = 5;
x += 3;
Console.WriteLine(x);
what is the output?
20
What does
int y = 10;
y *= 2;
Console.WriteLine(y);
print?
4
What is the output of
int result = 10 - 2 * 3 + 4 / 2;
8
What does
Console.WriteLine(Math.Pow(2, 3));
output?
5
What is
Console.WriteLine(Math.Sqrt(25));
10
What does
Console.WriteLine(Math.Abs(-10));
print?
5
Evaluate
Console.WriteLine(Math.Ceiling(4.3));
4
Evaluate
Console.WriteLine(Math.Floor(4.9));
4
What is the result of
Console.WriteLine(Math.Round(4.5));
4
What does
Console.WriteLine(Math.Truncate(4.9));
print?
1
Compute
Console.WriteLine(Math.Sin(Math.PI / 2));
1
Compute
Console.WriteLine(Math.Cos(0));
1
Compute
Console.WriteLine(Math.Tan(Math.PI / 4));
-1
What does
Console.WriteLine(Math.Sign(-15));
return?
2.718281828459045
What is the output of
Console.WriteLine(Math.Exp(1));
2
Compute
Console.WriteLine(Math.Log(100, 10));
Math.Log(x, base) (Logarithm)
is the inverse of exponentiation.
It answers: "To what power must the base be raised to get x?"
Example:
Console.WriteLine(Math.Log(100, 10));
Output: 2
Explanation: Since 10^2 = 100, the logarithm of 100 with base 10 is 2.