C# Basic Concept Thanachat Thanomkulabut
Naming Rules Letters, digits and underscores(_) First character letter or _ Up to 63 characters long Must not be a reserved word Case Sensitive C# Language Overview 2 Example nameName _data 9point class class_A class_”A” point9
Outline 3 C# Beginning Variable and Constant Expression Statement Modify-And-Assign Math Class
C# Beginning 4 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... } static void Main () {... starting point... } C# Beginning
Main Method declaration 5 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
6 Inside method Main Variable declarations Statements static void Main(string[] args) { const double pi = ; int radius; double area; radius = int.Parse(Console.ReadLine()); area = pi*radius*radius; Console.WriteLine(area); } C# Beginning
Outline 7 C# Beginning Variable and Constant Expression Statement Modify-And-Assign Math Class
What is Variable? A variable is used to store “data.” “It must be declared before used” Variable & Constant 8
Data Types TypeSizeDescriptionRange bool1 byteStore truth valuetrue / false char1 byteStore one charactercharacter code 0 – 255 byte1 byteStore positive integer0 – 255 short2 byteStore integer-32, ,767 int4 byteStore integer-2.1 x x 10 9 long8 byteStore integer-9.2 x x double16 byte Store real number± 5.0x ± 1.7x stringN/AStore sequence of characters N/A Variable & Constant 9
C# Variable Declaration Syntax: ; Example: We can also assign its initial value. Example: int radius; double area; bool isokay; int k = 200; bool done = false; Variable & Constant 10
C# Variable Declaration 11 Syntax:,,..., ; 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 12 Declare variable1 Name : num_Student Type : interger Initial value : nothing Declare variable2 Name : gender Type : character Initial value : m Declare variable3,4,5 Name3 : u Name4 : t Name5 : a Type : double Initial value3 : 5.0 Initial value4 : nothing Initial value5 : 9.8
C# Constant Declaration Syntax: const = ; Example: const int radius = 15; const double area=1.5; const bool isokay=true; const string movie=”StarWarIII”; const char mckazine=‘m’; Variable & Constant 13
C# Constant Declaration Syntax: const =, =,..., = ; Example: const int radius = 15, height = 5; const double area=1.5, wide=3.2, lenght = 4.1; Variable & Constant 14
Test II - Constant Declaration 15 Declare Constant Name : e Type : double Initial value :
Outline 16 C# Beginning Variable and Constant Expression Statement Modify-And-Assign Math Class
C# Expression 17 Expression Arithmetic Expression Boolean Expression Expression
Arithmetic Expression Operators + - * / % (remainder after division) Example 39 / 5 39.0/5 39 % 5 5.0 % 2.2 Expression
Piority of Arithmetic Operators Priorityoperator 1Parentheses () 2*, /, % 3+, - 4If equal precedence, left to right int a, b; a = 2-10*3/5+(6%4); b = 5*(15%4+(2-3))/9; a = -2 b = 1 Answer 19 Expression
Calculation Priority 20 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; } d = 2 e = 0 f = 2.5 g = 2.5 Answer 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 21
Example: Boolean Expression 10 > 50 ’A’ < ’B’ false true Expression 22
Outline 23 C# Beginning Variable and Constant Expression Statement Modify-And-Assign Math Class
Statements A statement is a unit of command to instruct your program A method consists of one or more statements class Hello { static void Main () { Console.WriteLine("Hello World!"); Console.ReadLine(); } class Hello { static void Main () { Console.WriteLine("Hello World!"); Console.ReadLine(); } Statement#1 Statement#2 Statement 24
C# Statement Types 25 C# Statement Types Assignment Statement Input Statement Output Statement Statement
Assignment Statement Assigning value to variable equal sign (=) Use the equal sign (=) when making assignments. Syntax: = ; = ; int Width,High; Width=10;High=20+Width; Statement 26
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 27
Example: Input Statement Ex1: string myname; myname = Console.ReadLine(); Ex2: int Width; string temp1; temp1 = Console.ReadLine(); Width = int.Parse(temp1); Statement 28
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(”Size {0}x{1}”, width, height); double salary=12000; Console.WriteLine("My salary is {0:f2}.", salary); Console.WriteLine("Hello");Console.WriteLine(area); Statement 29
Outline 30 C# Beginning Variable and Constant Expression Statement Modify-And-Assign Math Class
Increment & Decrement OperatorMeaningexample ++xpre increment int a = 5; int b = ++a; // a, b = 6 x++post increment int a = 5; int b = a++; // a = 6, b = 5 --xpre decrement int a = 5; int b = --a; // a, b = 4 x--post decrement int a = 5; int b = a- - ; // a = 4, b = 5 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 Modify-And-Assign 31
Increment & Decrement 32 Ex1: int a=5; int b=a++; Console.WriteLine(“a={0}, b={1}”,a,b); ab 556 a=6, b=5 Monitor Ex2: int a=5; int b=++a; Console.WriteLine(“a={0}, b={1}”,a,b); ab 566 a=6, b=6 Monitor
Modify-And-Assign Operations StatementDescription var += expression Increment var by the value of expression var -= expression Decrement var by the value of expression var *= expression Multiply var by the value of expression, then store the result in var var /= expression Divide var by the value of expression, then store the result in var 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) int y=8; int a=2; Console.WriteLine(y -= 3+a); Try this ! Modify-And-Assign 33
Outline 34 C# Beginning Variable and Constant Expression Statement Modify-And-Assign Math Class
The Math Class Method/ Constant Value returnedExample CallResult PI Value of Math.PI Max(x,y)Larger of the twoMath.Max(1,2)2 Abs(x)Absolute value of xMath.Abs(-1.3)1.3 Sqrt(x)Square-root of xMath.Sqrt(4.0)2.0 Round(x)Nearest integer to xMath.Round(0.8)1 Pow(x,y)xyxy Math.Pow(3,2)9.0 Log(x)Natural log of xMath.Log(10) Ceiling(x)Smallest integer greater than or equal to x Math.Ceiling(4.1)5 Cos(x)Cosine of x radiansMath.Cos(Math.PI) Math Class 35
Test III 36 Write the program which Input : Your name Output : Your name is.
Test IV 37 Write the program which Input : 3 number Output : average of 3 input number
Test VI 38 Write the program which Input : lenght of radius of circle Output : Area of circle
Any question?