Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2.

Similar presentations


Presentation on theme: "Chapter 2."— Presentation transcript:

1 Chapter 2

2 Chapter 2 Namespaces Input and Output
List of some classes in System, Math etc Compile and execute sequence

3 Typing C# is a strongly “typed” language
Most formal languages are (C++, Cobol, Java) Cannot mix numbers and characters Must know how big number will get

4 Data Types Short (16 bit integer) Char (16 bit character)
Float (32 bit floating point number) Boolean

5 More on Data Types Numeric Character or String Boolean
Integer (short, long, byte) Floating point (for decimal fractions) Decimal (with M suffix for money) 96 bits Double (includes decimal places) Character or String Any character that has to appear Boolean Represents logical values (TRUE OR FALSE)

6 Numeric Integer Double (the default type) Decimal Floating Point
Short(16) , long (32 bits), or byte (8bits) Literals – when you use a value in the program Double (the default type) Up to 15 decimal digits Decimal Up to 28 digits Floating Point Used to describe numbers with decimals Use ‘double’ to ensure full representation

7 Many characters (a sentence)
One character within single quotes (‘x’) String Many characters (a sentence)

8 Boolean Bool data stores logical result (true or false)

9 Conversion Sometimes programmer needs special variable
Two types in C#: Implicit and Explicit Compiler protects user from unsafe choice Casting converts inline: float pi=(float) 3.14 Default is double for Fl Pt literals Casting overrides the double and allows cast

10 Program Style We listed some // comments earlier
We also should have 4 sections in every program // declare section (where you assign variables etc) // input section (accept user input) // computations (where you do calculations) // display results IN THAT ORDER

11 using System; namespace WTprog1 { // wt // program 1 // due date" 1/31/05 class Class1 static void Main(string[] args) // declare section // input section // computation section // display section }// end Main }//end class }// end namespace

12 Variables To assign a value to an integer: int age; age = 5;
int tells the compiler that an integer follows age is the case-sensitive variable name 5 is the value that will be stored in age If you had a statement to display the value, Console.WriteLine(“age is ” + age); Console.WriteLine(“Age is {0}”, age);

13 Variables Contd To assign a character value to a variable:
char name = ‘d’; Notice the single quotes? In C# each character occupies 16 bits, so that even Japanese etc can fit You use string to input a whole name name=“Smith”

14 Printing Your program: string name = “jones”; string major = “IS”;
string yr = “02”; Console.WriteLine(“My name is ” + name); Console.WriteLine(“Major: ” + mj+ “ year ”+yr) Console.WriteLine(“Major: {0}, Year: {1}”, major, yr;


Download ppt "Chapter 2."

Similar presentations


Ads by Google