240 likes | 352 Views
C# Programming: Basic Concept. Computer and Programming (204111). Reminders. M@xLearn account Lab policy No game No chat No copying. Outline. Program Structure Data Types Variables Expressions Output Statements. C# Program. Consider the following program
E N D
C# Programming: Basic Concept Computer and Programming (204111)
Reminders • M@xLearn account • Lab policy • No game • No chat • No copying
Outline • Program Structure • Data Types • Variables • Expressions • Output Statements
C# Program • Consider the following program • What C#'s programming rules can you derive? namespace HelloW { class HelloWClass { static void Main () { System.Console.WriteLine("Hello World!"); System.Console.ReadLine(); } } }
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
Program Structure • The starting point is at the point that says: • 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 ... }
Class Namespace Program Structure • Think of a class as a container of methods • Think of a namespace as a container of classes • In C# • A program can contain several namespaces • A namespace can contain several classes • A class can contain several methods method1 method2
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(); } } }
Programming Example • Suppose we want to calculate the size of an area • What’s the input? • Data which its information is related to the size of that area • Width and Height (ความกว้าง และ ความยาว) • What’s the output? • The size of the area • Process: Multiply the width and the height
Variables • A variable is used to store data • Declaration: • Assigning value • Example: <type> <name>; <name> = <expression>; int width, height; int area; width = 10; height = 20;
Variables • An initial value can be given to a variable as it is declared • Example: • What you need to know • Naming Rules and Data Types int width = 10, height = 20; int area; area = width * height;
Naming Rules • An identifier's name must follow these rules: • Starting with a letter (A-Z, a-z) • Consisting of letters, digits, and underscore (_) • Up to 63 characters long • Must not be a reserved word (see next page) • Some valid names: • hEllO, E3_32ab, X_x_X022 • Some invalid names: • 32ABC, A.2, C#Program, while
Reserved Words • These words must not be used as names • Why? Because they are used in programming (reserved)
Data Types • Understanding of Base-2 Numerals (Binary) • Base-10 Numbers (0, 1, 2,…,9) • Base-2 Numbers (only 0, 1) • Consider the following 4-bit binary numbers 500 = (5 x 102)+(0 x 101)+(0 x 100) 5134 = (5 x 103)+(1 x 102)+(3 x 101)+(4 x 100) 0100 = (1 x 22)+(0 x 21)+(0 x 20) = 4 1110 = (1 x 23)+(1 x 22)+(1 x 21)+(0 x 20) = 8 + 4 + 2 = 14
Why Different Data Types/Sizes? • Easier for compiler • Use only necessary memory • Minimal resources used • Speed • If too less memory is used • Overflow! 11111001 + 00000001 = 11111010 11111111 + 00000001 = 00000000 OVERFLOW!!!
Arithmetic Expression • Operators • + - * / • % (remainder after division) • Example • 11 + 5 16 • 11 / 2 5 • 11.0 / 2 5.5 • 11 % 2 1 • 5.0 % 2.2 0.6
Precedence Rules • ( ) parentheses • *, / , % • +, – • If equal precedence, left to right double Width,Heigh; Width = 10*5+(16 * 12)/5; Heigh = (16+5)+20%2;
Explanation of Precedence Rules • double Width,Height; Width = 10*5+(16*12)/5 = 10*5+192/5 = 50+192/5 = 50+38.4 = 88.4 Heigh = (16+5)+20%2 = 21+20%2 = 21
What if we change data type • int Width,Height; Width = 10*5+(16*12)/5 = 10*5+192/5 = 50+192/5 = 50+38 = 88 Heigh = (16+5)+20%2 = 21+20%2 = 21
"using" Keyword • Write using statement at the beginning of a program • indicates that we are willing to refer to classes inside that namespace class Hello { static void Main () { System.Console.WriteLine("Hello World!"); System.Console.ReadLine(); } } using System; class Hello { static void Main () { Console.WriteLine("Hello World!"); Console.ReadLine(); } }
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
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);