170 likes | 311 Views
Working with Variables, Operators and Expressions. 1. 2. 3. Statements -Comments. Identifiers. Variables. 4. 5. Primitive Data Types. Arithmetic Operators. 6. 7. Implicitly Typed Local Variables. Incrementing & Decrementing Variables.
E N D
Working with Variables, Operators and Expressions
1 2 3 Statements -Comments Identifiers Variables 4 5 Primitive Data Types Arithmetic Operators 6 7 Implicitly Typed Local Variables Incrementing & Decrementing Variables
-A Statement is a command that performs an action, you must add “;” the end of command -Ex: Console.Write(“Hello C# ”); 1 Statements -Comments -Comments use to explain for your Coding How to write comments??? See next slide…
//Comment 1 /* *Comment 2 */ /*Comment 3*/ /// <summary> ///Write Comment 4 /// </summary> /// <paramname=“a"></param> /// <paramname=“b"></param> private void func1(inta,float b)
Identifiers are names use to identify the elements in program, ex: namespace, class, method… Syntax Rules: -Only use letter, digit, underscore -An identifier must start with a letter or an underscore -C# is a case –sensitive : nTestand ntestis different identifier 2 Identifiers
Identifying Keywords abstract do in protected true as double int public try base else interface readonlytypeof bool enum internal ref uint break event is return ulong byte explicit lock sbyte unchecked case extern long sealed unsafe catch false namespace short ushort char finally new sizeof using checked fixed null stackalloc virtual class float object static void const for operator string volatile continue foreach out struct while decimal goto override switch default if params this delegate implicit private throw dynamic join set from let value get orderbyvar group partial where into select yield
- A variable is storage location that holds a value. It as a box in the Computer’s memory holding temporary information. -To easy coding: +Don’t start an identifier with underscore +Don’t different only by case ( nTestand ntest) +Start name with lower case +…. 3 Variables
How to declare variables? Data_typeNameVariable; intnNumberOfElement; string strFullName; float fValue=0,fResult;
4 Primitive Data Types
You should initialize value for variable intnSize=0; String strName=“”;
+ - * 5 / % Arithmetic Operators () intnPlusTwoNumber=113 + 114 The + is the operator 113 and 114 are the operands
Not all operators are applicable to all data types Example: You will get compiler error with the statements: Console.WriteLine(“Tý” - “Tèo”);
How to convert from String to Number Format ? intnValue=System.Int32.Parse("113"); doubledValue = System.Double.Parse("3.5"); DatatypevName=System.DataTypeObject.Parse(“string”);
++X Prefix increment X++ Postfix increment 6 Incrementing & Decrementing Variables X-- Postfix decrement --X Prefix decrement
7 varnumber1=1; varstrAny=“tèo”; Implicitly Typed Local Variables varsAny; Error: Implicitly-typed local variables must be initialized