+ All Categories
Home > Documents > Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator...

Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator...

Date post: 26-Aug-2020
Category:
Upload: others
View: 8 times
Download: 0 times
Share this document with a friend
46
Operators and Expressions Operators and Expressions Performing Simple Calculations with C# Performing Simple Calculations with C# Svetlin Nakov Svetlin Nakov Telerik Corporation Telerik Corporation www.telerik.com www.telerik.com
Transcript
Page 1: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Operators and ExpressionsOperators and ExpressionsPerforming Simple Calculations with C#Performing Simple Calculations with C#

Svetlin NakovSvetlin NakovTelerik CorporationTelerik Corporationwww.telerik.comwww.telerik.com

Page 2: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Table of ContentsTable of Contents

1.1. Operators in C# and Operator PrecedenceOperators in C# and Operator Precedence

2.2. Arithmetic OperatorsArithmetic Operators

3.3. Logical OperatorsLogical Operators

4.4. Bitwise OperatorsBitwise Operators

5.5. Comparison OperatorsComparison Operators

6.6. Assignment OperatorsAssignment Operators

7.7. Other OperatorsOther Operators

8.8. Implicit and Explicit Type ConversionsImplicit and Explicit Type Conversions

9.9. ExpressionsExpressions 2

Page 3: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Operators in C#Operators in C#Arithmetic, Logical, Comparison, Assignment, Etc.Arithmetic, Logical, Comparison, Assignment, Etc.

Page 4: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

What is an OperatorWhat is an Operator??

OperatorOperator is an operation performed over data is an operation performed over data at runtimeat runtime

Takes one or more arguments (operands)Takes one or more arguments (operands)

Produces a new valueProduces a new value

Operators have precedenceOperators have precedence

Precedence defines which will be evaluated firstPrecedence defines which will be evaluated first

Expressions Expressions are sequences of operators and are sequences of operators and operands that are evaluated to a single valueoperands that are evaluated to a single value

4

Page 5: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Operators in C#Operators in C#

Operators in C# :Operators in C# :

Unary – take one operandUnary – take one operand

Binary – take two operandsBinary – take two operands

Ternary (Ternary (?:?:) – takes three operands) – takes three operands

Except for the assignment operators, all Except for the assignment operators, all binary operators are left-associativebinary operators are left-associative

The assignment operators and the The assignment operators and the conditional operator (conditional operator (?:?:) are right-associative) are right-associative

5

Page 6: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Categories of Operators in C#Categories of Operators in C#

6

CategoryCategory OperatorsOperatorsArithmeticArithmetic ++ -- ** // %% ++++ ----LogicalLogical &&&& |||| ^ ! ^ !BinaryBinary && || ^̂ ~~ <<<< >>>>ComparisonComparison ==== !=!= << >> <=<= >=>=

AssignmentAssignment == +=+= -=-= *=*= /=/= %=%= &=&= |=|= ^=^= <<=<<= >>=>>=

String concatenationString concatenation ++Type conversionType conversion is as typeofis as typeofOtherOther . [] () ?: new. [] () ?: new

Page 7: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Operators PrecedenceOperators Precedence

Page 8: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Operators PrecedenceOperators Precedence

8

PrecedencePrecedence OperatorsOperatorsHighestHighest ++ --++ -- (postfix) (postfix) new typeof new typeof

++ --++ -- (prefix) (prefix) + - + - (unary) (unary) ! ~ ! ~* / %* / %+ -+ -<< >><< >>< > <= >= is as< > <= >= is as== != == != &&

LowerLower ^̂

Page 9: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Operators Precedence (2)Operators Precedence (2)

9

PrecedencePrecedence OperatorsOperatorsHigherHigher ||

&&&&||||?:?:

LowestLowest = *= /= %= += -= <<= >>= &= = *= /= %= += -= <<= >>= &= ^= |=^= |=

Parenthesis operator always has highest Parenthesis operator always has highest precedenceprecedence

Note: prefer using Note: prefer using parenthesesparentheses, even when it , even when it seems stupid to do soseems stupid to do so

Page 10: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Arithmetic OperatorsArithmetic Operators

Page 11: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Arithmetic OperatorsArithmetic Operators Arithmetic operators Arithmetic operators ++,, --, , ** are the same as in are the same as in

math math Division operator Division operator // if used on integers returns if used on integers returns

integer (without rounding) or exceptioninteger (without rounding) or exception Division operator Division operator // if used on real numbers if used on real numbers

returns real number or returns real number or InfinityInfinity or or NaNNaN

