Presentation is loading. Please wait.

Presentation is loading. Please wait.

Procedural Programming in C# Chapters 2 - 5. 2 Objectives You will be able to: Describe the most important data types available in C#. Read numeric values.

Similar presentations


Presentation on theme: "Procedural Programming in C# Chapters 2 - 5. 2 Objectives You will be able to: Describe the most important data types available in C#. Read numeric values."— Presentation transcript:

1 Procedural Programming in C# Chapters 2 - 5

2 2 Objectives You will be able to: Describe the most important data types available in C#. Read numeric values from the keyboard. Describe the major differences between C# and C for procedural programming.

3 3 Changes to Standard C Familiar Data Types int 32 bit integer Like int in C Adequate for most purposes long 64 bit integer Unlike long in C float 32 bit floating point Like float in C Dont use unless space is critical. double 64 bit floating point Like double in C Right for most floating point needs

4 4 New and Changed Data Types char uses Unicode representation. 16 bits Normally transparent, except for size of variables. char and byte are not equivalent. string is a type Can be assigned, compared. decimal 28 significant digits Integer with decimal scaling factor Works as expected for dollars and cents bool is a type values are true and false See page 33 for examples.

5 5 How to Input Numeric Values Every numeric type has a Parse method. Converts text string values to that type Replaces sscanf() in C Example: String Input_Line; double length;... Console.Write ("Enter length: "); Input_Line = Console.ReadLine (); length = double.Parse(Input_Line);

6 6 Changes to Standard C The Definite Assignment Rule (page 34) You must assign a value to a variable before using it. It is impossible to use an uninitialized variable. Enforced by the compiler. It must be able to determine that the variable is initilized on every possible exectution path to a statement that uses the variable.

7 7 Changes to Standard C/C++ Functions defined as part of a class are called methods. In C# this is the only way functions can be defined. So method and function are essentially synonymous. A method can return a value, or can be declared as void just like functions in C. If a method is not declared as void, it must include a return statement on every execution path.

8 8 if statements require a boolean expression or a variable of type bool. class Program { static void Main(string[] args) { bool allOK; allOK = true; if (allOK) { System.Console.WriteLine("All OK!"); } } } Changes to Standard C

9 9 This works as expected: class Program { static void Main(string[] args) { int a = 1; int b = 2; if (a == b) { System.Console.WriteLine("Something is wrong!"); } else { System.Console.WriteLine("All OK!"); }

10 10 Changes to Standard C This gets a compile error: class Program { static void Main(string[] args) { int a = 1; int b = 2; if (a = b) { System.Console.WriteLine("Something is wrong!"); } else { System.Console.WriteLine("All OK!"); }

11 11 Changes to Standard C This gets a compile error: class Program { static void Main(string[] args) { int a = 0; if (a) { System.Console.WriteLine("Something is wrong!"); } else { System.Console.WriteLine("All OK!"); } You cant use an integer in an if statement in C#

12 12 Changes to Standard C In switch statements, fallthrough (after a statement) is prohibited. Every case must have a break. (Or return or throw) This works as expected: static void Main(string[ ] args) { string color = ""; suit trumps = suit.clubs; switch (trumps) { case suit.hearts: case suit.diamonds: color = "Red"; break; case suit.clubs: case suit.spades: color = "Black"; break; } System.Console.WriteLine(color); } This is OK. (Not fallthrough)

13 13 Changes to Standard C This gets a compile error: static void Main(string[ ] args) { string color = ""; suit trumps = suit.clubs; switch (trumps) { case suit.hearts: case suit.diamonds: color = "Red"; break; case suit.clubs: case suit.spades: color = "Black";// No break! } System.Console.WriteLine(color); } This is OK. (Not fallthrough)

14 14 Changes to Standard C This also gets a compile error: static void Main(string[ ] args) { string color = ""; suit trumps = suit.clubs; switch (trumps) { case suit.hearts: color = "Red"; // No break! case suit.diamonds: color = "Red"; break; case suit.clubs: case suit.spades: color = "Black"; break; } System.Console.WriteLine(color); }

15 15 Assignment Read Chapters 1 – 5. Compile and run several examples. Especially any examples in the book that do not look familiar to you. End of Presentation


Download ppt "Procedural Programming in C# Chapters 2 - 5. 2 Objectives You will be able to: Describe the most important data types available in C#. Read numeric values."

Similar presentations


Ads by Google