งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

Thanachat Thanomkulabut

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "Thanachat Thanomkulabut"— ใบสำเนางานนำเสนอ:

1 Thanachat Thanomkulabut
C# Basics & Math Thanachat Thanomkulabut

2 Outline Review Data Types Variable and Constant Declaration
Arithmetic Expression Statement

3 Lecture 1: Program Structure
C# Beginning Lecture 1: Program Structure The starting point of the program is: This is known as the method Main A method is put inside a class A class may be put inside a namespace namespace HelloW { class HelloWClass { static void Main () { System.Console.WriteLine("Hello World!"); System.Console.ReadLine(); }

4 Lecture 1: Variable Naming Rules
C# Beginning Lecture 1: Variable Naming Rules 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 point9 9point _data class class_A class_"A"

5 Outline Review Data Types Variable and Constant Declaration
Arithmetic Expression Statement

6 What is in the Program? Variable declarations Statements
C# Beginning What is in the Program? Variable declarations Statements static void Main(string[] args) { const double pi = ; int radius; double area; radius = int.Parse(Console.ReadLine()); area = pi*radius*radius; Console.WriteLine(area); }

7 A variable is used to store “data.”
Variable & Constant What is Variable? A variable is used to store “data.” “It must be declared before use”

8 Data Types Type Size Description Range bool 1 byte Store truth value
Variable & Constant Data Types Type Size Description Range bool 1 byte Store truth value true / false char Store one character character code 0 – 255 byte Store positive integer 0 – 255 short 2 byte Store integer -32, ,767 int 4 byte -2.1 x x 109 long 8 byte -9.2 x x 1018 double 16 byte Store real number ± 5.0x ± 1.7x10308 string N/A Store sequence of characters

9 Outline Review Data Types Variable and Constant Declaration
Arithmetic Expression Statement

10 Variable Declaration Syntax: Example:
Variable & Constant Variable Declaration Syntax: <data type> <name>; Example: We can also assign its initial value. Example: int radius; double area; bool isokay; int k = 200; bool done = false;

11 Multiple-Variable Declaration
Variable & Constant Multiple-Variable Declaration Syntax: <data type> <name_1>, <name_2>,..., <name_n>; Example: We can also assign their initial value. Example: int width, length, height; double mean, sd, max, min; bool isokay, isright, check; int width=5, length=2, height=4;

12 Test : Variable Declaration
Variable & Constant Test : Variable Declaration Declare variable1 Name : num_Student Type : integer Initial value : nothing Declare variable2 Name : gender Type : character Initial value : m int num_Student; char gender = ‘m’;

13 Test : Multiple-Variable Declaration
Variable & Constant Test : Multiple-Variable Declaration 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;

14 Constant Declaration Syntax: Example:
Variable & Constant Constant Declaration Syntax: const <data type> <name> = <value>; Example: const int radius = 15; const double area=1.5; const bool is_done=true; const string movie=”StarWarIII”; const char my_initial=‘m’;

15 Multiple-Constant Declaration
Variable & Constant Multiple-Constant Declaration 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;

16 Test : Constant Declaration
Declare Constant Name : e Type : double Initial value : const double e= ;

17 Outline Review Data Types Variable and Constant Declaration
Arithmetic Expression Statement

18 Arithmetic Expression
Boolean Expression

19 Arithmetic Expression
Operators + - * / % (modulo: remainder after division) Integer operates Integer  Integer Integer/Real operates Real/Integer  Real Example 39 / 5  39.0/5  39 % 5  5.0 % 2.2  16 7 7.8 4 0.6