Remainder operatorRemainder operator %% returns the remainder returns the remainder from division of integersfrom division of integers

The special addition operator The special addition operator ++++ increments a increments a variablevariable

11

Page 12: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Arithmetic Operators – ExampleArithmetic Operators – Example

int squarePerimeter = 17;int squarePerimeter = 17;double squareSide = squarePerimeter/4.0;double squareSide = squarePerimeter/4.0;double squareArea = squareSide*squareSide;double squareArea = squareSide*squareSide;Console.WriteLine(squareSide); // 4.25Console.WriteLine(squareSide); // 4.25Console.WriteLine(squareArea); // 18.0625Console.WriteLine(squareArea); // 18.0625

int a = 5;int a = 5;int b = 4;int b = 4;Console.WriteLine( a + b ); // 9Console.WriteLine( a + b ); // 9Console.WriteLine( a + b++ ); // 9Console.WriteLine( a + b++ ); // 9Console.WriteLine( a + b ); // 10Console.WriteLine( a + b ); // 10Console.WriteLine( a + (++b) ); // 11Console.WriteLine( a + (++b) ); // 11Console.WriteLine( a + b ); // 11Console.WriteLine( a + b ); // 11

Console.WriteLine(11 / 3); // 3Console.WriteLine(11 / 3); // 3Console.WriteLine(11 % 3); // 2Console.WriteLine(11 % 3); // 2Console.WriteLine(12 / 3); // 4Console.WriteLine(12 / 3); // 4

12

Page 13: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Arithmetic OperatorsArithmetic OperatorsLive DemoLive Demo

Page 14: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Logical OperatorsLogical Operators

Page 15: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Logical OperatorsLogical Operators Logical operators take boolean operands and Logical operators take boolean operands and

return boolean resultreturn boolean result Operator Operator !! turns turns truetrue to to falsefalse and and falsefalse

toto truetrue Behavior of the operators Behavior of the operators &&&&, , |||| and and ^̂

((11 == == truetrue, , 00 == == falsefalse) :) :

OperationOperation |||| |||| |||| |||| &&&& &&&& &&&& &&&& ^̂ ^̂ ^̂ ^̂Operand1Operand1 00 00 11 11 00 00 11 11 00 00 11 11

Operand2Operand2 00 11 00 11 00 11 00 11 00 11 00 11

ResultResult 00 11 11 11 00 00 00 11 00 11 11 00

15

Page 16: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Logical Operators – ExampleLogical Operators – Example Using the logical operators:Using the logical operators:

bool a = true;bool a = true;bool b = false;bool b = false;Console.WriteLine(a && b); // FalseConsole.WriteLine(a && b); // FalseConsole.WriteLine(a || b); // TrueConsole.WriteLine(a || b); // TrueConsole.WriteLine(a ^ b); // TrueConsole.WriteLine(a ^ b); // TrueConsole.WriteLine(!b); // TrueConsole.WriteLine(!b); // TrueConsole.WriteLine(b || true); // TrueConsole.WriteLine(b || true); // TrueConsole.WriteLine(b && true); // FalseConsole.WriteLine(b && true); // FalseConsole.WriteLine(a || true); // TrueConsole.WriteLine(a || true); // TrueConsole.WriteLine(a && true); // TrueConsole.WriteLine(a && true); // TrueConsole.WriteLine(!a); // FalseConsole.WriteLine(!a); // FalseConsole.WriteLine((5>7) ^ (a==b)); // FalseConsole.WriteLine((5>7) ^ (a==b)); // False

16

Page 17: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Logical OperatorsLogical OperatorsLive DemoLive Demo

Page 18: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Bitwise OperatorsBitwise Operators

Page 19: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Bitwise operator Bitwise operator ~~ turns all turns all 00 to to 11 and all and all 11 to to 00

Like Like !! for boolean expressions but bit by bitfor boolean expressions but bit by bit The operators The operators ||,, && and and ^̂ behave like behave like ||||,, &&&& and and ^̂

for boolean expressions but bit by bitfor boolean expressions but bit by bit The The <<<< and and >>>> move the bits (left or right) move the bits (left or right) Behavior of the operatorsBehavior of the operators||,, && and and ^̂::

OperationOperation || || || || && && && && ^̂ ^̂ ^̂ ^̂Operand1Operand1 00 00 11 11 00 00 11 11 00 00 11 11

Operand2Operand2 00 11 00 11 00 11 00 11 00 11 00 11

