Operators and Expressions

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/118

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

119 Terms

1
New cards

Operators

are the symbols that represent a specific mathematical or logical processing in programming.

2
New cards

The following are some of the operators used in C#:

3
New cards

Arithmetic Operators

Logical Operators

Relational Operators

Assignment Operators

4
New cards

Arithmetic Operators

These are operators used in performing mathematical operations on numerical values.

5
New cards

Table 1 shows the basic arithmetic operators on C#.

6
New cards

Assume the following variable declarations:

int a = 11, b = 5;

7
New cards

Operator Description Example Return value

8
New cards

+

Adds two (2) operands

Example: a + b

Return value: 16

9
New cards

-

Subtracts the second operand from the first operand

Example: a – b

Return value: 6

10
New cards

*

Multiplies both operands

Example: a * b

Return value: 55

11
New cards

/

Divides the first operand by second operand

a / b

2

12
New cards

Table 1. C# arithmetic operators (Gaddis, 2016)

13
New cards

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.

14
New cards

Assume the following variable declarations:

int a = 11, b = 5;

15
New cards

>

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

16
New cards

<

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

17
New cards

=

>=

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

18
New cards

<=

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

19
New cards

==

Determines if the two (2) operands are equal or not—if the values are equal, it returns true; otherwise, false. a == b false

20
New cards

!=

Determines if the two (2) operands are equal or not—if the values are not equal, it returns true; otherwise, false.

a != b

true

21
New cards

Table 2. C# relational operators (Gaddis, 2016)

22
New cards

Logical Operators

These are operators used in performing logical operation. These operate on logical expressions and returns a Boolean value.

23
New cards

Table 3 shows the logical operators on C#.

24
New cards

Assume the following variable declarations:

bool A = true, B = false;

25
New cards

&& Logical AND operator

This returns true only if both operands are true; otherwise, false.

Example: A && B

Value: false

26
New cards

^ Logical XOR operator

This returns true if only one (1) operand is true; otherwise, false.

Example: A ^ B

Value: true

27
New cards

! 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

28
New cards

Assignment Operators

These are used to assign a value or the result of an expression to a variable.

29
New cards

Table 4 shows the assignment operators on C#.

30
New cards

Assume the following variable declarations:

int a = 2, b = 5;

31
New cards

=

This assigns a value of a variable or expression to the variable on its left side.

Example: b = a

Value: 2

32
New cards

+=

This adds left operand to the first operand and assigns the result to the first operand.

Example: b+=a

Value: 7

33
New cards

-=

This subtracts the second operand from the first operand and assigns the result to the first operand.

Example: b-=a

Value: 3

34
New cards

*=

This multiplies both operands and assigns the result to the first operand.

Example: b*=a

Value: 10

35
New cards

/=

This divides the first operand by the second operand and assigns the result to the first operand.

Example: b/=a

Value: 2

36
New cards

%=

This assigns the remainder result to the first operand after dividing the first operand by the second operand. Example: b%=a Value: 1

37
New cards

++

This adds 1 to the first operand and assigns the result to the first operand. Example: b++ Value: 6

38
New cards

--

This subtracts 1 from the first operand and assigns the result to the first operand. Example: b-- Value: 4

39
New cards

Table 4. C# assignment operators (Gaddis, 2016)

40
New cards

Expression

An __________in C# is a combination of operands (or variables) and operators that can be evaluated to a single value.

41
New cards

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.

42
New cards

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.

43
New cards

Operator precedence and associativity

defines a set of rules indicating the order in which the operator should be evaluated in an expression.

44
New cards

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.

45
New cards

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.

46
New cards

Precedence: Highest

Operators: !, ++, --, ()

Associativity: Left to right

47
New cards

Precedence: Intermediate 1

Operators: *, /, %

Associativity: Left to right

48
New cards

Precedence: Intermediate 2

Operators: +, -

Associativity: Left to right

49
New cards

Precedence: Intermediate 3

Operators: <, <=, >, >=

Associativity: Left to right

50
New cards

Precedence: Intermediate 4

Operators: ==, !=

