Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "C# Programming: Basic Concept Part 2 Computer and Programming (204111)"— Presentation transcript:

1 C# Programming: Basic Concept Part 2 Computer and Programming (204111)

2 Reminders M@xLearn account M@xLearn account Group arrangement Group arrangement Pair up with another person Pair up with another person

3 Review Some of important basic rules: Some of important basic rules: C# syntax is case-sensitive C# syntax is case-sensitive Every statement ends with a semicolon (;) Every statement ends with a semicolon (;) Program structure; namespaces, class and methods Program structure; namespaces, class and methods method1 method2 Namespace Class

4 Review (Cont’) Variables: declaration and assignment Variables: declaration and assignment Data Types: for example; int (integer), char (character), bool (Boolean) Data Types: for example; int (integer), char (character), bool (Boolean) Arithmetic expression: Arithmetic expression: +, -, *, /, % +, -, *, /, % Precedence rules: ( )  * / %  + -  left to right Precedence rules: ( )  * / %  + -  left to right int width, height; int area; width = 10; height = 20; area = width * height; int width, height; int area; width = 10; height = 20; area = width * height;

5 Lab Review Statements Statements Output Statement Output Statement System.Console.WriteLine() is used to write a string to the screen and then start a new line System.Console.WriteLine() is used to write a string to the screen and then start a new line System.Console.Write() is the same as WriteLine(), but it does not start a new line System.Console.Write() is the same as WriteLine(), but it does not start a new line int area=10; System.Console.WriteLine(“Hello”); System.Console.WriteLine(area); int area=10; System.Console.WriteLine(“Hello”); System.Console.WriteLine(area); System.Console.WriteLine(string);