ResultResult 00 11 11 11 00 00 00 11 00 11 11 00

19

Page 20: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Bitwise Operators (2)Bitwise Operators (2) Bitwise operators are used on integer numbers Bitwise operators are used on integer numbers

((bytebyte, , sbytesbyte, , intint, , uintuint, , longlong, , ulongulong)) Bitwise operators are applied bit by bitBitwise operators are applied bit by bit Examples:Examples:

ushort a = 3; // 00000000 00000011ushort a = 3; // 00000000 00000011ushort b = 5; // 00000000 00000101ushort b = 5; // 00000000 00000101Console.WriteLine( a | b); // 00000000 00000111Console.WriteLine( a | b); // 00000000 00000111Console.WriteLine( a & b); // 00000000 00000001Console.WriteLine( a & b); // 00000000 00000001Console.WriteLine( a ^ b); // 00000000 00000110Console.WriteLine( a ^ b); // 00000000 00000110Console.WriteLine(~a & b); // 00000000 00000100Console.WriteLine(~a & b); // 00000000 00000100Console.WriteLine( a<<1 ); // 00000000 00000110Console.WriteLine( a<<1 ); // 00000000 00000110Console.WriteLine( a>>1 ); // 00000000 00000001Console.WriteLine( a>>1 ); // 00000000 00000001

20

Page 21: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Bitwise OperatorsBitwise OperatorsLive DemoLive Demo

Page 22: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Comparison and Comparison and Assignment OperatorsAssignment Operators

Page 23: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Comparison OperatorsComparison Operators Comparison operators are used to compare Comparison operators are used to compare

variablesvariables

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

Comparison operators example:Comparison operators example:

int a = 5;int a = 5;int b = 4;int b = 4;Console.WriteLine(a >= b); // TrueConsole.WriteLine(a >= b); // TrueConsole.WriteLine(a != b); // TrueConsole.WriteLine(a != b); // TrueConsole.WriteLine(a == b); // FalseConsole.WriteLine(a == b); // FalseConsole.WriteLine(a == a); // TrueConsole.WriteLine(a == a); // TrueConsole.WriteLine(a != ++b); // FalseConsole.WriteLine(a != ++b); // FalseConsole.WriteLine(a > b); // FalseConsole.WriteLine(a > b); // False

23

Page 24: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Assignment OperatorsAssignment Operators

Assignment operators are used to assign a Assignment operators are used to assign a value to a variable ,value to a variable ,

==,, +=+=,, -=-=,, |=|=,, ...... Assignment operators example:Assignment operators example:

int x = 6;int x = 6;int y = 4;int y = 4;Console.WriteLine(y *= 2); // 8Console.WriteLine(y *= 2); // 8int z = y = 3; // y=3 and z=3 int z = y = 3; // y=3 and z=3 Console.WriteLine(z); // 3Console.WriteLine(z); // 3Console.WriteLine(x |= 1); // 7Console.WriteLine(x |= 1); // 7Console.WriteLine(x += 3); // 10Console.WriteLine(x += 3); // 10Console.WriteLine(x /= 2); // 5Console.WriteLine(x /= 2); // 5

24

Page 25: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Comparison and Comparison and Assignment OperatorsAssignment Operators

Live DemoLive Demo

Page 26: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Other OperatorsOther Operators

Page 27: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Other OperatorsOther Operators String concatenation operator String concatenation operator ++ is used to is used to

concatenate strings concatenate strings If the second operand is not a string, it is If the second operand is not a string, it is

converted to string automaticallyconverted to string automatically

string first = "First";string first = "First";string second = "Second";string second = "Second";Console.WriteLine(first + second); Console.WriteLine(first + second); // FirstSecond// FirstSecondstring output = "The number is : ";string output = "The number is : ";int number = 5;int number = 5;Console.WriteLine(output + number);Console.WriteLine(output + number);// The number is : 5// The number is : 5

27

Page 28: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Other Operators (2)Other Operators (2) Member access operator Member access operator .. is used to access is used to access

object membersobject members Square brackets Square brackets [][] are used with arrays are used with arrays

indexers and attributesindexers and attributes ParenthesesParentheses (( )) are used to override the are used to override the

default operator precedencedefault operator precedence Class cast operator Class cast operator (type)(type) is used to cast one is used to cast one

compatible type to anothercompatible type to another

28

Page 29: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Other Operators (3)Other Operators (3) Conditional operator Conditional operator ?:?: has the form has the form

(if (if bb is true then the result is is true then the result is xx else the result is else the result is yy))

