CIT304 Chapter 4 Y24

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

C# Programming (CSC 304)

C# Operators

Adedoyin I. OYEBADE
[email protected]

Bowen University,
Iwo, Nigeria

March 18, 2024

OYEBADE A. (BU) CSC 439 March 18, 2024 1 / 24


Presentation Overview

1 C# Operators
Introduction to C# Operators
Precedence of Operators in C#
C# Expression

OYEBADE A. (BU) CSC 439 March 18, 2024 2 / 24


Introduction C# Operators

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: +, -, /, *

OYEBADE A. (BU) CSC 439 March 18, 2024 3 / 24


C# Operators and there symbols

Figure: C# Operators Types

OYEBADE A. (BU) CSC 439 March 18, 2024 4 / 24


Types of C# Operators: Types

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

OYEBADE A. (BU) CSC 439 March 18, 2024 5 / 24


Types of C# Operators: Arithmetic Operators
Examples
1 Simple Arithmetic Calculation
• int squarePerimeter = 17;
double squareSide = squarePerimeter / 4.0;
double squareArea = squareSide * squareSide;
Console.WriteLine(squareSide); // 4.25
Console.WriteLine(squareArea); // 18.0
2 Increment Arithmetic Operators: ++
• int a = 5,int b = 4;
Console.WriteLine(a + b);
Console.WriteLine(a + (b++));
Console.WriteLine(a + b);
Console.WriteLine(a + (++b));
Console.WriteLine(a + b);
3 Decrement Arithmetic Operators: - -
• int a = 5, int b = 4;
Console.WriteLine(a + b);
Console.WriteLine(a + (b- -));
Console.WriteLine(a + (- -b));
OYEBADE A. (BU) CSC 439 March 18, 2024 6 / 24
Types of C# Operators: Logical Operators

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 (!).

Figure: C# Logical Operators Operation

OYEBADE A. (BU) CSC 439 March 18, 2024 7 / 24


Types of C# Operators: Logical Operators Examples

1 Logical Operators Calculation


• bool a = true;
bool b = false;
Console.WriteLine(a && b); // False
Console.WriteLine(a || b); // True
Console.WriteLine(!b); // True
Console.WriteLine(b || true); // True
Console.WriteLine((5 > 7) ˆ(a == b)); // False

OYEBADE A. (BU) CSC 439 March 18, 2024 8 / 24


Types of C# Operators: String Concatenation

1 String Concatenation Operators


• Logical (This is the joining of two string together to form a single
string
• The operator + is used to join strings (string).
2 String Concatenation Operators Examples
• string csharp = ”C#”;
string dotnet = ”.NET”;
string csharpDotNet = csharp + dotnet;
Console.WriteLine(csharpDotNet);
string csharpDotNet4 = csharpDotNet + ” ” + 5;
Console.WriteLine(csharpDotNet4);

OYEBADE A. (BU) CSC 439 March 18, 2024 9 / 24


Types of C# Operators: Bitwise Operators
1 Bitwise Operators
• A bitwise operator is an operator that acts on the binary
representation of numeric types.
• The binary numeral system is used for this purpose
• bitwise operators are used to analyze and change those ones to
zeros and vice versa.
2 String Concatenation Operators Examples
• byte a = 3;
byte b = 5;
Console.WriteLine(a | b);
Console.WriteLine(a & b);
Console.WriteLine(a b̂);
Console.WriteLine( a & b);
Console.WriteLine(a << 1);
Console.WriteLine(a << 2);
Console.WriteLine(a >> 1);
OYEBADE A. (BU) CSC 439 March 18, 2024 10 / 24
Types of C# Operators: Comparison Operators

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).

OYEBADE A. (BU) CSC 439 March 18, 2024 11 / 24


Types of C# Operators: Comparison Operators
Examples

1 Comparison Operators Calculation


• int x = 10, y = 5;
Console.WriteLine(”x > y : ” + (x > y ));
True Console.WriteLine(”x < y : ” + (x < y));
Console.WriteLine(”x >= y : ” + (x >= y));
Console.WriteLine(”x <= y : ” + (x <= y));
Console.WriteLine(”x == y : ” + (x == y));
Console.WriteLine(”x! = y : ” + (x! = y ));

