Download presentation
Presentation is loading. Please wait.
Published byWendy Wilcox Modified over 8 years ago
1
1 st Semester 2005 1 Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang aphirak.j@ku.ac.th http://www.cpe.ku.ac.th/~aphirak Computer Engineering Department Kasetsart University, Bangkok THAILAND
2
1 st Semester 2005 2 Outline C# Overview Variable and Constant Expression Statement
3
1 st Semester 2005 3 Simple C# program structure Your Class Name Your declaration part Your statements Your Namespace Name C# Overview
4
1 st Semester 2005 4 Naming Guidelines Letters, digits, underscores(_) First character letter Can be long (63 char) Reserved words (keywords) are not allowed *** Case Sensitive *** Example KU65 ≠ kU65 ≠ku65 KU65 ≠ kU65 ≠ ku65 C# Overview
5
1 st Semester 2005 5 Built-in Namespace: System Console Console class can be used for Displaying Displaying information on the screenSystem.Console.Write(”HelloWorld!”);System.Console.WriteLine(”HelloWorld!”); Retrieving Retrieving information from the user String ch; ch = System.Console.Readline(); C# Overview
6
1 st Semester 2005 6 How to Comment Code in C# 1 2
7
1 st Semester 2005 7 Summary C# Program Overview Namespace Class Main() C# Overview
8
1 st Semester 2005 8 Outline C# Overview Variable and Constant Expression Statement
9
1 st Semester 2005 9 Basic uses Declaration Part Variable Constant Variable & Constant
10
1 st Semester 2005 10 What is Variable? Variables are used to store “data.” “They must be declared before used” Variable & Constant
11
1 st Semester 2005 11 C# Declaration Location Declaration Part Here Variable & Constant
12
1 st Semester 2005 12 C# Variable Declaration Syntax: ; Example: We can also assign its initial value. E.g., int radius; double area; int a,b,c; bool isokay; int k = 200; bool done = false; Variable & Constant
13
1 st Semester 2005 13 C# Basic Data Type int Integer ( -2147483648 to 2147483647) double Floating-point ( ) bool Boolean values, true and false char a Unicode character (’A’ ’B’) string string of Unicode characters (”StarsIII”) Variable & Constant
14
1 st Semester 2005 14 Example: Variable Declaration Variable & Constant
15
1 st Semester 2005 15 Basic uses Declaration Part Variable Constant Variable & Constant
16
1 st Semester 2005 16 C# Constant Declaration Syntax: const = ; Example: const int radius = 15; const double area=1.5; const bool isokay=true; const string movie=”StarWarIII”; Variable & Constant
17
1 st Semester 2005 17 Outline C# Overview Variable and Constant Expression Statement
18
1 st Semester 2005 18 Arithmetic Expression in C# Operators + - * / % (remainder after division) Example 11 + 5 16 11 / 2 5.5 11 % 2 1 5.0 % 2.2 0.6Expression
19
1 st Semester 2005 19 Precedence rules for Arithmetic Operators 1.( ) parentheses 2.*, /, % 3.+ – 4.If equal precedence, left to right Example int Width,High; Width=10*5+(16 * 12)/5; High= (16+5)+20%2;
20
1 st Semester 2005 20 Outline C# Overview Variable and Constant Expression Statement
21
1 st Semester 2005 21 C# Statement LocationStatementsHereStatement
22
1 st Semester 2005 22 C# Statement Types Assignment Statement Input Statement Output StatementStatement
23
1 st Semester 2005 23 Assignment Statement "put" To "put" a value in the memory space allocated to a variable equal sign (=) Use the equal sign (=) when making assignments. Syntax: = ; = ; int Width,High; Width=10;High=5; int Width = 10; int High = 5; Statement
24
1 st Semester 2005 24 Example: Assignment StatementStatement
25
1 st Semester 2005 25 C# Statement Types Assignment Statement Input Statement Output StatementStatement
26
1 st Semester 2005 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 yourname; yourname = Console.ReadLine(); Statement
27
1 st Semester 2005 27 Example: Input Statement Ex1: string myname; myname = Console.ReadLine(); Ex2: int Width,High; string temp1; temp1 = Console.ReadLine(); Width = int.Parse(temp1); temp1 = Console.ReadLine(); Width = int.Parse(temp1); Statement
28
1 st Semester 2005 28 C# Statement Types Assignment Statement Input Statement Output StatementStatement
29
1 st Semester 2005 29 Output Statement Console.WriteLine(VariableName) Display variable value in some position of string Output Formatting Example1 Console.WriteLine(”Hello {0}, {1}”, ps0, ps1); Example2 double salary=12000; Console.WriteLine("My salary is {0:f}.", salary); More information about formatting *http://msdn.microsoft.com/library/en-us/csref/html/vclrfFormattingNumericResultsTable.asp Statement
30
1 st Semester 2005 30 Summary C# Overview Variable and Constant Expression Statement
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.