Transcript Document

C# Basics
& Math
Thanachat Thanomkulabut
Outline
2





Review
Data Types
Variable and Constant Declaration
Arithmetic Expression
Statement
C# Beginning
Lecture 1: Program Structure
3

The starting point of the program is:
namespace HelloW {
class HelloWClass {
static void Main () {
System.Console.WriteLine("Hello World!");
System.Console.ReadLine();
}
}
}



This is known as the method Main
A method is put inside a class
A class may be put inside a namespace
C# Beginning
Lecture 1: Variable Naming Rules
4




Letters, digits and underscores(_)
First character must be a letter or _
Up to 63 characters long
Must not be a reserved word
Example
name
Name
_data
class
point9
class_A
9point
class_"A"
Outline
5





Review
Data Types
Variable and Constant Declaration
Arithmetic Expression
Statement
C# Beginning
What is in the Program?
6


Variable declarations
Statements
static void Main(string[] args)
{
const double pi = 3.1416;
int radius;
double area;
radius = int.Parse(Console.ReadLine());
area = pi*radius*radius;
Console.WriteLine(area);
}
Variable & Constant
What is Variable?
7
A variable is used to store “data.”
“It must be declared before use”
Variable & Constant
Data Types
8
Type
bool
char
Size
1 byte
1 byte
Description
Store truth value
Store one character
byte
short
int
1 byte
2 byte
4 byte
Store positive integer 0 – 255
-32,768 -- 32,767
Store integer
Store integer
-2.1 x 109 -- 2.1 x 109
long
8 byte
double 16
byte
Store integer
Store real number
string
Store sequence of
characters
N/A
Range
true / false
character code 0 – 255
-9.2 x 1018 -- 9.2 x 1018
± 5.0x10-324 -± 1.7x10308
N/A
Outline
9





Review
Data Types
Variable and Constant Declaration
Arithmetic Expression
Statement
Variable & Constant
Variable Declaration
10

Syntax:
<data type> <name>;


Example:
int radius;
double area;
bool isokay;
We can also assign its initial value.
Example:
int k = 200;
bool done = false;
Variable & Constant
Multiple-Variable Declaration
11
Syntax:
<data type> <name_1>, <name_2>,..., <name_n>;
Example:
int width, length, height;
double mean, sd, max, min;
bool isokay, isright, check;
We can also assign their initial value.
Example:
int width=5, length=2, height=4;
Variable & Constant
Test : Variable Declaration
12

Declare variable1

Declare variable2
 Name
: num_Student
 Type : integer
 Initial value : nothing
 Name
: gender
 Type : character
 Initial value : m
int num_Student;
char gender = ‘m’;
Variable & Constant
Test : Multiple-Variable Declaration
13

Declare variables 3,4,5
 Name3
:u
Name4 : t
Name5 : a
 Type : double
 Initial value3 : 5.0
Initial value4 : nothing
Initial value5 : 9.8
double u=5.0, t, a=9.8;
Variable & Constant
Constant Declaration
14


Syntax:
const <data type> <name> = <value>;
Example:
const
const
const
const
const
int radius = 15;
double area=1.5;
bool is_done=true;
string movie=”StarWarIII”;
char my_initial=‘m’;
Variable & Constant
Multiple-Constant Declaration
15


Syntax:
const <data type> <name_1> = <value_1>,
<name_2> = <value_2>, ... ,
<name_n> = <value_n>;
Example:
const int radius = 15, height = 5;
const double area=1.5, width=3.2, length = 4.1;
Test : Constant Declaration
16

Declare Constant
 Name
:e
 Type : double
 Initial value : 2.71828
const double e=2.71828;
Outline
17