20 Example static void Main(){ int a,b,c,d; double e,f,g; a=2; b=7; c=5;
Arithmetic Example 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

21 Pre/Post Increment & Decrement
In/Decrement Pre/Post Increment & Decrement 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 b = a++; // a = 6, b = 5 --x pre decrement int b = --a; // a, b = 4 x-- post decrement int b = a- - ; // a = 4, b = 5

22 Pre & Post Increment a b Post: int a=5; int b=a++;
In/Decrement Pre & Post Increment a b Post: int a=5; int b=a++; Console.WriteLine(“a={0}, b={1}”,a,b); Monitor 5 6 5 a=6, b=5 a b Pre: int a=5; int b=++a; Console.WriteLine(“a={0}, b={1}”,a,b); Monitor 6 5 6 a=6, b=6

23 Operators เครื่องหมาย ตัวแปร ความหมาย ตัวอย่าง
+ , - 1 เครื่องหมายแสดงว่าเป็นจำนวนบวกหรือจำนวนลบ -5, -3, -a, +b +, - 2 การบวกและการลบตัวเลข 5+3 , a+10 * การคูณตัวเลขสองจำนวน (สำหรับจำนวนเต็มสองจำนวนคูณกันผลลัพธ์จะเป็นจำนวนเต็ม) 5*3, 10*0.1, a*b , a+10*3 / การหารตัวเลขสองจำนวน (สำหรับจำนวนเต็มสองจำนวนหารกันผลลัพธ์จะเป็นจำนวนเต็ม) 10/3 , 10.0/3, b/a , a*b/3 % หารเอาเฉพาะเศษที่เหลือ (ใช้ได้กับจำนวนเต็มเท่านั้น) 10%3 , 7%5 , a%b ++(x) เพิ่มค่าตัวแปรอีก 1 แต่จะทำก่อนการคำสั่งอื่น a=10; b = ++a; (=>b=11) --(x) ลงค่าตัวแปรลงไป 1 แต่จะทำก่อนการคำสั่งอื่น a=10; b = --a; (=>b=9) (x)++ เพิ่มค่าตัวแปรอีก 1 แต่จะทำหลังคำสั่งอื่น a=10; b = a++; (=>b=10) (x)-- ลดค่าตัวแปรลงไป 1 แต่จะทำหลังคำสั่งอื่น a=10; b = a--; (=>b=10) = การให้ค่าตัวแปรทางซ้ายจากค่าผลลัพธ์ทางขวา a = 10+1 + ใช้กับสายข้อความเป็นการนำมาต่อกัน st = “Hello” + “World”

24 Assignment Operator ทั่วไปใช้คำสั่ง = ในการให้ค่าตัวแปรทางซ้าย แต่ สามารถลดรูปได้ดังนี้ เครื่องหมาย ตัวอย่าง ผลลัพธ์ += 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

25 Operator Priority ( ) ++(x) , --(x), +(x), -(x) * / % + - = , += , -=, *=, /=, %= (x)++ , (x)-- Highest precedence (performed first) Lowest precedence (performed later) If operator priority is equal, arithmetic calculation is performed from left to right

26 Priority of Arithmetic Operators
1 ++x, --x 2 Parentheses () 3 *, / , % 4 +, - 5 If equal priority, work from left to right

27 Example int a, b, c; a = 2-10*3/5+(6%4); b = 5*(15%4+(2-3))/9;
Arithmetic Example 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

28 Comparison x/y*++b x/y*b++ Answer: 4.5 Answer: 3.0
Given x = 3.0, y = 2.0, b =2, what is the output? x/y*++b x/y*b++ 3 2 Answer: 4.5 Answer: 3.0

29 Modify-and-Assign Operations
Statement Description var += expression 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)

30 Example 3 7.5 -4 double x = 1, sum = 2, prod = 3; int y = 4, a = 5;
Modify-And-Assign Example 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); Console.WriteLine(prod); Console.WriteLine(y); Monitor 3 7.5 -4

31 Math Class Math Class Method/ Constant Value returned Example Call
Result PI Value of π Math.PI 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) 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

32 Example What is the output? 3 4 Monitor
Console.WriteLine(Math.Round(3.499)); Console.WriteLine(Math.Floor(3.499)); Console.WriteLine(Math.Ceiling(3.499)); 3 4

33 Example cos(π/3) | ln(4) |
Write arithmetic expressions using Math class cos(π/3) | ln(4) | Math.Cos(Math.PI/3) Math.Pow(3,0.2) Math.Abs(Math.Log(4))

34 Outline Review Data Types Variable and Constant Declaration
Arithmetic Expression Statement

35 Statement A statement is a command to computer Statement#1
class Hello { static void Main () { Console.WriteLine("Hello World!"); Console.ReadLine(); } Statement#2

36 C# Statement Types C# Statement Types Assignment Statement
Input Statement Output Statement

37 Assignment Statement To assign value to variable Use equal sign (=)
Syntax: <variable> = <expression>; int Width,Height; Width=10; Height=20+Width;

38 Output Statement Value Math expression
More usages of output statement will be showed later in this course Console.WriteLine("Hello"); Console.WriteLine(2012); Console.WriteLine(a); Console.WriteLine(a+b); Console.WriteLine(Math.Pow(a,b));

39 Any question?


ดาวน์โหลด ppt Thanachat Thanomkulabut

งานนำเสนอที่คล้ายกัน


Ads by Google