Download - Lecture 2
Objectives of this lecture
In this chapter you will learn: Increment and Decrement (++ and --) Assignment Operators Basic data types in C# Constant values Conditional logical operators
Data Types— 2
Main Mathematic Operations
Operator Action
+ Addition
- Subtraction
* Multiply
/ Division
% Reminder
++ Increment
-- Decrement
Data Types— 3
Increment & decrement (I)
C/C++/C# includes two useful operators not found in some other computer languages.
These are the increment and decrement operators, ++ and
The operator ++ adds 1 to its operand, and − − subtracts 1.
i++ = i + 1 i-- = i - 1
Both the increment and decrement operators may either precede (prefix) or follow (postfix) the operand
i = i+1 can be written as i++ or ++i i = i -1 can be written as i-- or --i Please note: There is, however, a difference between the
prefix and postfix forms when you use these operators in an expression
Data Types— 4
Increment & decrement (II)
// Increment and Decrement Operations ++/--using System; class Comparison { static void Main( string[] args ) { int a = 10; int b = 10; int x, y; x = a++; y = ++b; Console.WriteLine("x = {0}, a ={1}.", x, a); Console.WriteLine("y = {0}, b ={1}.", y, b); }// end method Main} // end class Comparison
x = 10 a = 11
y = 11 b = 11
Data Types— 5
Increment & decrement (III)
// Increment and Decrement Operations ++/--using System; class Comparison { static void Main( string[] args ) { int a = 10; int b = 10; Console.WriteLine(a++); Console.WriteLine(a); Console.WriteLine(++b); Console.WriteLine(b); }// end method Main} // end class Comparison
10
11
11
11
Data Types— 6
Common Programming Error 1
Attempting to use the increment or decrement operator on an expression other than a variable reference is a syntax error. A variable reference is a variable or expression that can appear on the left side of an assignment operation. For example, writing ++(x + 1) is a syntax error, because (x + 1) is not a variable reference
Data Types — 7
Assignment Operator (I) C# provides several assignment operators for
abbreviating assignment expressions. Example:
c = c + 3; c += 3;
where operator is one of the binary operators +, -, *, / or %, can be written in the form variable operator= expression;
Data Types — 9
Common Programming Error 1
Placing a space character between symbols that compose an arithmetic assignment operator is a syntax error.
A += 6; True A + = 6; False
Data Types — 11
Basic data types in C# (I) Programming languages store and process
data in various ways depending on the type of the data; consequently, all data read, processed, or written by a program must have a type
A data type is used to Identify the type of a variable when the variable is
declared Identify the type of the return value of a function
(later) Identify the type of a parameter expected by a
function (later)
Data Types— 13
Basic data types in C# (II) There are 7 major data types in C++
char e.g., ‘a’, ‘c’, ‘@’ string e.g., “Zagros” int e.g., 23, -12, 5, 0, 145678 float e.g., 54.65, -0.004, 65 double e.g., 54.65, -0.004, 65 bool only true and false void no value
Data Types— 14
Basic data types in C# (III)Type Range Size (bits)
char U+0000 to U+ffff Unicode 16-bit character
sbyte -128 to 127 Signed 8-bit integer
byte 0 to 255 Unsigned 8-bit integer
short -32,768 to 32,767 Signed 16-bit integer
ushort 0 to 65535 Unsigned 16-bit integer
int -2,147,483,648 to 2,147,483,647 Signed 32-bit integer
uint 0 to 4,294,967,295 Unsigned 32-bit integer
long -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
Signed 64-bit integer
ulong 0 to 18,446,744,073,709,551,615 Unsigned 64-bit integer
float -3.402823e38 .. 3.402823e38 Signed 32 bits
double -1.79769313486232e308 .. 1.79769313486232e308
Signed 64 bits
decimal -79228162514264337593543950335 .. 79228162514264337593543950335
Signed 128 bits
Data Types— 15
Basic data types in C# (IV) The general form of a declaration is
Type variable-list Examples
int I, j, k; char ch, a; float f, balance; double d; bool decision; string str;
Variables name The first character must be a letter -an underscore or @ The subsequent characters must be either letters, digits, or
underscores
Correct incorrect
Count 3count
test23 hi!there
high_balance high...balance
_name Test?
@count co@nt
Data Types— 16
Const variables (I) Variables of type const may not be changed by
your program The compiler is free to place variables of this type into
read-only memory (ROM). Example:
const int a = 10;
Data Types— 18
// Constant learningusing System; class Constant { static void Main( string[] args ) { const int c = 999; // c = 82; Error becuase c is constant and you cannot change it // c = 999; Error becuase c is constant and you cannot change it Console.WriteLine(c); }// end method Main} // end class Constant
Data Types— 19
Conditional Logical operators (I) Conditional logical operators are useful when we want to
test multiple conditions. There are 3 types of conditional logical operators and
they work the same way as the boolean AND, OR and NOT operators.
&& - Logical AND All the conditions must be true for the whole
expression to be true. Example: if (a == 10 && b == 9 && d == 1)
means the if statement is only true when a == 10 and b == 9 and d == 1.
Data Types— 21
expression1 expression2 expression1 && expression2
false false false false true false true false false true true true
Data Types— 22
Conditional Logical operators (II)
|| - Conditional logical OR The truth of one condition is enough to make
the whole expression true. Example: if (a == 10 || b == 9 || d == 1)
means the if statement is true when either one of a, b or d has the right value.
! – Conditional logical NOT (also called logical negation) Reverse the meaning of a condition Example: if (!(points > 90))
means if points not bigger than 90.
Data Types— 23
expression1 expression2 expression1 || expression2
false false false false true true true false true true true true
Expression !expression
false true true false
Data Types— 24
Data Types — 25
// Conditional logical operatorusing System; class Conditional_logical { static void Main( string[] args ) { int a = 10; int b = 15; if ((a == 10) && (b == 15)) Console.WriteLine("Apple"); else Console.WriteLine("Orange"); if ((a == 10) || (b == 15)) Console.WriteLine("Apple"); else Console.WriteLine("Orange"); if (!(a > 20)) Console.WriteLine("Apple"); else Console.WriteLine("Orange"); }// end method Main} // end class
Apple
Apple
Apple
Operator preferences
Operators Preferences
() 1
!,~,--,++ 2
*,/,% 3
+, - 4
<, <=, >=, > 5
==, != 6
&& 7
|| 8
Note: in a+++a*a, the * has preference over ++Note: in ++a+a*a, the ++ has preference over *
Data Types— 26
Common Programming Error Tip
Although 3 < x < 7 is a mathematically correct condition, it does not evaluate as you might expect in C#. Use ( 3 < x && x < 7 ) to get the proper evaluation in C#.
Using operator == for assignment and using operator = for equality are logic errors.
Use your text editor to search for all occurrences of = in your program and check that you have the correct assignment operator or logical operator in each place.
Data Types— 27