1 / 15

Procedural Programming in C#

Procedural Programming in C#. Chapters 2 - 5. 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. Changes to Standard C.

breena
Download Presentation

Procedural Programming in C#

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 from the keyboard. • Describe the major differences between C# and C for procedural programming.

  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 Don’t use unless space is critical. double 64 bit floating point Like double in C Right for most floating point needs

  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. stringis a type Can be assigned, compared. decimal 28 significant digits Integer with decimal scaling factor Works as expected for dollars and cents boolis a type values are true and false See page 33 for examples.

  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. 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. 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. Changes to Standard C “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!"); } } }

  9. Changes to Standard C 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. 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. 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 can’t use an integer in an “if” statement in C#

  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. 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. 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. 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

More Related