Associativity: Left to right

51
New cards

Precedence: Intermediate 5

Operators: ^

Associativity: Left to right

52
New cards

Precedence: Intermediate 6

Operators: &&, ||

Associativity: Left to right

53
New cards

Precedence: Lowest

Operators: =, *=, /=, %=, +=, -=

Associativity: Right to left

54
New cards

Table 5. Common C# operators’ precedence and associativity (Harwani, 2015)

55
New cards

Consider the following expression:

int a = 2 + 3 * 4;

56
New cards

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.

57
New cards

When operators of the same precedence are used in a single expression, they are evaluated from left to right.

58
New cards

System.Math

The ___________ class includes several methods that perform a variety of calculations that can be used in a program.

59
New cards

Some of methods provided by Math class:

60
New cards

Pow()

Raises a number to the given power.

61
New cards

Exp()

Raises the constant e to the given power.

62
New cards

Log()

Returns the natural and 10-based logarithm, respectively.

63
New cards

Sqrt()

Returns the square root of the given number.

64
New cards

Sign()

Returns the sign of the specified number.

65
New cards

Abs()

Returns the absolute value of a given number.

66
New cards

Ceiling()

Returns the smallest integral value larger than or equal to a fractional number.

67
New cards

Floor()

Returns the largest integral value smaller than or equal to the given fractional number.

68
New cards

Round()

Returns the number after rounding it to the nearest integral value.

69
New cards

Truncate()

Returns the number after removing its fractional part.

70
New cards

Sin()

Returns the sine of the specified angle.

71
New cards

Cos()

Returns the cosine of the specified angle.

72
New cards

Tan()

Returns the tangent of the specified angle.

73
New cards

Table 6. C# Math class methods (Harwani, 2015)

74
New cards

static methods

The methods of Math class are ______________, which means they are used with the class name without instantiating an object.

75
New cards

For example:

double answer = Math.Pow(2, 3);

//returns 8

76
New cards

Operators

This specifies which operations to perform on operands

77
New cards

% Modulus operator

returns the remainder after dividing first operand by second operand

78
New cards

1

What is the output of

int result = 10 % 3; 
Console.WriteLine(result);

79
New cards

11

What is the result of

int result = 5 + 3 * 2;

80
New cards

True

What is the output of

Console.WriteLine(5 > 3);

81
New cards

False

Evaluate

bool result = (4 == 5);
Console.WriteLine(result);

82
New cards

False

What does

Console.WriteLine(true && false); 

output?

83
New cards

True

What is the result of

Console.WriteLine(true || false);

84
New cards

Given

int x = 5; 
x += 3;
Console.WriteLine(x);

what is the output?

85
New cards

20

What does

int y = 10; 
y *= 2;
Console.WriteLine(y); 

print?

86
New cards

4

What is the output of

int result = 10 - 2 * 3 + 4 / 2;

87
New cards

8

What does

Console.WriteLine(Math.Pow(2, 3)); 

output?

88
New cards

5

What is

Console.WriteLine(Math.Sqrt(25));

89
New cards

10

What does

Console.WriteLine(Math.Abs(-10)); 

print?

90
New cards

5

Evaluate

Console.WriteLine(Math.Ceiling(4.3));

91
New cards

4

Evaluate

Console.WriteLine(Math.Floor(4.9));

92
New cards

4

What is the result of

Console.WriteLine(Math.Round(4.5));

93
New cards

4

What does

Console.WriteLine(Math.Truncate(4.9)); 

print?

94
New cards

1

Compute

Console.WriteLine(Math.Sin(Math.PI / 2));

95
New cards

1

Compute

Console.WriteLine(Math.Cos(0));

96
New cards

1

Compute

Console.WriteLine(Math.Tan(Math.PI / 4));

97
New cards

-1

What does

Console.WriteLine(Math.Sign(-15)); 

return?

98
New cards

2.718281828459045

What is the output of

Console.WriteLine(Math.Exp(1));

99
New cards

2

Compute

Console.WriteLine(Math.Log(100, 10));

100
New cards

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.