390 likes | 467 Views
C# Basic Concept. Thanachat Thanomkulabut. C # Language 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. . . . . name. Name. point9. 9point. . . . .
E N D
C# Basic Concept Thanachat Thanomkulabut
C# Language 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 name Name point9 9point _data class class_A class_”A”
Outline • C# Beginning • Variable and Constant • Expression • Statement • Modify-And-Assign • Math Class
C# Beginning C# Beginning • 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 static void Main () { ... starting point ...}
C# Beginning Main Method declaration • There are 4 ways to define Main function. • We use the simplest one. • static void Main() {...} • static void Main(string[] args) {...} • static int Main() {...} • static int Main(string[] args) {...}
C# Beginning Inside method Main • 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); }
Outline • C# Beginning • Variable and Constant • Expression • Statement • Modify-And-Assign • Math Class
Variable & Constant What is Variable? A variable is used to store “data.” “It must be declared before used”
Variable & Constant Data Types
Variable & Constant C# Variable Declaration • Syntax: <datatype> <name>; • Example: • We can also assign its initial value. Example: int radius; double area; boolisokay; int k = 200; bool done = false;
C# Variable Declaration • Syntax: • <datatype> <name_1>, <name_2>,..., <name_n>; • Example: • We can also assign its initial value. Example: int width, length, height; double mean, sd, max, min; bool isokay, isright, check; int width=5, length=2, height=4;
Test I - Variable Declaration • Declare variable3,4,5 • Name3 : u • Name4 : tName5 : a • Type : double • Initial value3 : 5.0 • Initial value4 : nothing • Initial value5 : 9.8 • Declare variable1 • Name : num_Student • Type : interger • Initial value : nothing • Declare variable2 • Name : gender • Type : character • Initial value : m
Variable & Constant C# Constant Declaration • Syntax: const <datatype> <name> = <value>; • Example: const int radius = 15; const double area=1.5; const bool isokay=true; const string movie=”StarWarIII”; const char mckazine=‘m’;
Variable & Constant C# Constant Declaration • Syntax: const <datatype> <name_1> = <value_1>, <name_2> = <value_2>, ... , <name_n> = <value_n>; • Example: const int radius = 15, height = 5; const double area=1.5, wide=3.2, lenght = 4.1;
Test II - Constant Declaration • Declare Constant • Name : e • Type : double • Initial value : 2.71828
Outline • C# Beginning • Variable and Constant • Expression • Statement • Modify-And-Assign • Math Class
Expression C# Expression
Expression Arithmetic Expression • Operators • + - * / • % (remainder after division) • Example • 11 + 5 • 39 / 5 • 39.0/5 • 39 % 5 • 5.0 % 2.2 16 7 7.8 4 0.6
Expression Piority of Arithmetic Operators Answer int a, b; a = 2-10*3/5+(6%4); b = 5*(15%4+(2-3))/9; a = -2 b = 1
Expression Calculation Priority 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
Expression Boolean Expression • Operators • Comparison • Equal == • Not equal != • Less < • Greater > • Less than or equal to <= • Greater than or equal to >= • Boolean • And && • Or || • Not! 0 and 0 = 0 0 and 1 = 0 1 and 0 = 0 1 and 1 = 1 0 or 0 = 0 0 or 1 = 1 1 or 0 = 1 1 or 1 = 1 not 0 = 1 not 1 = 0
Expression Example: Boolean Expression • 10 > 50 • ’A’ < ’B’ false true
Outline • C# Beginning • Variable and Constant • Expression • Statement • Modify-And-Assign • Math Class
Statement Statements • A statement is a unit of command to instruct your program • A method consists of one or more statements Statement#1 class Hello { static void Main () { Console.WriteLine("Hello World!"); Console.ReadLine(); } } Statement#2
Statement C# Statement Types
Statement Assignment Statement • Assigning value to variable • Use the equal sign (=) when making assignments. • Syntax: <variable> = <expression>; int Width,High; Width=10; High=20+Width;
Statement Input Statement • Console.ReadLine() Return string • Use to get the input from user • Convert string to other data type • int.Parse() Convert string to integer • double.Parse() Convert string to double Example string st; st = Console.ReadLine();
Statement Example: Input Statement Ex1: string myname; myname = Console.ReadLine(); Ex2: int Width; string temp1; temp1 = Console.ReadLine(); Width = int.Parse(temp1);
Statement Output Statements • Use the method Write or WriteLine in the Console class (which is in System namespace) • Basic usage: • Advanced usage: • Even more advanced usage: Console.WriteLine("Hello"); Console.WriteLine(area); Console.WriteLine(”Size {0}x{1}”, width, height); double salary=12000; Console.WriteLine("My salary is {0:f2}.", salary);
Outline • C# Beginning • Variable and Constant • Expression • Statement • Modify-And-Assign • Math Class
Modify-And-Assign Increment & Decrement • Pre in/de-crement: • Use the value which has already been in/de-crement. • Post in/de-crement: • Use the value before in/de-crement
Increment & Decrement a b Monitor Ex1: int a=5; int b=a++; Console.WriteLine(“a={0}, b={1}”,a,b); 5 6 5 a=6, b=5 a b Monitor Ex2: int a=5; int b=++a; Console.WriteLine(“a={0}, b={1}”,a,b); 5 6 6 a=6, b=6
Modify-And-Assign Modify-And-Assign Operations 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.WriteLine(y -= 3+a);
Outline • C# Beginning • Variable and Constant • Expression • Statement • Modify-And-Assign • Math Class
Math Class The Math Class
Test III • Write the program which • Input : Your name • Output : Your name is <your name>.
Test IV • Write the program which • Input : 3 number • Output : average of 3 input number
Test VI • Write the program which • Input : lenght of radius of circle • Output : Area of circle