Download presentation
Presentation is loading. Please wait.
Published byLuke Byrd Modified over 8 years ago
1
C# Basic Concept Thanachat Thanomkulabut
2
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
3
Outline 3 C# Beginning Variable and Constant Expression Statement Modify-And-Assign Math Class
4
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
5
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
6 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); } C# Beginning
7
Outline 7 C# Beginning Variable and Constant Expression Statement Modify-And-Assign Math Class
8
What is Variable? A variable is used to store “data.” “It must be declared before used” Variable & Constant 8
9
Data Types TypeSizeDescriptionRange bool1 byteStore truth valuetrue / false char1 byteStore one charactercharacter code 0 – 255 byte1 byteStore positive integer0 – 255 short2 byteStore integer-32,768 -- 32,767 int4 byteStore integer-2.1 x 10 9 -- 2.1 x 10 9 long8 byteStore integer-9.2 x 10 18 -- 9.2 x 10 18 double16 byte Store real number± 5.0x10 -324 -- ± 1.7x10 308 stringN/AStore sequence of characters N/A Variable & Constant 9
10
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
11
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;
12
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
13
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
14
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
15
Test II - Constant Declaration 15 Declare Constant Name : e Type : double Initial value : 2.71828
16
Outline 16 C# Beginning Variable and Constant Expression Statement Modify-And-Assign Math Class
17
C# Expression 17 Expression Arithmetic Expression Boolean Expression Expression
18
Arithmetic Expression Operators + - * / % (remainder after division) Example 11 + 5 39 / 5 39.0/5 39 % 5 5.0 % 2.2 Expression 16 7 4 0.6 18 7.8
19
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
20
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
21
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
22
Example: Boolean Expression 10 > 50 ’A’ < ’B’ false true Expression 22
23
Outline 23 C# Beginning Variable and Constant Expression Statement Modify-And-Assign Math Class
24
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
25
C# Statement Types 25 C# Statement Types Assignment Statement Input Statement Output Statement Statement
26
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
27
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
28
Example: Input Statement Ex1: string myname; myname = Console.ReadLine(); Ex2: int Width; string temp1; temp1 = Console.ReadLine(); Width = int.Parse(temp1); Statement 28
29
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
30
Outline 30 C# Beginning Variable and Constant Expression Statement Modify-And-Assign Math Class
31
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
32
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
33
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
34
Outline 34 C# Beginning Variable and Constant Expression Statement Modify-And-Assign Math Class
35
The Math Class Method/ Constant Value returnedExample CallResult PI Value of Math.PI3.1415927 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)2.302585 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
36
Test III 36 Write the program which Input : Your name Output : Your name is.
37
Test IV 37 Write the program which Input : 3 number Output : average of 3 input number
38
Test VI 38 Write the program which Input : lenght of radius of circle Output : Area of circle
39
Any question?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.