1 / 43

Module 2 C# Basic Concept

Module 2 C# Basic Concept. Thanawin Rakthanmanon Email: fengtwr@ku.ac.th Computer Engineering Department Kasetsart University, Bangkok THAILAND. Outline. C# Overview Variable and Constant Expression Statement Modify-And-Assign Math Class. C# Program.

Télécharger la présentation

Module 2 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. Module2C# Basic Concept Thanawin Rakthanmanon Email: fengtwr@ku.ac.th Computer Engineering Department Kasetsart University, Bangkok THAILAND

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

  3. C# Program • Consider all the programs you wrote in Lab#0 • What C#'s programming rules can you derive? namespace HelloW { class HelloWClass { static void Main () { System.Console.WriteLine("Hello World!"); System.Console.ReadLine(); } } }

  4. 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 • Comments will not be translated

  5. Program Structure • 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 ... }

  6. Class Class namespace Program Structure • In C# • A program can contain several namespaces • A namespace can contain several classes • A class can contain several methods • In other words • Think of a namespace as a container of classes • Think of a class as a container of methods method1 method2

  7. Class namespace Class Class method1 method2 : : Program Structure (example) namespace namespace HelloW { class HelloWClass { static void Main () { System.Console.WriteLine(“Hello"); } } } method1 method2 method3

  8. Program Structure • For this 204111 course • Program with only one class and at most one namespace • For now until sometime before midterm • Program with one method (i.e., Main) namespace HelloW { class HelloWClass { static void Main () { System.Console.WriteLine("Hello World!"); System.Console.ReadLine(); } } }

  9. 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 KU66 ≠ kU66 ≠ku66

  10. C# Overview C# Reserved Words

  11. Short break – 3 Minutes

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

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

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

  15. Data Types

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

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

  18. Expression C# Expression • Arithmetic Expression • Boolean Expression

  19. Expression Arithmetic Expression • Operators • + - * / • % (remainder after division) • Example • 11 + 5  16 • 11 / 2  5 • 11 % 2  1 • 5.0 % 2.2  0.6

  20. Precedence rules for Arithmetic Operators • ( ) parentheses • *, / , % • + – • If equal precedence, left to right int Width,High; Width=10*5+(16*12)/5; High= (16+5)+20%2;

  21. Calculation Priority Calculate from left to right! public static void Main(){ int a,b,c,d; a=1; b=2; c=3; d = c/b*a; Console.WriteLine("d={0}",d); d = a/b; Console.WriteLine("d={0}",d); } Console.WriteLine(3/4*8); = Console.WriteLine((3/4)*8); = 0 (= ) ? 3 ??? x 8 4

  22. Expression Expressionin C# • Arithmetic Expression • Boolean Expression

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

  24. Example: Boolean Expression • 50 > 10  true • ’A’ < ’B’  true • ’ANT’ < ’B’  true • ’5’ > ’1’  true • ’5’ > ’10’  true • ’5’ > ’100’  true

  25. 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 = ?

  26. Short break – 5 Minutes

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

  28. 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 () { System.Console.WriteLine("Hello World!"); System.Console.ReadLine(); } } Statement#2

  29. Statement C# Statement Types • Assignment Statement • Input Statement • Output Statement

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

  31. Statement C# Statement Types • Assignment Statement • Input Statement • Output Statement

  32. 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 = System.Console.ReadLine();

  33. Example: Input Statement string st; int i; double d; 1. st = Console.ReadLine(); i = int.Parse(st); 2. st = Console.ReadLine(); d = double.Parse(st); 3. i = int.Parse(Console.ReadLine()); 4. d = double.Parse(Console.ReadLine());

  34. Statement Example: Input Statement Ex1: string myname; myname = System.Console.ReadLine(); Ex2: int Width,High; string temp1; temp1 = System.Console.ReadLine(); Width = int.Parse(temp1); temp1 = System.Console.ReadLine(); High = int.Parse(temp1);

  35. Statement Example: Input Statement

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

  37. More information about formatting http://msdn.microsoft.com/library/en-us/csref/html/vclrfFormattingNumericResultsTable.asp 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);

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

  39. Increment & Decrement • Pre in/decrement: • Use the value which has already been in-decrement. • Post in-decrement: • Use the value before in-decrement

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

  41. Operator Priority Highest Priority (Do First) Lowest Priority (Do Last)

  42. The Math Class

  43. Summary Summary • C# Overview • Variable and Constant • Expression • Statement • Modify-And-Assign • Math Class

More Related