Download presentation
Presentation is loading. Please wait.
Published byJulianna Carpenter Modified over 9 years ago
1
What are Operators? Some useful operators to get you started
2
What’s an Operator? Definition: An operator is a symbol that provides a shorthand way of telling your program to perform a computation or other type of action Unary operators: applied to a single variable or expression E.g., -y or j++ Binary operators: applied to two expressions E.g., x+y, x<y, x=y
3
Spreadsheet Example
4
Common Operators for Numbers Arithmetic +, -, *, / % gives remainder for integer division ++ (--) can be used to increment (decrement) values, and are unary operators Comparison == and != >, >=, <, <= Assignment = += (and -=, *=, /=) Negation (minus sign)
5
Numeric Examples
6
Assignment Issues Don’t mix up the assignment (=) and equality test (= =) operators Assignment moves RHS value into LHS variable LHS changes Equality test checks if LHS equals RHS Returns true if the are, false if not Neither side changes
7
Integer Division When integers are divided, the result is truncated 17/5 3 19/5 3 4/5 0 Remainders can be obtained with % (modulus) operator 17%5 2 19%5 4 4%5 4
8
Mixing Integers & Real Numbers Review: integers and real numbers have vastly different ranges of values Operations that mix integers and real numbers could exceed integer ranges Solution: Generally, when operators mix integers and real numbers, the values are “promoted” to real numbers before the operation takes place
9
Typecast C# will not let you assign values of one type to another type if it could cause problems—even if you “know” it is okay Typical example: assigning a double to an int int Test = 3.0; // will produce an error! Typecast operator: Unary operator puts the value type in parentheses in front of expression to eliminate warning int Test = (int)3.0; // Eliminates error Typecasts can also be used between compatible objects, as we’ll see later in the course
10
Typecast Example Notes: Only typecast when you are sure that you’ve got compatible types If they aren’t compatible (e.g., typecasting a negative number to an unsigned integer) the results will either be “unpredictable” or it will crash your program with an exception. Neither is good…
11
Common Operators for Strings Concatenation + “Hello” + “World” gives “HelloWorld” Comparison == and != These comparisons are case sensitive! Assignment = +=
12
string Operator Example Notes: Comparisons == and != test exact equality—they are case sensitive (like everything in C#) Because of case sensitivity, comparisons such as < and <= are not supported
13
Common Operators for Booleans Logical binary operators && : returns true if both sides are true || : returns true if either side is true Negation ! : Makes a true expression false or a false expression true Comparison == and != Conditional Boolean-expression ? Value1 : Value2 Assignment =
14
Conditional Operator Identical in usage to Excel =if( test, value-if-true, value-if-false) int X=17; int Y=12; string z=(X>Y) ? “X > Y” : “X <= Y”; int X=17; int Y=12; int z=(X>Y) ? X-Y : Y-X;
15
bool Operator Example
16
Parting words… Operators are generally intuitive once you start working with them Operators have “precedence” (e.g., * gets done before +) but use parentheses if there’s any doubt Remember that operators “return” a value Sometimes it’s the same type (e.g., x+y) Sometimes it’s different (e.g., x>y)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.