1 / 39

C# Basic Concept

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. . . . .

Télécharger la présentation

C# Basic Concept

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C# Basic Concept Thanachat Thanomkulabut

  2. 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”

  3. Outline • C# Beginning • Variable and Constant • Expression • Statement • Modify-And-Assign • Math Class

  4. 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 ...}

  5. 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) {...}

  6. 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); }

  7. Outline • C# Beginning • Variable and Constant • Expression • Statement • Modify-And-Assign • Math Class

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

  9. Variable & Constant Data Types

  10. 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;

  11. 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;

  12. 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

  13. 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’;

  14. 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;

  15. Test II - Constant Declaration • Declare Constant • Name : e • Type : double • Initial value : 2.71828

  16. Outline • C# Beginning • Variable and Constant • Expression • Statement • Modify-And-Assign • Math Class

  17. Expression C# Expression

  18. 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

  19. 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

  20. 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

  21. 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

  22. Expression Example: Boolean Expression • 10 > 50  • ’A’ < ’B’  false true

  23. Outline • C# Beginning • Variable and Constant • Expression • Statement • Modify-And-Assign • Math Class

  24. 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

  25. Statement C# Statement Types

  26. 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;

  27. 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();

  28. Statement Example: Input Statement Ex1: string myname; myname = Console.ReadLine(); Ex2: int Width; string temp1; temp1 = Console.ReadLine(); Width = int.Parse(temp1);

  29. 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);

  30. Outline • C# Beginning • Variable and Constant • Expression • Statement • Modify-And-Assign • Math Class

  31. 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

  32. 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

  33. 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);

  34. Outline • C# Beginning • Variable and Constant • Expression • Statement • Modify-And-Assign • Math Class

  35. Math Class The Math Class

  36. Test III • Write the program which • Input : Your name • Output : Your name is <your name>.

  37. Test IV • Write the program which • Input : 3 number • Output : average of 3 input number

  38. Test VI • Write the program which • Input : lenght of radius of circle • Output : Area of circle

  39. Any question?

More Related