Review
Data Types
Variable and Constant Declaration
Arithmetic Expression
Statement
Expression
C# Expression
18
Expression
Arithmetic
Expression
Boolean
Expression
Arithmetic
Arithmetic Expression
19
Operators
+ - * /
% (modulo: remainder after division)
Integer operates Integer  Integer
Integer/Real operates Real/Integer  Real
Example
11 + 5 
39 / 5  16
39.0/5  7
39 % 5  7.8
5.0 % 2.2 4
0.6
Arithmetic
Example
20
static void Main(){
int a,b,c,d;
double e,f,g;
a=2; b=7; c=5;
d=c/a;
e=5/b;
f=5.0/2;
g=5/2.0;
}
Answer
d=2
e=0
f = 2.5
g = 2.5
In/Decrement
Pre/Post Increment & Decrement
21
 Pre in/de-crement:
 Increment/decremente the value first, then use the value
 Post in/de-crement:
 Use the value first, then increment/decrement the value
Operator
Meaning
example
++x
pre increment
int a = 5;
int b = ++a;
// a, b = 6
x++
post increment
int a = 5;
int b = a++;
// a = 6, b = 5
--x
pre decrement
int a = 5;
int b = --a;
// a, b = 4
x--
post decrement
int a = 5;
int b = a- - ; // a = 4, b = 5
In/Decrement
Pre & Post Increment
22
a
b
Monitor
Post:
a=6, b=5
5
6
5
int a=5;
int b=a++;
Console.WriteLine(“a={0}, b={1}”,a,b);
a
b
Monitor
Pre:
a=6, b=6
5
6
6
int a=5;
int b=++a;
Console.WriteLine(“a={0}, b={1}”,a,b);
Operators
เครื่องหมาย
ตัวแปร
+,-
1
เครื่ องหมายแสดงว่าเป็ นจานวนบวกหรื อจานวนลบ
-5, -3, -a, +b
+, -
2
การบวกและการลบตัวเลข
5+3 , a+10
*
2
การคูณตัวเลขสองจานวน (สาหรับจานวนเต็มสอง
จานวนคูณกันผลลัพธ์จะเป็ นจานวนเต็ม)
5*3, 10*0.1,
a*b , a+10*3
/
2
การหารตัวเลขสองจานวน (สาหรับจานวนเต็มสอง
จานวนหารกันผลลัพธ์ จะเป็ นจานวนเต็ม)
10/3 , 10.0/3,
b/a , a*b/3
%
2
หารเอาเฉพาะเศษที่เหลือ (ใช้ได้กบั จานวนเต็มเท่านั้น)
10%3 , 7%5 , a%b
++(x)
1
เพิ่มค่าตัวแปรอีก 1 แต่จะทาก่อนการคาสัง่ อื่น
a=10; b = ++a; (=>b=11)
--(x)
1
ลงค่าตัวแปรลงไป 1 แต่จะทาก่อนการคาสัง่ อื่น
a=10; b = --a;
(x)++
1
เพิ่มค่าตัวแปรอีก 1 แต่จะทาหลังคาสัง่ อื่น
a=10; b = a++; (=>b=10)
(x)--
1
ลดค่าตัวแปรลงไป 1 แต่จะทาหลังคาสัง่ อื่น
a=10; b = a--; (=>b=10)
=
2
การให้ค่าตัวแปรทางซ้ายจากค่าผลลัพธ์ทางขวา
a = 10+1
+
2
ใช้กบั สายข้อความเป็ นการนามาต่อกัน
st = “Hello” + “World”
23
ความหมาย
ตัวอย่ าง
(=>b=9)
Assignment Operator
24