The The newnew operator is used to create new objects operator is used to create new objects The The typeoftypeof operator returns operator returns System.TypeSystem.Type

object (the reflection of a type)object (the reflection of a type) The The isis operator checks if an object is operator checks if an object is

compatible with given typecompatible with given type

b ? x : yb ? x : y

29

Page 30: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Other Operators – ExampleOther Operators – Example Using some other operators:Using some other operators:

int a = 6;int a = 6;int b = 4;int b = 4;Console.WriteLine(a > b ? "a>b" : "b>=a"); // a>bConsole.WriteLine(a > b ? "a>b" : "b>=a"); // a>bConsole.WriteLine((long) a); // 6Console.WriteLine((long) a); // 6

int c = b = 3; // b=3; followed by c=3;int c = b = 3; // b=3; followed by c=3;Console.WriteLine(c); // 3Console.WriteLine(c); // 3Console.WriteLine(a is int); // TrueConsole.WriteLine(a is int); // TrueConsole.WriteLine((a+b)/2); // 4Console.WriteLine((a+b)/2); // 4Console.WriteLine(typeof(int)); // System.Int32Console.WriteLine(typeof(int)); // System.Int32

int d = new int();int d = new int();Console.WriteLine(d); // 0Console.WriteLine(d); // 0

30

Page 31: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Other OperatorsOther OperatorsLive DemoLive Demo

Page 32: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Implicit and Explicit Implicit and Explicit Type ConversionsType Conversions

Page 33: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Implicit Type ConversionImplicit Type Conversion Implicit Type ConversionImplicit Type Conversion

Automatic conversion of value of one data type Automatic conversion of value of one data type to value of another data typeto value of another data type

Allowed when no loss of data is possibleAllowed when no loss of data is possible

"Larger" types can implicitly take values of "Larger" types can implicitly take values of smaller "types"smaller "types"

Example:Example:

int i = 5;int i = 5;long l = i;long l = i;

33

Page 34: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Explicit Type ConversionExplicit Type Conversion Explicit type conversionExplicit type conversion

Manual conversion of a value of one data type Manual conversion of a value of one data type to a value of another data typeto a value of another data type

Allowed only explicitly by Allowed only explicitly by (type)(type) operator operator

Required when there is a possibility of loss of Required when there is a possibility of loss of data or precisiondata or precision

Example:Example:

long l = 5;long l = 5;int i = (int) l;int i = (int) l;

34

Page 35: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Type Conversions – ExampleType Conversions – Example Example of implicit and explicit conversions:Example of implicit and explicit conversions:

Note: Explicit conversion may be used even if Note: Explicit conversion may be used even if not required by the compilernot required by the compiler

float heightInMeters = 1.74f; // Explicit conversionfloat heightInMeters = 1.74f; // Explicit conversiondouble maxHeight = heightInMeters; // Implicitdouble maxHeight = heightInMeters; // Implicit

double minHeight = (double) heightInMeters; // Explicitdouble minHeight = (double) heightInMeters; // Explicit

float actualHeight = (float) maxHeight; // Explicitfloat actualHeight = (float) maxHeight; // Explicit

float maxHeightFloat = maxHeight; // Compilation error!float maxHeightFloat = maxHeight; // Compilation error!

35

Page 36: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Type ConversionsType ConversionsLive DemoLive Demo

Page 37: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

ExpressionsExpressions

Page 38: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

ExpressionsExpressions Expressions are sequences of operators, Expressions are sequences of operators,

literals and variables that are evaluated to literals and variables that are evaluated to some valuesome value

Examples:Examples:

int r = (150-20) / 2 + 5; // r=70int r = (150-20) / 2 + 5; // r=70

// Expression for calculation of circle area// Expression for calculation of circle areadouble surface = Math.PI * r * r;double surface = Math.PI * r * r;

// Expression for calculation of circle perimeter// Expression for calculation of circle perimeterdouble perimeter = 2 * Math.PI * r;double perimeter = 2 * Math.PI * r;

38

Page 39: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Expressions (2)Expressions (2) Expressions has:Expressions has:

Type (integer, real, boolean, ...)Type (integer, real, boolean, ...)

ValueValue

Examples:Examples:

int a = 2 + 3; // a = 5int a = 2 + 3; // a = 5int b = (a+3) * (a-4) + (2*a + 7) / 4; // b = 12int b = (a+3) * (a-4) + (2*a + 7) / 4; // b = 12bool greater = (a > b) || ((a == 0) && (b == 0));bool greater = (a > b) || ((a == 0) && (b == 0));

