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

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

C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement.

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


งานนำเสนอเรื่อง: "C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement."— ใบสำเนางานนำเสนอ:

1 C# Basics & Math Thanachat Thanomkulabut

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

3 Lecture 1: Program Structure 3  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 C# Beginning namespace HelloW { class HelloWClass { static void Main () { System.Console.WriteLine("Hello World!"); System.Console.ReadLine(); } namespace HelloW { class HelloWClass { static void Main () { System.Console.WriteLine("Hello World!"); System.Console.ReadLine(); }

4 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 4 Example nameName _data 9point class class_Aclass_"A" point9 C# Beginning

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

6 6 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); } C# Beginning

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

8 Data Types TypeSizeDescriptionRange bool1 byteStore truth valuetrue / false char1 byteStore one charactercharacter code 0 – 255 byte1 byteStore positive integer0 – 255 short2 byteStore integer-32, ,767 int4 byteStore integer-2.1 x x 10 9 long8 byteStore integer-9.2 x x double16 byte Store real number± 5.0x ± 1.7x stringN/AStore sequence of characters N/A Variable & Constant 8

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

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

11 Multiple-Variable Declaration 11  Syntax:,,..., ;  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; Variable & Constant

12 Test : Variable Declaration 12  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’; Variable & Constant

13 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

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

15 Multiple-Constant Declaration  Syntax: const =, =,..., = ;  Example: const int radius = 15, height = 5; const double area=1.5, width=3.2, length = 4.1; Variable & Constant 15

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

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

18 C# Expression 18 Expression Arithmetic Expression Boolean Expression 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  Arithmetic

20 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; } d = 2 e = 0 f = 2.5 g = 2.5 Answer Arithmetic

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

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

23 23 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; (=>b=9) (x)++1 เพิ่มค่าตัวแปรอีก 1 แต่จะทำหลัง คำสั่งอื่น a=10; b = a++; (=>b=10) (x)--1 ลดค่าตัวแปรลงไป 1 แต่จะทำหลัง คำสั่งอื่น a=10; b = a--; (=>b=10) =2 การให้ค่าตัวแปรทางซ้ายจากค่า ผลลัพธ์ทางขวา a = ใช้กับสายข้อความเป็นการนำมาต่อ กัน st = “ Hello ” + “ World ”

24 24 Assignment Operator  ทั่วไปใช้คำสั่ง = ในการให้ค่าตัวแปรทางซ้าย แต่สามารถ ลดรูปได้ดังนี้ เครื่องหม าย ตัวอย่างผลลัพธ์ +=+=x += yx = x+ y -=x -= yx = x- y *=x *= yx = x* y /=x /= yx = x/ y %=x %= yx = x% y ++x++, ++xx = x+1 --x--, --xx = x-1

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

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

27 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 a = -2 b = 1 c = 9 Answer 27 Arithmetic

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

29 Modify-and-Assign Operations StatementDescription 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) Modify-And-Assign 29

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

31 Math Class Method/ Constant Value returnedExample CallResult PIValue of πMath.PI Max(x,y)Larger of the twoMath.Max(1,2)2 Abs(x)Absolute value of xMath.Abs(-1.3)1.3 Sqrt(x)Square-root of xMath.Sqrt(4.0)2.0 Round(x)Nearest integer to xMath.Round(0.8)1 Pow(x,y)xyxy Math.Pow(3,2)9.0 Log(x)Natural log of x = ln xMath.Log(10) Floor(x)Greatest integer smaller than or equal to xMath.Floor(4.9)4 Ceiling(x)Smallest integer greater than or equal to xMath.Ceiling(4.1)5 Cos(x)Cosine of x radiansMath.Cos(Math.PI) Math Class 31

32 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

33 Example 33  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 34  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement

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

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

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

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

39 Any question?


ดาวน์โหลด ppt C# Basics & Math Thanachat Thanomkulabut. Outline 2  Review  Data Types  Variable and Constant Declaration  Arithmetic Expression  Statement.

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


Ads by Google