02 Handout 2
02 Handout 2
02 Handout 2 *Property of
STI
IT1907
versa.
Table 3. C# logical operators (Gaddis, 2016)
02 Handout 2 *Property of
STI
IT1907
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;
Operator Description Example Value
= This assigns a value of a variable or expression to the variable on its left side. b=a 2
+= This adds left operand to the first operand and assigns the result to the first operand. b+=a 7
-= This subtracts the second operand from the first operand and assigns the result to the first b-=a 3
operand.
*= This multiplies both operands and assigns the result to the first operand. b*=a 10
/= This divides the first operand by the second operand and assigns the result to the first b/=a 2
operand.
%= This assigns the remainderresult to the first operand after dividing the first operand bythe b%=a 1
second operand.
++ This adds 1 to the first operand and assigns the result to the first operand. b++ 6
-- This subtracts 1 from the first operand and assigns the result to the first operand. b-- 4
Table 4. C# assignment operators (Gaddis, 2016)
An expression 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.
Precedence and Associativity
The 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 Operators Associativity
Highest !, ++, --, () Left to right
*, /, % Left to right
+, - Left to right
<, <=, >, >= Left to right
Intermediate ==, != Left to right
^ Left to right
&&, || Left to right
Lowest =, *=, /=, %=, +=, -= 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.
The Math Class
The System.Math class includes several methods that perform a variety of calculations that can be used in a program.
Table 6 shows some of methods provided by Math class.
Method Description
Pow() Raises a number to the given power.
02 Handout 2 *Property of
STI
IT1907
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)
The methods of Math class are static methods, which means they are used with the class name without instantiating an
object. For example: double answer = Math.Pow(2, 3); //returns 8
REFERENCES:
Deitel, P. & Deitel, H. (2015). Visual C# 2012 how to program (5th Ed.). USA: Pearson Education, Inc.
Gaddis, T. (2016). Starting out with visual C# (4th Ed.). USA: Pearson Education, Inc.
Harwani, B. (2015). Learning object-oriented programming in C# 5.0. USA: Cengage Learning PTR.
02 Handout 2 *Property of
STI