C Basic Concept Adopted from Computer Engineering Department

C# Basic Concept Adopted from Computer Engineering Department Kasetsart University 1

Outline C# Overview n Variable and Constant n Expression n Statement n Modify-And-Assign n Math Class n 2

C# Program n 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(); } } } n What C#'s programming rules can you derive? 3

C# Program C# syntax is case-sensitive n Every statement ends with a semicolon ; n White space means nothing n Code block is inside braces { } n Anything between /* */ or after // is considered a comment n ¨ Comments will not be translated 4

Program Structure n The starting point of the program is: static void Main () {. . . starting point. . . } This is known as the method Main n A method is put inside a class n A class may be put inside a namespace n 5

Program Structure n In C# ¨A program can contain several namespaces ¨ A namespace can contain several classes ¨ A class can contain several methods n In other words ¨ 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 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 7

Program Structure namespace Hello. W { class Hello. WClass { static void Main () { System. Console. Write. Line("Hello World!"); System. Console. Read. Line(); } } } n For this 204111 course ¨ Program with only one class and at most one namespace n For now until sometime before midterm ¨ Program with one method (i. e. , Main) 8

C# Overview Naming Rules Letters, digits and underscores(_) n First character letter or _ n Up to 63 characters long n Must not be a reserved word n * Case Sensitive Example KU 67 ≠ ku 67 9

C# Overview C# Reserved Words 10

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

Variable & Constant C# Variable Declaration n Syntax: <data type> <name>; int radius; double area; int a, b, c; bool isokay; n Example: n We can also assign its initial value. Example: int k = 200; bool done = false; 12

Data Types Type Size Description Range 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 13

Variable & Constant C# Constant Declaration n Syntax: const <data type> <name> = <value>; n Example: const const int radius = 15; double area=1. 5; bool isokay=true; string movie=”Star. War. III”; char pattara=‘p’; 14

Expression Arithmetic Expression n Operators ¨+ - * / ¨% (remainder after division) n Example ¨ 11 + 5 16 ¨ 11 / 2 5 ¨ 11 % 2 1 ¨ 5. 0 % 2. 2 0. 6 15

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; 16

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); } 17

Expression Boolean Expression n Operators ¨ Comparison n Equal = n Not equal != n Less < n Greater > n Less than or equal to <= n Greater than or equal to >= ¨ Boolean n And && n Or || n Not ! 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 18

Example: Boolean Expression 10 > 50 false n ’A’ < ’B’ true n 19

Quiz n ((1 and 0) or (1 and 1)) = ? What is I/O device ? n What is RAM and ROM ? n 1 byte = xxx bits n 1 Mbyte = ? n C# is xxx language. n 10000102 = ? n 20

Outline C# Overview n Variable and Constant n Expression n Statement n Modify-And-Assign n Math Class n 21

Statements n A statement is a unit of command to instruc your program Statement#1 Statement#2 n class Hello { static void Main () { System. Console. Write. Line("Hello World!"); System. Console. Read. Line(); } } A method consists of one or more statements 22

Statement Assignment Statement Assigning value to variable n Use the equal sign (=) when making assignments. n Syntax: n <variable> = <expression>; int Width, High; Width=10; High=5; 23

Statement Input Statement n Console. Read. Line() Return string ¨ Use to get the input from user Example string st; st = Console. Read. Line(); n Convert string to other data type ¨ int. Parse() Convert string to integer ¨ double. Parse() Convert string to double 24

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); 25

Output Statements n n 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); n Advanced usage: Console. Write. Line(”Size {0}x{1}”, width, height); n 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 26

Increment & Decrement n n Pre in/de-crement: ¨ Use the value which has already been in/decrement. Post in/de-crement: ¨ Use the value before in/de-crement 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 = 527

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); 28

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 29

Summary C# Overview n Variable and Constant n Expression n Statement n Modify-And-Assign n Math Class n 30
- Slides: 30