DONG NAI UNIVERSITY OF TECHNOLOGY
DONG NAI UNIVERSITY OF TECHNOLOGY
Working with Variables,
Operators and Expressions
DONG NAI UNIVERSITY OF TECHNOLOGY
1
Statements
2
-Comments
3
Identifiers
4
Primitive Data Types
5
Variables
Arithmetic
Operators
6
Incrementing &
Decrementing Variables
7
Implicitly Typed Local
Variables
DONG NAI UNIVERSITY OF TECHNOLOGY
-A Statement is a command that performs an action,
you must add “;” the end of command
-Ex: Console.Write(“Hello C# ”);
1
Statements
-Comments
-Comments use to explain for your Coding
How to write comments???
See next slide…
DONG NAI UNIVERSITY OF TECHNOLOGY
//Comment 1
/// <summary>
///Write Comment 4
/*
/// </summary>
*Comment 2
///
</param>
*/
///
</param>
private void func1(int a,float b)
/*Comment 3*/
DONG NAI UNIVERSITY OF TECHNOLOGY
Identifiers are names use to identify the elements in
program, ex: namespace, class, method…
Syntax Rules:
2
Identifiers
-Only use letter, digit, underscore
-An identifier must start with a letter or an underscore
-C# is a case –sensitive : nTest and ntest is different
identifier
DONG NAI UNIVERSITY OF TECHNOLOGY
Identifying Keywords
abstract
do
in
protected
as
double
int
base
else
interface
readonly
bool
enum
internal
ref
break
event
is
return
ulong
byte
explicit
lock
sbyte
unchecked
case
extern
long
sealed
unsafe
catch
false
namespace
short
ushort
char
finally
new
sizeof
using
checked
fixed
public
true
null
class
float
object
const
for
operator
foreach
out
decimal
goto
override
if
params
implicit
private
dynamic
join
set
from
let
value
get
orderby
group
partial
where
into
select
yield
var
virtual
void
string
volatile
struct
while
switch
this
delegate
typeof
uint
stackalloc
static
continue
default
try
throw
DONG NAI UNIVERSITY OF TECHNOLOGY
- A variable is storage location that holds a value. It as a
box in the Computer’s memory holding temporary
information.
-To easy coding:
3
+Don’t start an identifier with underscore
Variables
+Don’t different only by case ( nTest and ntest)
+Start name with lower case
+….
DONG NAI UNIVERSITY OF TECHNOLOGY
How to declare variables?
Data_type NameVariable;
int nNumberOfElement;
string strFullName;
float fValue=0,fResult;
DONG NAI UNIVERSITY OF TECHNOLOGY
Primitive Data Types
4
Data Type
Description
Size(bits)
int
Whole numbers
32
Range
31
31
–2 through 2 – 1
64
63
63
–2 through 2 – 1
long lSize;
32
45
38
±1.5 × 10 through ±3.4 × 10
float fDelta;
64
±5.0 × 10
long
float
Whole numbers
(bigger range)
Floating-point numbers
Double-precision (more
double
accurate) floating-point
−324
through ±1.7 × 10
Example
int nSize;
308
double dDelta;
numbers
decimal
Monetary values
128
28 significant figures
decimal decKe;
string
Sequence of characters
16 bits per character
Not applicable
string strName;
char
Single character
16
0 through 2
bool
Boolean
8
True or false
16
–1
char chrAns;
bool bRet;
DONG NAI UNIVERSITY OF TECHNOLOGY
You should initialize value for variable
int nSize=0;
String strName=“”;
DONG NAI UNIVERSITY OF TECHNOLOGY
5
Arithmetic
Operators
+
-
*
/
%
()
int nPlusTwoNumber=113 + 114
The + is the operator
113 and 114 are the operands
DONG NAI UNIVERSITY OF TECHNOLOGY
Not all operators are applicable to all data
types
Example:
You will get compiler error with the
statements:
Console.WriteLine(“Tý” - “Tèo”);
DONG NAI UNIVERSITY OF TECHNOLOGY
How to convert from String to Number Format ?
int nValue=System.Int32.Parse("113");
double dValue = System.Double.Parse("3.5");
Datatype vName=System.DataTypeObject.Parse(“string”);
DONG NAI UNIVERSITY OF TECHNOLOGY
X++
++X
Postfix increment
Prefix increment
X--
--X
Postfix decrement
Prefix decrement
6
Incrementing &
Decrementing Variables
DONG NAI UNIVERSITY OF TECHNOLOGY
7
Implicitly Typed Local
Variables
var number1=1;
var strAny=“tèo”;
var sAny;
Error: Implicitly-typed local variables must be initialized
DONG NAI UNIVERSITY OF TECHNOLOGY
END