APPLICATION
SOFTWARE
DEVELOPMENT
~THARUKA PERERA
Lecture 03
Foundation Certification in IT – UOB batch 1
2
Operators in C#
OVERVIEW
Strings Handling in C#
3
OPERATORS IN C#
There are 04 Operator Types in C# Language.
Arithmetic Operators
Relational Operators
Logical Operators
Assignment Operators
RELATIONAL OPERATORS
Relational Operators are useful to check the relation between two operands
Operator Name Description Example (a = 6, b = 3)
== Equal to It compares two operands, and it returns true if both are a == b (false)
the same.
> Greater than It compares whether the left operand greater than the a > b (true)
right operand or not and returns true if it is satisfied.
< Less than It compares whether the left operand less than the right a < b (false)
operand or not and returns true if it is satisfied.
>= Greater than or It compares whether the left operand greater than or a >= b (true)
Equal to equal to the right operand or not and returns true if it is
satisfied.
<= Less than or It compares whether the left operand less than or equal a <= b (false)
Equal to to the right operand or not and returns true if it is
satisfied.
!= Not Equal to It checks whether two operand values equal or not and a != b (true)
return true if values are not equal.
LOGICAL OPERATORS
Boolean expressions can also use the following logical and conditional operators:
! Logical NOT
& Logical AND
| Logical OR
^ Logical exclusive OR (XOR)
&& Conditional AND
|| Conditional OR
They all take boolean operands and produce boolean results
Logical AND (&) and Logical OR (|)
Always evaluate both conditions
Conditional AND (&&) and Conditional OR (||)
COMPARISON: Would not evaluate the second condition if the result of the
first condition would already decide the final outcome.
LOGICAL AND Ex 1: (false) && (x++ > 10) --- no need to evaluate
the 2nd condition because first condition gets False. Due to
CONDITIONAL use &&, it not wants to check second condition.
OPERATORS Ex 2:
if (count != 0 && total /count)
{
…
}
ASSIGNMENT OPERATIONS
You can consider assignment as another The right- and left-hand sides of an
operator, with a lower precedence than assignment statement can contain the
the arithmetic operators same variable
ASSIGNMENT OPERATORS
Assignment operator Sample expression Explanation
+= c += 7 c = c + 7
-= d -= 4 d = d - 4
*= e *= 5 e = e * 5
/= f /= 3 f = f / 3
%= g %= 2 g = g % 2
INCREMENT AND DECREMENT OPERATORS
Operator Called Sample expression Explanation
++ preincrement ++a Increment a by 1, then use the new value
of a in the expression in which a resides.
++ postincrement a++ Use the current value of a in the expression
in which a resides, then increment a by 1.
-- predecrement --b Decrement b by 1, then use the new value
of b in the expression in which b resides.
-- postdecrement b-- Use the current value of b in the expression
in which b resides, then decrement b by 1.
The increment and decrement operators.
using System;
class Increment
{
static void Main(string[] args)
{
int c;
c = 5;
Console.WriteLine( c ); // print 5
Console.WriteLine( c++ ); // print 5 then postincrement
Console.WriteLine( c ); // print 6
Console.WriteLine(); // skip a line
c = 5;
Console.WriteLine( c ); // print 5
Console.WriteLine( ++c ); // preincrement then print 6
Console.WriteLine( c ); // print 6
} // end of method Main
}
STRING HANDLING IN C#
1. Manage Strings in Output - Concatenation
Example 1 –
string part1 = "SLIIT";
string part2 = " Academy"; SLIIT Academy
string part3 = part1 + part2;
Console.WriteLine(part3);
Example 2 –
string part1 = "SLIIT";
string part2 = " Academy"; SLIIT Academy
string part3 = string.Concat(part1,part2);
Console.WriteLine(part3);
Example 3 –
string x = “1"; 12
string y = " 2";
string part3 = part1 + part2;
Console.WriteLine(part3);
STRING HANDLING IN C#
2. Manage Strings in Output – Special Characters
Example 1 –
Console.WriteLine(“using \" in Output”); Using “ in output
Example 2 –
Console.WriteLine(“using \’ in Output”); Using ‘ in output
Example 3 –
Console.WriteLine(“using \\ in Output”); Using \ in output
Example 4 –
Console.WriteLine(“using \n new line character”); Using
new line character
Example 5 –
Console.WriteLine(“using \t tab line character”); Using tab line character
Operators in C#
LET'S
SUMMARIZE
String Handling in C#
14
SEE YOU NEXT
WEEK
15