OYEBADE A. (BU) CSC 439 March 18, 2024 12 / 24


Types of C# Operators: Assignment Operators
1 Assignment Operators
• This is the operator for assigning value to a variable is ”=”
• e.g int a=65;
2 Cascade Assignment
• The assignment operator can be used in cascade (more than
once in the same expression)
• int x, y, z;
x = y = z = 25;
3 Compound Assignment Operators
• They help to reduce the volume of the code by typing two
operations together with an operator
• int x = 2;
int y = 4; x *= y;
Console.WriteLine(x);

OYEBADE A. (BU) CSC 439 March 18, 2024 13 / 24


Types of C# Operators: Conditional Operators
1 Conditional Operators
• TThe conditional operator ?: uses the Boolean value of an
expression to determine which of two other expressions must
be calculated and returned as a result
• The operator works on three operands and that is why it is
called ternary operator
2 The operator ?: has the following syntax
• operand1 ? operand2 : operand3
• It works like this: if operand1 is set to true, the operator returns
as a result operand2. Otherwise (if operand1 is set to false), the
operator returns as a result operand3.
3 Conditional Operator ”?:” – Example
• int a = 6;
int b = 4;
Console.WriteLine(a > b ? ”a>b” : ”b<=a”); // a>b
int num = a == b ? 1 : -1; // num will have value -1
OYEBADE A. (BU) CSC 439 March 18, 2024 14 / 24
Types of C# Operators: Other Operators

1 The ”.” Operator


• The access operator ”.” (dot) is used to access the member fields
or methods of a class or object
• Console.WriteLine(DateTime.Now);
2 Square Brackets [] Operator
• Square brackets [] are used to access elements of an array by
index, they are the so-called indexer
• int[] arr = 1, 2, 3 ;
Console.WriteLine(arr[0]);
string str = ”Hello”;
Console.WriteLine(str[1]);
3 Brackets () Operator
• Brackets () are used to override the priority of execution of
expressions and operators.

OYEBADE A. (BU) CSC 439 March 18, 2024 15 / 24


Types of C# Operators: Other Operators Cont...
1 Type Conversion Operator
• The operator for type conversion (type) is used to convert a
variable from one type to another.
2 Operator ”as”
• The operator as also is used for type conversion but invalid
conversion returns null, not an exception
3 Operator ”new”
• The new operator is used to create and initialize new objects
4 Operator ”is”
• The is operator is used to check whether an object is compatible
with a given type (check object’s type).
5 Operator ”??”
• The operator ?? is similar to the conditional operator ?:. The
difference is that it is placed between two operands and returns
the left operand only if its value is not null, otherwise it returns
the right operand

OYEBADE A. (BU) CSC 439 March 18, 2024 16 / 24


Types of C# Operators: Other Operators Examples

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”);

OYEBADE A. (BU) CSC 439 March 18, 2024 17 / 24


Types of Operators by Number of Arguments

Figure: C# Operators Types by Number of Arguments

OYEBADE A. (BU) CSC 439 March 18, 2024 18 / 24


Precedence of Operators in C#
1 Precedence of Operators
• The precedence of operator specifies that which operator will
be evaluated first and next. The associativity specifies the
operators direction to be evaluated, it may be left to right or
right to left.

OYEBADE A. (BU) CSC 439 March 18, 2024 19 / 24


Precedence of Operators in C#: Associative

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 ??

OYEBADE A. (BU) CSC 439 March 18, 2024 20 / 24


C# Operators Precedence: Order of Priority

Figure: C# Operators Precedence: Order of Priority


OYEBADE A. (BU) CSC 439 March 18, 2024 21 / 24
C# Expression

1 Much of the program’s work is the calculation of expressions.


Expressions are sequences of operators, literals and variables
that are calculated to a value of some type (number, string,
object or other type).
• int r = (150-20) / 2 + 5;
double surface = Math.PI * r * r;
double perimeter = 2 * Math.PI * r;
Console.WriteLine(r);
Console.WriteLine(surface);
Console.WriteLine(perimeter);

OYEBADE A. (BU) CSC 439 March 18, 2024 22 / 24


C# Keyword
1 A keyword is a reserved word
• Keyword cannot be used as identifier, variable and constant

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

You might also like