1|Page
Lesson 3 C# Operators, Math, and String
Intended Learning
Outcome At the end of the lesson, you are expected to:
1. Discuss the different function of C# operators
2. Apply the use of different operators in writing program
3. Familiarize some of the methods in math class
4. Manipulate string values using string methods
Activity
1. Compute the area of a trapezoid if the two sides (a,b) are 4 and 8 and
the height (h) is 10.
Analysis
How did you get the area of the trapezoid? To get the area of the trapezoid,
we have to look first the formula to calculate its area which is area=(a+b)*h/2.
If we examine it, the given sides and height were computed using addition,
multiplication and division. Every programming language uses operators,
through which we can perform various operations on the data to get the
output needed. It is important to be familiar on the functions of different
operators so we will be guided on how we are going to solve problems which
will be discussed in this module.
Abstraction
Operator
An operator is a symbol that tells the compiler to perform specific
mathematical or logical manipulations. C# has rich set of built-in operators
and provides the following type of operators:
Arithmetic Operators
Relational Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc Operators
IT 103 Training Module
2|Page
Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.
Following table shows all the arithmetic operators supported by C#. Assume
variable A holds 5 and variable B holds 50 then −
Operator Description Example
+ Adds two operands A + B = 55
- Subtracts second operand from the first A - B = -45
B – A = 45
* Multiplies both operands A * B = 250
/ Divides numerator by de-numerator B / A = 10
% Modulus Operator and remainder of after B%A=0
an integer division
++ Increment operator increases integer value A++ = 6
by one
-- Decrement operator decreases integer A-- = 4
value by one
Note: For expressions with multiple operators, the operators with higher
precedence are evaluated first before the operators with lower precedence.
For example; x = 3 + 5 * 10; here, x is assigned 53, not 18 because operator *
has higher precedence than +, so the first evaluation takes place for 3*2 and
then 7 is added into it. To change the order of evaluation imposed by operator
precedence, enclose the values to be computed with parenthesis.
Relational Operators
Relational operators are used to compare two values.
Following table shows all the relational operators supported by C#. Assume
variable A holds 5 and variable B holds 50, then −
Operator Description Example
== Checks if the values of two operands (A == B) is not true.
are equal or not, if yes then condition
becomes true.
IT 103 Training Module
3|Page
!= Checks if the values of two operands (A != B) is true.
are equal or not, if values are not equal
then condition becomes true.
> Checks if the value of left operand is (A > B) is not true.
greater than the value of right operand,
if yes then condition becomes true.
< Checks if the value of left operand is (A < B) is true.
less than the value of right operand, if
yes then condition becomes true.
>= Checks if the value of left operand is (A >= B) is not true.
greater than or equal to the value of
right operand, if yes then condition
becomes true.
<= Checks if the value of left operand is (A <= B) is true.
less than or equal to the value of right
operand, if yes then condition becomes
true.
Logical Operators
Logical operators are used to determine the logic between variables or
values.
Following table shows all the logical operators supported by C#. Assume
variable A holds Boolean value true and variable B holds Boolean value false,
then −
Operator Description Example
&& Called Logical AND operator. If both the (A && B) is false.
operands are non zero then condition
becomes true.
|| Called Logical OR Operator. If any of the two (A || B) is true.
operands is non zero then condition
becomes true.
! Called Logical NOT Operator. Use to !(A && B) is true.
reverses the logical state of its operand. If a
condition is true then Logical NOT operator
will make false.
IT 103 Training Module
4|Page
Bitwise Operators
Bitwise operator works on bits and perform bit by bit operation. The truth
tables for &, |, and ^ are as follows:
p q p&q p|q p^q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
Assume if A = 60; and B = 13; then in the binary format they are as follows −
A = 0011 1100
B = 0000 1101
-------------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A = 1100 0011
The Bitwise operators supported by C# are listed in the following table.
Assume variable A holds 60 and variable B holds 13, then −
Operator Description Example
& Binary AND Operator copies a bit to (A & B) = 12, which is 0000
the result if it exists in both 1100
operands.
| Binary OR Operator copies a bit if it (A | B) = 61, which is 0011
exists in either operand. 1101
^ Binary XOR Operator copies the bit (A ^ B) = 49, which is 0011
if it is set in one operand but not 0001
both.
<< Binary Left Shift Operator. The left
operands value is moved left by the A << 2 = 240, which is 1111
number of bits specified by the 0000
right operand.
IT 103 Training Module
5|Page
>> Binary Right Shift Operator. The left
operands value is moved right by A >> 2 = 15, which is 0000
the number of bits specified by the 1111
right operand.
Assignment Operators
Assignment operators are used to assign values to variables.
There are following assignment operators supported by C#:
Operator Description Example
= Simple assignment operator, Assigns
C = A + B assigns value
values from right side operands to left
of A + B into C
side operand
+= Add AND assignment operator, It adds
C += A is equivalent to
right operand to the left operand and
C=C+A
assign the result to left operand
-= Subtract AND assignment operator, It
subtracts right operand from the left C -= A is equivalent to
operand and assign the result to left C=C-A
operand
*= Multiply AND assignment operator, It
multiplies right operand with the left C *= A is equivalent to
operand and assign the result to left C=C*A
operand
/= Divide AND assignment operator, It
divides left operand with the right C /= A is equivalent to
operand and assign the result to left C=C/A
operand
%= Modulus AND assignment operator, It
C %= A is equivalent
takes modulus using two operands and
to C = C % A
assign the result to left operand
IT 103 Training Module
6|Page
Miscellaneous Operators
There are few other important operators including sizeof, typeof and ?
:supported by C#.
Operator Description Example
sizeof() Returns the size of a data type. sizeof(int), returns 4.
typeof() Returns the type of a class. typeof(StreamReader);
& &a; returns actual address
Returns the address of an variable.
of the variable.
* *a; creates pointer named
Pointer to a variable.
'a' to a variable.
?: If Condition is true ? Then
Conditional Expression
value X : Otherwise value Y
is If( Ford is Car) // checks if
Determines whether an object is of
Ford is an object of the Car
a certain type.
class.
as Object obj = new
Cast without raising an exception if StringReader("Hello");
the cast fails. StringReader r = obj as
StringReader;
C# Math
The C# Math class has many methods that allows you to perform
mathematical tasks on numbers. Some of these are the following:
1. [Link](x,y)- a method can be used to find the highest value of x and y:
2. [Link](x,y)- a method can be used to find the lowest value of of x and y:
3. [Link](x)- a method returns the square root of x:
4. [Link](x)- a method returns the absolute (positive) value of x
5. [Link]()- rounds a number to the nearest whole number:
IT 103 Training Module
7|Page
Example:
[Link] ([Link](5, 10));
[Link] ([Link](5, 10));
[Link] ([Link](64));
[Link] ([Link](-4.7));
[Link] ([Link](9.99));
Output:
10
5
8
4.7
10
C# String
Strings are used for storing text. A string variable contains a collection of
characters surrounded by double quotes. A string in C# is actually an object,
which contain properties and methods that can perform certain operations
on strings.
String Methods:
1. String Length- a string method that returns the length of the string.
2. ToUpper() and ToLower()- a string method that returns a copy of the string
converted to uppercase or lowercase.
3. Concat() – a string method to concatenate two strings
Example:
string txt = "I love Programming";
string txt1 = "Hello”
string txt2= “World";
string message = [Link](txt1, txt2);
[Link]("The length of the txt string is: " + [Link]);
[Link]([Link]());
[Link]([Link]());
[Link](message);
Output:
18
HELLO
world
HelloWorld
IT 103 Training Module
8|Page
Application
1. Write a C# program that will compute the area of your dining table. Input
its length and width and display the area.
Assignment
Write the output of the following bitwise operations.
Given:
A = 1001 0110
B = 0100 0101
1. A&B
2. A|B
3. A^B
4. A<<3
5. B>>3
Activity
Run the following machine problem using Visual Studio or android apps C#
compiler. Submit a video of you discussing while running your program.
Exercise 3
Write a C# program that will input two integers and
display its sum, difference, product, quotient and
remainder.
IT 103 Training Module
9|Page
Learning Resources Books
Boyle,B. C# Programming: Problem analysis to program design.
Pasig City, Philippines 1605.; Cengage Learning Asia Pte LTD. (2012)
Murach, J, et al. C# 2015. Mike Murach & Associate. (2016)
Nakov, S., et al. Fundamentals of Computer Programming with C#: The
Bulgarian C#. Faber Publishing.(2019)
Sharp, J, Microsoft Visual C# Step by step, 8th Edition
Microsoft Press A Division of Microsoft Corporation. (2015)
C# Tutorial. [Link]
C#[Link]-tutorials: The complete C# Tutorials.
[Link]
Csharp_tutorial_lesson.
[Link]
Learn C# Programming. C 2019.
[Link]
C# Tutorial. [Link]
C#Programmming.
[Link]
C# Programming
[Link]
1305075773_454683.pdf
IT 103 Training Module
10 | P a g e
IT 103 Training Module