Module 2 C Basic Concept Thanawin Rakthanmanon Email
Module 2 C# Basic Concept Thanawin Rakthanmanon Email: fengtwr@ku. ac. th Computer Engineering Department Kasetsart University, Bangkok THAILAND 1 nd Semester 200 7 1
Outline C# Overview ¡ Variable and Constant ¡ Expression ¡ Statement ¡ Modify-And-Assign ¡ Math Class ¡ 1 nd Semester 200 7 2
C# Program ¡ Consider all the programs you wrote in Lab#0 namespace Hello. W { class Hello. WClass { static void Main () { System. Console. Write. Line("Hello World!"); System. Console. Read. Line(); } } } ¡ What C#'s programming rules can you derive? 1 nd Semester 200 7 3
C# Program C# syntax is case-sensitive ¡ Every statement ends with a semicolon ; ¡ White space means nothing ¡ Code block is inside braces { } ¡ Anything between /* */ or after // is considered a comment ¡ l Comments will not be translated 1 nd Semester 200 7 4
Program Structure ¡ The starting point of the program is: static void Main () {. . . starting point. . . } This is known as the method Main ¡ A method is put inside a class ¡ A class may be put inside a namespace ¡ 1 nd Semester 200 7 5
Program Structure ¡ In C# l l l ¡ A program can contain several namespaces A namespace can contain several classes A class can contain several methods In other words l l Think of a namespace as a container of classes Think of a class as a container of methods method 1 method 2 Class namespace Class 1 nd Semester 200 7 6
Program Structure (example) namespace Hello. W { class Hello. WClass { static void Main () { System. Console. Write. Line(“Hello"); } } } method 1 method 2 Class method 3 Class namespace method 1 method 2 Class : : 1 nd Semester 200 7 7
Program Structure namespace Hello. W { class Hello. WClass { static void Main () { System. Console. Write. Line("Hello World!"); System. Console. Read. Line(); } } } ¡ For this 204111 course l ¡ Program with only one class and at most one namespace For now until sometime before midterm l Program with one method (i. e. , Main) 1 nd Semester 200 7 8
C# Overview Naming Rules Letters, digits and underscores(_) ¡ First character letter or _ ¡ Up to 63 characters long ¡ Must not be a reserved word ¡ * Case Sensitive Example KU 66 ≠ ku 66 1 nd Semester 200 7 9
C# Overview C# Reserved Words 1 nd Semester 200 7 10
Short break – 3 Minutes 1 nd Semester 200 7 11
Outline C# Overview ¡ Variable and Constant ¡ Expression ¡ Statement ¡ Modify-And-Assign ¡ Math Class ¡ 1 nd Semester 200 7 12
Variable & Constant What is Variable? A variable is used to store “data. ” “It must be declaredbefore used” 1 nd Semester 200 7 13
Variable & Constant C# Variable Declaration ¡ Syntax: <data type> <name>; ¡ ¡ Example: int radius; double area; int a, b, c; bool isokay; We can also assign its initial value. Example: int k = 200; bool done = false; 1 nd Semester 200 7 14
Data Types Type Size Description bool 1 byte Store truth value true / false char 1 byte Store one character code 0 – 255 byte 1 byte Store positive integer 0 – 255 short 2 byte Store integer -32, 768 -- 32, 767 int 4 byte Store integer -2. 1 x 109 -- 2. 1 x 109 long 8 byte Store integer -9. 2 x 1018 -- 9. 2 x 1018 double 16 byte Store real number ± 5. 0 x 10 -324 -- ± 1. 7 x 10308 string N/A Store sequence of characters N/A 1 nd Semester 200 7 Range 15
Variable & Constant C# Constant Declaration ¡ Syntax: const <data type> <name> = <value>; ¡ Example: const 1 nd Semester 200 7 int radius = 15; double area=1. 5; bool isokay=true; string movie=”Star. War. III”; 16
Outline C# Overview ¡ Variable and Constant ¡ Expression ¡ Statement ¡ Modify-And-Assign ¡ Math Class ¡ 1 nd Semester 200 7 17
Expression C# Expression Arithmetic Expression ¡ Boolean Expression ¡ 1 nd Semester 200 7 18
Expression Arithmetic Expression ¡ ¡ Operators l+ - * / l % (remainder after division) Example l l 11 + 5 16 11 / 2 5 11 % 2 1 5. 0 % 2. 2 0. 6 1 nd Semester 200 7 19
Precedence rules for Arithmetic Operators 1. 2. 3. 4. ( ) parentheses *, / , % + – If equal precedence, left to right int Width, High; Width = 10*5+(16*12)/5; High= (16+5)+20%2; 1 nd Semester 200 7 20
Calculation Priority Calculate from left to right! Console. Write. Line(3/4*8); = Console. Write. Line((3/4)*8); = 0 (=? 3 x 8 ? ? ? 4 ) public static void Main(){ int a, b, c, d; a=1; b=2; c=3; d = c/b*a; Console. Write. Line("d={0}", d); d = a/b; Console. Write. Line("d={0}", d); } 1 nd Semester 200 7 21
Expression in C# Arithmetic Expression ¡ Boolean Expression ¡ 1 nd Semester 200 7 22
Expression Boolean Expression ¡ Operators l Comparison Equal = ¡ Not equal != ¡ Less < ¡ Greater > ¡ Less than or equal to <= ¡ Greater than or equal to >= ¡ l Boolean And && ¡ Or || ¡ Not ! ¡ 1 nd Semester 200 7 0 0 1 1 and and 0 1 = = 0 0 0 1 1 or or 0 1 = = 0 1 1 1 not 0 = 1 not 1 = 0 23
Example: Boolean Expression 50 > 10 ¡ ’A’ < ’B’ ¡ ’ANT’ < ’B’ ¡ ’ 5’ > ’ 10’ ¡ ’ 5’ > ’ 100’ ¡ 1 nd Semester 200 7 true true 24
Quiz ¡ ((1 and 0) or (1 and 1)) = ? What is I/O device ? ¡ What is RAM and ROM ? ¡ 1 byte = xxx bits ¡ 1 Mbyte = ? ¡ C# is xxx language. ¡ 10000102 = ? ¡ 1 nd Semester 200 7 25
Short break – 5 Minutes 1 nd Semester 200 7 26
Outline C# Overview ¡ Variable and Constant ¡ Expression ¡ Statement ¡ Modify-And-Assign ¡ Math Class ¡ 1 nd Semester 200 7 27
Statements ¡ A statement is a unit of command to instruct your program Statement#1 Statement#2 ¡ class Hello { static void Main () { System. Console. Write. Line("Hello World!"); System. Console. Read. Line(); } } A method consists of one or more statements 1 nd Semester 200 7 28
Statement C# Statement Types Assignment Statement ¡ Input Statement ¡ Output Statement ¡ 1 nd Semester 200 7 29
Statement Assignment Statement Assigning value to variable ¡ Use the equal sign (=)when making assignments. ¡ Syntax: ¡ <variable> = <expression >; int Width, High; Width=10; High=5; 1 nd Semester 200 7 int Width = 10; int High = 5; 30
Statement C# Statement Types Assignment Statement ¡ Input Statement ¡ Output Statement ¡ 1 nd Semester 200 7 31
Statement Input Statement ¡ Console. Read. Line() Return string l Use to get the input from user Example string st; st = System. Console. Read. Line(); ¡ Convert string to other data type l l int. Parse() Convert string to integer double. Parse () Convert string to double 1 nd Semester 200 7 32
Example: Input Statement string st; int i; double d; 1. st = Console. Read. Line(); i = int. Parse(st); 2. st = Console. Read. Line(); d = double. Parse(st); 3. i = int. Parse(Console. Read. Line()); 4. d = double. Parse(Console. Read. Line()); 1 nd Semester 200 7 33
Statement Example: Input Statement Ex 1: string myname; myname = System. Console. Read. Line(); Ex 2: int Width, High; string temp 1; temp 1 = System. Console. Read. Line(); Width = int. Parse(temp 1); temp 1 = System. Console. Read. Line(); High = int. Parse(temp 1); 1 nd Semester 200 7 34
Statement Example: Input Statement 1 nd Semester 200 7 35
Statement C# Statement Types Assignment Statement ¡ Input Statement ¡ Output Statement ¡ 1 nd Semester 200 7 36
Output Statements ¡ ¡ Use the method Write or Write. Line in the Console class (which is in System namespace) Basic usage: Console. Write. Line("Hello"); Console. Write. Line(area); ¡ Advanced usage: Console. Write. Line(”Size {0}x{1}”, width, height); ¡ Even more advanced usage: double salary=12000; Console. Write. Line("My salary is {0: f 2}. ", salary); More information about formatting http: //msdn. microsoft. com/library/en-us/csref/html/vclrf. Formatting. Numeric. Results. Table. asp 1 nd Semester 200 7 37
Outline C# Overview ¡ Variable and Constant ¡ Expression ¡ Statement ¡ Modify-And-Assign ¡ Math Class ¡ 1 nd Semester 200 7 38
Increment & Decrement ¡ ¡ Pre in/decrement: l Use the value which has already been indecrement. Post in-decrement: l Use the value before in-decrement 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 increment int a = 5; int b = a- - ; // a = 4, b = 5 1 nd Semester 200 7 39
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 /= 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) Try this ! int y=8; int a=2; Console. Write. Line(y -= 3+a); 1 nd Semester 200 7 40
Operator Priority Highest Priority (Do First) () ++(x) , --(x), +(x), (x) */% +- Lowest Priority (Do Last) 1 nd Semester 200 7 = , += , -=, *=, /=, %= (x)++ , (x)-- 41
The Math Class 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 Math. Log(10) 2. 302585 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 1 nd Semester 200 7 42
Summary C# Overview ¡ Variable and Constant ¡ Expression ¡ Statement ¡ Modify-And-Assign ¡ Math Class ¡ 1 nd Semester 200 7 43
- Slides: 43