6 Lab Review (Cont’) Consider the program to calculate the size of an area (20 x 10) Consider the program to calculate the size of an area (20 x 10) If different size, modify what? If different size, modify what? It’s not efficient to modify the program every time It’s not efficient to modify the program every time namespace Lect31 { class Lect31class { static void Main () { int width, height, area; width = 20; height = 10; System.Console.WriteLine(“Rectangle: H = {0}, W = {1}“, height, width); area = width*height; System.Console.WriteLine(“The rectangle area is {0}.”, area); } namespace Lect31 { class Lect31class { static void Main () { int width, height, area; width = 20; height = 10; System.Console.WriteLine(“Rectangle: H = {0}, W = {1}“, height, width); area = width*height; System.Console.WriteLine(“The rectangle area is {0}.”, area); }

7 Outline "using" keyword "using" keyword Input statements Input statements More on arithmetic operations More on arithmetic operations Type conversion Type conversion Calling methods Calling methods Math class Math class

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

9 Input Statement The Console class has a method ReadLine which The Console class has a method ReadLine which Reads keyboard input until Enter is pressed Reads keyboard input until Enter is pressed Returns the whole input as a string Returns the whole input as a string Example: Example: string yourname; yourname = System.Console.ReadLine(); string yourname; yourname = System.Console.ReadLine();

10 Complete Example: ReadLine using System; class HelloYou { static void Main() static void Main() { string name; string name; Console.Write("What is your name: "); Console.Write("What is your name: "); name = Console.ReadLine(); name = Console.ReadLine(); Console.WriteLine("Hello, {0}", name); Console.WriteLine("Hello, {0}", name); }} using System; class HelloYou { static void Main() static void Main() { string name; string name; Console.Write("What is your name: "); Console.Write("What is your name: "); name = Console.ReadLine(); name = Console.ReadLine(); Console.WriteLine("Hello, {0}", name); Console.WriteLine("Hello, {0}", name); }} This program will ask the user for his/her name and say Hello This program will ask the user for his/her name and say Hello

11 String to Number Conversion As we have seen, Console.ReadLine returns a string As we have seen, Console.ReadLine returns a string Unfortunately, strings cannot be processed as numerical values (at least directly) Unfortunately, strings cannot be processed as numerical values (at least directly) What if we need a number input from user? What if we need a number input from user? Each numeric type has a Parse method which can be used to convert a string to number of that type Each numeric type has a Parse method which can be used to convert a string to number of that type string s = "12"; int x = s+1; // WRONG!!! string s = "12"; int x = s+1; // WRONG!!! string s = Console.ReadLine(); int x = int.Parse(s); double y = double.Parse(s); string s = Console.ReadLine(); int x = int.Parse(s); double y = double.Parse(s);

12 Example: Calculating Area using System; class AreaCalculation { static void Main() static void Main() { int width, height, area; int width, height, area; string input; string input; Console.Write("Enter width: "); Console.Write("Enter width: "); input = Console.ReadLine(); input = Console.ReadLine(); width = int.Parse(input); width = int.Parse(input); Console.Write("Enter height: "); Console.Write("Enter height: "); input = Console.ReadLine(); input = Console.ReadLine(); height = int.Parse(input); height = int.Parse(input); area = width * height; area = width * height; Console.WriteLine("Area = {0}x{1} = {2}", width, height, area); Console.WriteLine("Area = {0}x{1} = {2}", width, height, area); }} using System; class AreaCalculation { static void Main() static void Main() { int width, height, area; int width, height, area; string input; string input; Console.Write("Enter width: "); Console.Write("Enter width: "); input = Console.ReadLine(); input = Console.ReadLine(); width = int.Parse(input); width = int.Parse(input); Console.Write("Enter height: "); Console.Write("Enter height: "); input = Console.ReadLine(); input = Console.ReadLine(); height = int.Parse(input); height = int.Parse(input); area = width * height; area = width * height; Console.WriteLine("Area = {0}x{1} = {2}", width, height, area); Console.WriteLine("Area = {0}x{1} = {2}", width, height, area); }} Obtaining "width" Obtaining "height" area = width x height height width

13 Numeric Type Conversion Conversion from a smaller size variable to a larger size can be done implicitly Conversion from a smaller size variable to a larger size can be done implicitly (i.e., no loss of information possible) (i.e., no loss of information possible) If loss of information is possible, explicit type casting is required If loss of information is possible, explicit type casting is required doublefloatlongintshortsbyte ulonguintushortbyte char LargerSmaller int i = 50; double d = i; // OK int i = 50; double d = i; // OK double d = 5000.78; long x = d; // NOT OK long x = (long) d; // OK double d = 5000.78; long x = d; // NOT OK long x = (long) d; // OK

14 Modify-And-Assign Operations Arithmetic operators can be combined with to produce a modify-and-assign operation Arithmetic operators can be combined with "=" to produce a modify-and-assign operation Examples Examples 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)

15 Incrementing Operators Operators ++ and – can be used to increment and decrement a variable's value by 1, respectively Operators ++ and – can be used to increment and decrement a variable's value by 1, respectively Example: Example:StatementDescription x++ Add 1 to the variable x x-- Subtract 1 to the variable x int n = 0; n++; // is equivalent to n = n+1, or n += 1 n++; // n is now 2 n--; // n is now 1 int n = 0; n++; // is equivalent to n = n+1, or n += 1 n++; // n is now 2 n--; // n is now 1

16 Calling Methods Some classes provide static methods that can be called with the class name and method name Some classes provide static methods that can be called with the class name and method name General Form – calling the method inside the class which is inside the namespace General Form – calling the method "methodName" inside the class "className" which is inside the namespace "namespaceName" If the using statement is already used with "namespaceName", then the namespace part can be omitted If the using statement is already used with "namespaceName", then the namespace part can be omitted Well-known examples are and Well-known examples are System.Console.WriteLine() and System.Console.ReadLine() using namespaceName; : className. methodName ( optional-arguments ) namespaceName.className. methodName ( optional-arguments )

17 The Math Class The Math class inside the System namespace provides a large collection of math methods and constants, for example, The Math class inside the System namespace provides a large collection of math methods and constants, for example, Method/ Constant Value returned Example Call Result 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) xyxyMath.Pow(3,2)9.0 Log(x) Natural log of xMath.Log(10)2.302585 Ceiling(x) Smallest integer greater than or equal to xMath.Ceiling(4.1)5 Cos(x) Cosine of x radiansMath.Cos(Math.PI)


Download ppt "C# Programming: Basic Concept Part 2 Computer and Programming (204111)"

Similar presentations


Ads by Google