Expression of Expression of type type intint. .

Calculated at Calculated at compile time.compile time.

Expression Expression of type of type intint. . Calculated Calculated at runtime.at runtime.

Expression of type Expression of type boolbool. Calculated at . Calculated at

runtime.runtime. 39

Page 40: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

ExpressionsExpressionsLive DemoLive Demo

Page 41: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

SummarySummary We discussed the operators in C#:We discussed the operators in C#:

Arithmetic, logical, bitwise, comparison, Arithmetic, logical, bitwise, comparison, assignment and othersassignment and others

Operator precedence Operator precedence

We learned when to use implicit and explicit We learned when to use implicit and explicit type conversionstype conversions

We learned how to use expressionsWe learned how to use expressions

41

Page 42: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Questions?Questions?

Operators and ExpressionsOperators and Expressions

http://academy.telerik.com

Page 43: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

ExercisesExercises1.1. Write an expression that checks if given integer is Write an expression that checks if given integer is

odd or even.odd or even.

2.2. Write a boolean expression that checks for given Write a boolean expression that checks for given integer if it can be divided (without remainder) by 7 integer if it can be divided (without remainder) by 7 and 5 in the same time.and 5 in the same time.

3.3. Write an expression that calculates rectangle’s area Write an expression that calculates rectangle’s area by given by given widthwidth and and heightheight..

4.4. Write an expression that checks for given integer if Write an expression that checks for given integer if its third digit (right-to-left) is its third digit (right-to-left) is 77. E. g. . E. g. 17321732 truetrue..

5.5. Write a boolean expression for finding if the bit Write a boolean expression for finding if the bit 33 (counting from (counting from 00) of a given integer is ) of a given integer is 11 or or 00..

6.6. Write an expression that checks if given point (Write an expression that checks if given point (xx, , yy) ) is within a circle K(is within a circle K(OO, , 55).).

43

Page 44: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Exercises (2)Exercises (2)

1.1. Write an expression that checks if given positive Write an expression that checks if given positive integer number integer number nn ( (nn ≤≤ 100) is prime. E.g. 100) is prime. E.g. 3737 is prime. is prime.

E Write an expression that calculates trapezoid's area Write an expression that calculates trapezoid's area by given sides by given sides aa and and bb and height and height hh..

i Write an expression that checks for given point (x, Write an expression that checks for given point (x, y) if it is within the circle K( (y) if it is within the circle K( (11,,11), ), 33) and out of the ) and out of the rectangle R(top=rectangle R(top=11, left=, left=-1-1, width=, width=66, height=, height=22).).

Write a boolean expression that returns if the bit at Write a boolean expression that returns if the bit at position position pp (counting from (counting from 00) in a given integer ) in a given integer number number vv has value of has value of 11. Example: . Example: vv==55; ; pp==11 false.false. 44

Page 45: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Exercises (3)Exercises (3)

s Write an expression that extracts from a given Write an expression that extracts from a given integer integer ii the value of a given bit number the value of a given bit number bb. . Example: i=5; b=2 Example: i=5; b=2 value=1. value=1.

2.2. We are given integer number We are given integer number nn, value , value vv ( (vv=0 or 1) =0 or 1) and a position and a position pp. Write a sequence of operators that . Write a sequence of operators that modifies modifies nn to hold the value to hold the value vv at the position at the position pp from from the binary representation of the binary representation of nn..

Example: n = 5 (00000101), p=3, v=1 Example: n = 5 (00000101), p=3, v=1 13 13 (00001101)(00001101)

n = 5 (00000101), p=2, v=0 n = 5 (00000101), p=2, v=0 1 (00000001) 1 (00000001)45

Page 46: Operators and Expressions...2019/01/03  · Table of Contents 1. Operators in C# and Operator Precedence 2. Arithmetic Operators 3. Logical Operators 4. Bitwise Operators 5. Comparison

Exercises (4)Exercises (4)

1.1. Write a program that exchanges bits 3, 4 and 5 with Write a program that exchanges bits 3, 4 and 5 with bits 24,25 and 26 of given 32-bit unsigned integer.bits 24,25 and 26 of given 32-bit unsigned integer.

2.2. * Write a program that exchanges bits {p, p+1, …, * Write a program that exchanges bits {p, p+1, …, p+k-1) with bits {q, q+1, q+k-1} of given 32-bit p+k-1) with bits {q, q+1, q+k-1} of given 32-bit unsigned integer.unsigned integer.

46


Recommended