C Programming Basic Concept Computer and Programming 204111

C# Programming: Basic Concept Computer and Programming (204111)

Reminders M@x. Learn account n Lab policy n No game n No chat n No copying n

Outline Program Structure n Data Types n Variables n Expressions n Output Statements n

C# Program n Consider the following program 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?

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 n Comments will not be translated

Program Structure n The starting point is at the point that says: 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

Program Structure n n n Think of a class as a container of methods Think of a namespace as a container of classes In C# n n n A program can contain several namespaces A namespace can contain several classes A class can contain several methods method 1 method 2 Class Namespace

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 n n Program with only one class and at most one namespace For now until sometime before midterm n Program with one method (i. e. , Main)

Programming Example Suppose we want to calculate the size of an area n What’s the input? n Data which its information is related to the size of that area n Width and Height (������� ( n n What’s the output? n n The size of the area Process: Multiply the width and the height

Variables A variable is used to store data n Declaration: n <type> <name>; n Assigning value <name> = <expression>; n Example: int width, height; int area; width = 10; height = 20;

Variables An initial value can be given to a variable as it is declared n Example: n int width = 10, height = 20; int area; area = width * height; n What you need to know n Naming Rules and Data Types

Naming Rules n An identifier's name must follow these rules: Starting with a letter (A-Z, a-z) n Consisting of letters, digits, and underscore (_) n Up to 63 characters long n Must not be a reserved word (see next page) n n Some valid names: n n h. Ell. O, E 3_32 ab, X_x_X 022 Some invalid names: n 32 ABC, A. 2, C#Program, while

Reserved Words n These words must not be used as names n Why? Because they are used in programming (reserved)

Data Types Understanding of Base-2 Numerals (Binary) n Base-10 Numbers (0, 1, 2, …, 9( n 500 5134 n = (5 x 102)+(0 x 101)+(0 x 100) = (5 x 103)+(1 x 102)+(3 x 101)+(4 x 100) Base-2 Numbers (only 0, 1( n Consider the following 4 -bit binary numbers 0100 1110 = = = (1 x 22)+(0 x 21)+(0 x 20) 4 (1 x 23)+(1 x 22)+(1 x 21)+(0 x 20) 8 + 4 + 2 14

Data Types Type Size Description Range bool 1 byte Store truth value true /false char 1 byte Store one character code 0. . . 255 sbyte 1 byte Store integer -128. . . 127 byte 1 byte Store non-negative integer 0. . . 255 short 2 bytes Store integer -32, 768. . . 32, 767 ushort 2 bytes Store non-negative integer 0. . . 65535 int 4 bytes Store integer -2. 1 x 109. . . 2. 1 x 109 uint 4 bytes Store non-negative integer 0. . . 4. 3 x 109 long 8 bytes Store integer -9. 2 x 1018. . . 9. 2 x 1018 ulong 8 bytes Store non-negative integer 0. . . 1. 8 x 1019 float 4 bytes Store real number ± 1. 5 x 10 -45. . . ± 3. 4 x 1038 double 8 bytes Store double-precision real number ± 5. 0 x 10 -324. . . ± 1. 7 x 10308 decimal 16 bytes Store high-precision real number ± 1. 0 x 10 --28. . . ± 7. 9 x 1028 string N/A Store sequence of characters N/A

Why Different Data Types/Sizes? Easier for compiler n Use only necessary memory n Minimal resources used n Speed n n If too less memory is used n Overflow! 11111001 + 00000001 = 11111010 1111 + 00000001 = 0000 OVERFLOW!!!

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

Precedence Rules 1. 2. 3. 4. ( ) 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 n double Width, Height; Width = = = 10*5+(16*12)/5 10*5+192/5 50+38. 4 88. 4 Heigh = = = (16+5)+20%2 21

What if we change data type n int Width, Height; Width = = = 10*5+(16*12)/5 10*5+192/5 50+38 88 Heigh = = = (16+5)+20%2 21

"using" Keyword n Write using statement at the beginning of a program n indicates that we are willing to refer to classes inside that class Hello { namespace static void Main () { System. Console. Write. Line("Hello World!"); System. Console. Read. Line(); } } using System; class Hello { static void Main () { Console. Write. Line("Hello World!"); Console. Read. Line(); } }

Statements n A statement is a unit of command to instruct 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

n n Output Statements 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

Questions?
- Slides: 24