Module 4
Module 4
Operators
I. Preparations
At the end of this module students will:
construct arithmetic, relational and logical expression;
construct simple condition and compound condition
II. Presentation
Operator are used to perform mathematical or logical operation on variable.
Operator is a symbol that perform specific mathematical or logical operation on
variable in any programming language.
List of C# Operators
Arithmetic Operators
Logical Operators
Relational or Comparison Operators
Assignment Operators
Operator Description
* Multiplication
/ Division
% Reminder
+ Addition
- Subtraction
Example #1 – Addition
Above example as show screen, there are two values 10 and 15 to the textbox and on
Integer Addition and String Addition buttons. The results, for Integer Addition button is 15
while String Addition button is 1015.
}
protected void btnIntegeraddition_Click(object sender, EventArgs e)
{
int firstno;
int secondno;
firstno = Convert.ToInt32(TextBox1.Text);
secondno = Convert.ToInt32(TextBox2.Text);
firstno = TextBox1.Text;
secondno = TextBox2.Text;
firstno = Convert.ToInt32(TextBox1.Text);
secondno = Convert.ToInt32(TextBox2.Text);
}
protected void btnmultiplication_Click(object sender, EventArgs e)
{
int firstno;
int secondno;
firstno = Convert.ToInt32(TextBox1.Text);
secondno = Convert.ToInt32(TextBox2.Text);
firstno = Convert.ToInt32(TextBox1.Text);
secondno = Convert.ToInt32(TextBox2.Text);
firstno = Convert.ToInt32(TextBox1.Text);
secondno = Convert.ToInt32(TextBox2.Text);
Logical Operators
The C# language supports basic three logical operators. The logical operators returns true
or false as output.
The logical operators are used when we have conditional statement such as if statement.
C# Supports following logical operators
Assume variable X is true and variable Y is false then
Operator Meaning Example
&& Logical AND if(X && Y) then false
|| Logical OR if(X || Y) then true
! Logical NOT if !(X && Y) then true
The Convert class
The Convert class includes different methods which convert base data type to another
base data type.
The Convert class includes the following methods to convert from different data types to
int type.
Convert.ToInt16()
Convert.ToInt32()
Convert.ToInt64()
The Convert.ToInt16() method returns the 16-bit integer e.g. short, the Convert.ToInt32()
returns 32-bit integers e.g. int and the Convert.ToInt64() returns the 64-bit integer e.g. long.
Convert.ToInt64("1003232131321321");//returns long
Input string must be valid number string, cannot include different numeric formats. Only
works with valid integer string.
Input string must be within the range of called IntXX method e.g. Int16, Int32, Int64.
The input string cannot include parenthesis, comma, etc.
Must use a different method for different integer ranges e.g. cannot use the
Convert.ToInt16() for the integer string higher than "32767".
Visit Convert class for more information.
III. Practice. Problem Solving
Construct Webform that will accept 3-digit integer and will display the one’s,
tent’s and the hundred’s place.