CIT304 Chapter 4 Y24
CIT304 Chapter 4 Y24
CIT304 Chapter 4 Y24
C# Operators
Adedoyin I. OYEBADE
[email protected]
Bowen University,
Iwo, Nigeria
1 C# Operators
Introduction to C# Operators
Precedence of Operators in C#
C# Expression
1 Definition
• An operator is simply a symbol that is used to perform
operations. There can be many types of operations like
arithmetic, logical, bitwise etc
• they are used to perform transformations on one, two or three
operands
• Examples are: +, -, /, *
1 Arithmetic Operators
• they are used to perform simple mathematical operations.
• They perform addition, subtraction and multiplication on
numerical values and the result is also a numerical value.
• +, -, /, *, - -, ++, %
2 Arithmetic Operators: ++
• adds one unit to the value of the variable
3 Arithmetic Operators: - -
• subtracts one unit from the value
1 Logical Operators
• Logical (Boolean) operators take Boolean values and return a
Boolean result (true or false).
• The basic Boolean operators are ”AND” (&&), ”OR” (——),
”exclusive OR” (ˆ) and logical negation (!).
1 Comparison Operators
• Comparison operators in C# are used to compare two or more
operands
2 C# supports the following comparison operators:
• greater than (>)
• less than (<)
• greater than or equal to (>=)
• less than or equal to (<=)
• equality (==)
• difference (! =)
3 All comparison operators in C# are binary (take two operands)
and the returned result is a Boolean value (true or false).
1 Brackets () Operator
• int a = 6; int b = 3;
Console.WriteLine(a + b / 2);
Console.WriteLine((a + b) / 2);
2 Operator ”is”
• string s = ”Beer”;
Console.WriteLine(s is string);
3 Operator ”??”
• string notNullString = s;
string nullString = null;
Console.WriteLine(nullString ?? ”Unspecified”);
Console.WriteLine(notNullString ?? ”Specified”);
1 Associatives
• Is the direction in which the expression is calculated.
• It can be left or right.
2 Left Associatives
• the expressions are calculated from left to right
• All binary operators in C# are left-associative
3 Right Associatives
• the expressions are calculated from right to left
• All assignment operators and conditional operators ?: and ??
Figure: Keyword in C#
OYEBADE A. (BU) CSC 439 March 18, 2024 23 / 24
Assignment
1 A Write an expression that calculates the area of a trapezoid by
given sides a, b and height h
2 Write a program that prints on the console the perimeter and
the area of a rectangle by given side and height entered by the
user
3 Write a Boolean expression that checks if the bit on position p
in the integer v has the value 1. Example v = 5, p = 1− > false.
4 Write a program that checks if a given number n (1 < n < 100)
is a prime number (i.e. it is divisible without remainder only to
itself and 1).
5 Write an expression that checks for given point {x, y } if it is
within the circle K ({0, 0}, R = 5) and out of the rectangle
[{−1, 1}, {5, 5}]. Clarification: for the rectangle the lower left
and the upper right corners are given.
OYEBADE A. (BU) CSC 439 March 18, 2024 24 / 24