Principle Programming by C# Lec 3
Principle Programming by C# Lec 3
First Year
______________________________
Week: 3 C#
c#
Lecturer
Emad Majed Rashed
1
Operators :
allow processing of primitive data types and objects. They take as an input
one or more operands and return some value as a result.
Operators in C#
Operators can be separated in several different categories:
- Arithmetic operators – they are used to perform simple
mathematical operations.
3
C# Assignment Operators:
The C# assignment operator is generally suffix with arithmetic
operators. The symbol of c sharp assignment operator is.
Example
Operator Category Result
Expression
= Binary var1 = var2; var1 is assigned the value of var2.
var1 is assigned the value that is the sum of var1 and
+= Binary var1 += var2;
var2.
Category Operators
aarithmetic -, +, *, /, %, ++, --
assignment =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=
string concatenation +
type conversion (type), as, is, typeof, sizeof
6
Operator Precedence in C# :
lower.
7
The following table illustrates the precedence of the
operators in C#:
Priority Operators
Highest priority (, )
++, -- (as postfix), new, (type), typeof, sizeof
++, -- (as prefix), +, - (unary), !, ~
*, /, %
+ (string concatenation)
+, -
<, >, <=, >=, is, as
==, !=
&, ^, |
&&
||
Lowest priority =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
8
Arithmetic Expressions
- It is composed of operands and arithmetic
operations ( + , - , *, /, %).
(e.g. 3 + 4 gives 7)
numeric values.
9
static void Main(string[] args)
{ int num1, num2;
int add, sub, mul;
float div;
//Accepting Values from users
Console.Write("Enter first number\t\t");
num1 = Convert.ToInt32(Console.ReadLine());
Console.Write("\n\nEnter second number\t\t");
num2 = Convert.ToInt32(Console.ReadLine());
//Processing Values
add = num1 + num2;
sub = num1 - num2;
mul = num1 * num2;
div = (float)num1 / num2;
//Displaying Output
Console.WriteLine("\n\n=====================\n");
Console.WriteLine("Addition\t\t{0}", add);
Console.WriteLine("Subtraction\t\t{0}", sub);
Console.WriteLine("Multiplication\t\t{0}", mul);
Console.WriteLine("Division\t\t{0}", div);
Console.WriteLine("\n=======================\n");
Console.ReadLine();
10
}
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 (!).
&& Operator :It is pronounced as and operator. It returns true if
both or all the condition is true and return false if any of the
condition is false.
|| Operator:It is pronounced as or operator. It also returns true or
false based on condition. If any one of the condition matches then it
returns true but if both or all the conditions are false then it returns
false.
11
The following table contains the logical operators in C# and the
operations that they perform:
12
using System;
namespace and_operator
{
class Program
{
static void Main(string[] args)
{string name, password;
bool name1;
name="Steven";
password="Steven123";
// evaluating both expresson and returns true if all are true.
name1=(name == "Steven" && password == "Steven123") ;
Console.WriteLine(name1);
name1 = (name == "Steven1" || password == "Steven123");
Console.WriteLine(name1);
Console.WriteLine(name1);
Console.ReadLine();
}
}
}
13
Relational Expressions
- It is composed from operands and operators.
- Operands may be numbers and/or identifiers
that have numeric values
- Its result is a logical value (true or false).
- Operators are relational operators:
< , > , <= , >= , = =, !=
e.g.
(a < b) gives true, if value of a is less than value of b
false, if value of a is not less than value of b
14
Comparison Operators:
Comparison operators in C# are used to compare
two or more operands. C# supports the following
comparison operators:
- greater than (>)
- less than (<)
- greater than or equal to (>=)
- less than or equal to (<=)
- equality (==)
- difference (!=)
All comparison operators in C# are binary (take two
operands) and the returned result is a Boolean value (true
or false). 15
Comparison Operators – Example
The following example demonstrates the usage of
comparison operators in C#:
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
int x = 10, y = 5;
Console.WriteLine("x > y : " + (x > y)); // True
Console.WriteLine("x < y : " + (x < y)); // False
Console.WriteLine("x >= y : " + (x >= y)); // True
Console.WriteLine("x <= y : " + (x <= y)); // False
Console.WriteLine("x == y : " + (x == y)); // False
Console.WriteLine("x != y : " + (x != y)); // True
}
}
16
}