Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Principle Programming by C# Lec 3

The document provides an overview of operators in C#, categorizing them into arithmetic, assignment, comparison, logical, binary, and type conversion operators. It explains the syntax and usage of assignment operators, including examples of cascading assignments and various arithmetic operations. Additionally, it discusses operator precedence and includes examples of logical and comparison operators, demonstrating their functionality in C# programming.

Uploaded by

saifgoran037
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Principle Programming by C# Lec 3

The document provides an overview of operators in C#, categorizing them into arithmetic, assignment, comparison, logical, binary, and type conversion operators. It explains the syntax and usage of assignment operators, including examples of cascading assignments and various arithmetic operations. Additionally, it discusses operator precedence and includes examples of logical and comparison operators, demonstrating their functionality in C# programming.

Uploaded by

saifgoran037
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

Principle Programming by C#

Duhok Polytechnic University

Akre Technical Institute

Information Technology Department

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.

- Assignment operators – allow assigning values to


variables.
- Comparison operators – allow comparison of two literals
and/or variables.
- Logical operators – operators that work with Boolean data
types and Boolean expressions.
- Binary operators – used to perform operations on the
binary representation of numerical data.
- Type conversion operators – allow conversion of data
from one type to another.
2
Assignment Operators
The operator for assigning value to a variable is "=" (the character for
mathematical equation). The syntax used for assigning value is as it

follows: operand1 = literal, expression or operand2;


Assignment Operators – Example
int x = 6;
string helloString = "Hello string.";
int y = x;
Cascade Assignment
The assignment operator can be used in cascade (more than once in the
same expression).Example:
int x, y, z;
x = y = z = 25;

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.

var1 is assigned the value that is the value of var2


-= Binary var1 -= var2;
subtracted from the value of var1.

var1 is assigned the value that is the product of var1


*= Binary var1 *= var2;
and var2.
var1 is assigned the value that is the result of
/= Binary var1 /= var2;
dividing var1 by var2.

var1 is assigned the value that is the remainder


%= Binary var1 %= var2;
when var1 is divided by var2.
4
using System;
namespace assignment_operator
{ class Program
{ static void Main(string[] args)
{ int num1,num2;
num1 = 10;
num2 = 5;
num1 += num2; // same as num1=num1+num2
Console.WriteLine("Add = {0}", num1);

num1 -= num2; // same as num1=num1-num2


Console.WriteLine("\n\nSubtraction = {0}", num1);

num1 *= num2; // same as num1=num1*num2


Console.WriteLine("\n\nMultiplication={0}",num1);

num1 %= num2; // same as num1=num1%num2


Console.WriteLine("\n\nModulus = {0}", num1);
Console.ReadLine();
}
}
5
}
Operator Categories :
Below is a list of the operators, separated into categories:

Category Operators

aarithmetic -, +, *, /, %, ++, --

logical &&, ||, !, ^


comparison ==,!=, >, <, >=, <=

assignment =, +=, -=, *=, /=, %=, &=, |=, ^=, <<=, >>=

string concatenation +
type conversion (type), as, is, typeof, sizeof
6
Operator Precedence in C# :

Some operators have precedence (priority) over

others. For example, in math multiplication has

precedence over addition. The operators with a

higher precedence are calculated before those with

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 ( + , - , *, /, %).

- Its result is a numeric value

(e.g. 3 + 4 gives 7)

- Operands may be numbers and/or identifiers that have

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.

! Operator:It is pronounced as not operator. It returns true if


expression is 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

(x != y) also gives true or false according to the values of x


and y

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
}

You might also like