ทัว่ ไปใช้คำสั่ ง = ในกำรให้คำตั
่ วแปรทำงซ้ำย แต่
สำมำรถลดรูปไดดั
้ งนี้
เครื่องหมาย
ตัวอย่ าง
ผลลัพธ์
+=
x += y
x = x+ y
-=
x -= y
x = x- y
*=
x *= y
x = x* y
/=
x /= y
x = x/ y
%=
x %= y
x = x% y
++
x++ , ++x
x = x+1
--
x-- , --x
x = x-1
Operator Priority
25
Highest precedence (performed first)
()
++(x) , --(x), +(x), -(x)
*/%
+= , += , -=, *=, /=, %=
Lowest precedence (performed later)
(x)++ , (x)--
If operator priority is equal, arithmetic calculation is
performed from left to right
Arithmetic
Priority of Arithmetic Operators
26
Priority
Operator
1
++x, --x
2
Parentheses ()
3
*, / , %
4
+, -
5
If equal priority, work from left to right
Arithmetic
Example
27
int a, b, c;
a = 2-10*3/5+(6%4);
b = 5*(15%4+(2-3))/9;
c = 5+9%2*4-14/4%3
Answer
a = -2
b=1
c=9
Comparison
28
Given x = 3.0, y = 2.0, b =2, what is the output?
 x/y*++b
3
 Answer:
 x/y*b++
2
4.5
 Answer:
3.0
Modify-And-Assign
Modify-and-Assign Operations
29
Statement
var += expression
Description
Increment var by the value of expression
var -= expression
Decrement var by the value of expression
var *= expression
Multiply var by the value of expression,
then store the result in var
var /= expression
Divide var by the value of expression,
then store the result in var
sum += x;
// is equivalent to sum = sum + x
prod *= 2.5; // is equivalent to prod = prod * 2.5
y -= 3+a;
// is equivalent to y = y – (3+a)
Modify-And-Assign
Example
30
double x = 1, sum = 2, prod = 3;
int y = 4, a = 5;
sum += x;
// is equivalent to sum = sum + x
prod *= 2.5; // is equivalent to prod = prod * 2.5
y -= 3+a;
// is equivalent to y = y – (3+a)
Console.WriteLine(sum);
Monitor
Console.WriteLine(prod);
3
7.5
-4
Console.WriteLine(y);
Math Class
Math Class
31
Method/
Constant
Value returned
Example Call
Result
PI
Value of π
Math.PI
3.1415927
Max(x,y)
Larger of the two
Math.Max(1,2)
2
Abs(x)
Absolute value of x
Math.Abs(-1.3)
1.3
Sqrt(x)
Square-root of x
Math.Sqrt(4.0)
2.0
Round(x)
Nearest integer to x
Math.Round(0.8)
1
Pow(x,y)
xy
Math.Pow(3,2)
9.0
Log(x)
Natural log of x = ln x
Math.Log(10)
2.302585
Floor(x)
Greatest integer smaller than or equal to x
Math.Floor(4.9)
4
Ceiling(x)
Smallest integer greater than or equal to x
Math.Ceiling(4.1)
5
Cos(x)
Cosine of x radians
Math.Cos(Math.PI)
-1
Example
32

What is the output?
Console.WriteLine(Math.Round(3.499));
Console.WriteLine(Math.Floor(3.499));
Console.WriteLine(Math.Ceiling(3.499));
Monitor
3
3
4
Example
33
Write arithmetic expressions using Math class
 cos(π/3)
 1/ 5 3
| ln(4) |
Math.Cos(Math.PI/3)
Math.Pow(3,0.2)
Math.Abs(Math.Log(4))
Outline
34





Review
Data Types
Variable and Constant Declaration
Arithmetic Expression
Statement
Statement
Statement
35

A statement is a command to computer
Statement#1
class Hello {
static void Main () {
Console.WriteLine("Hello World!");
Console.ReadLine();
}
}
Statement#2
Statement
C# Statement Types
36
C#
Statement
Types
Assignment
Statement
Input
Statement
Output
Statement
Statement
Assignment Statement
37
To assign value to variable
 Use equal sign (=)
 Syntax:

<variable> = <expression>;
int Width,Height;
Width=10;
Height=20+Width;
Statement
Output Statement
38

Value
Console.WriteLine("Hello");
Console.WriteLine(2012);

Math expression
Console.WriteLine(a);
Console.WriteLine(a+b);
Console.WriteLine(Math.Pow(a,b));

More usages of output statement will be showed
later in this course
Any question?