240 likes | 438 Views
Syntax and semantics of C#. 1. Review. 2. Syntax vs. semantics. 3. Syntax. 4. Semantics. 5. Lab. 1. Review of the program life-cycle. A. The Design Phase. Creation of an algorithm . B. The Implementation Phase. Creation of a program . C. The Maintenance Phase.
E N D
Syntax and semantics of C#. 1. Review. 2. Syntax vs. semantics. 3. Syntax. 4. Semantics. 5. Lab.
1. Review of the program life-cycle. • A. The Design Phase. • Creation of an algorithm. • B. The Implementation Phase. • Creation of a program. • C. The Maintenance Phase. • Use and modification of a program.
2. Syntax vs. Semantics. • Any language has 2 dimensions, a syntax and a semantics. • Syntax refers to…. • The form or “shape” of the language. • Semantics refers to… • The content or “meaning” of the language.
3. Syntax. • Syntax includes: (A) grammar, (B) spelling, (C) punctuation. • A. Grammar. • In English: “To go boldly,” not “To boldly go.” • In C#: any input statement uses an input method (such as Console.ReadLine()); • an assignment statement begins with a variable (or “lvalue”).
3. Syntax. • For example, • Answer = Num + 5; // correct • Num + 5 = Answer; // incorrect—WHY?
Syntax (cont.). • B. Spelling. • Most reserved words (except Main() and certain built-in constants) must be lower case and spelled correctly. • Variable names can be made up by the programmer, but must begin with either a letter or an underscore, followed by up to 127 letters, numbers or underscores.
Syntax (cont.). • B. Spelling. • Although various possibilities are legal, it is helpful to distinguish our own variable names from reserved words (typically lower case) and constants (typically upper case), by using mixed case.
Syntax (cont.). • C. Punctuation. • Before each item in a Console.Write statement, there must be a concatenation operator e.g. • Console.Write (Num1 + “ plus ” + Num2 • + “ = ” + Answer); • Main is a method (function), so it requires argument and scope brackets Main() { } • Variables in a list are separated by commas. • Statements must be terminated by ;
Syntax (cont.). • Why does syntax matter? • The compiler will only translate source code into object code if the source code has perfect syntax. • Hence, one cannot run a program with syntax errors.
4. Semantics. • Concerns content/meaning. • We can distinguish (a) the reference of terms e.g. What does “zebra” refer to? from (b) the meaning of whole statements, e.g. “There are zebras jumping on your pick-up truck.”
Semantics (cont.). • In a programming language, the terms e.g. variable names / function names refer to items in the computer’s memory, while statements are imperatives whose meaning is specified by what they make the computer do.
Semantics (cont.). • Reference of terms: • Terms may refer to built-in functions and operations (e.g. sqrt(), *) or to user-defined variables and functions. • A variable name like “Sum” or “Total” is really a symbolic address. The compiler translates it into a memory location where a number can be stored.
Semantics (cont.). • A good analogy is given by a mailbox. • See diagram. • 1. The variable name is like: • the name on the mailbox. • 2. The variable (memory location) is like: • the mailbox itself (re-usable). • 3. The value of the variable is like: • the mail in the mailbox.
Semantics (cont.). • A variable declaration causes memory to be reserved. This is crucial because we cannot access memory if we have not reserved it. • float Num1; • Num1 = 7; // OK • Num2 = Num1 + 5; • // The name ‘Num2’ does not exist in the //current context
Semantics (cont.). • Constants? E.g. for Pi. • const float PI = 3.14159; • const int WEEKS=52; • const char YES=‘Y’; • Constant names also refer to memory. Obvious difference from variables?
Semantics (cont.). • Meaning of whole statements. • Imperative semantics: what does it make the machine do? • 1. “non-executable”: e.g. variable declarations---reserve memory but do not perform an operation. • 2. “executable”: perform some operation on variables e.g. input / assign / output.
Semantics (cont.). • E.g. of executable statements: • // input: • Num1 = Convert.ToInt32 (Console.ReadLine()); • Num2 = Convert.ToInt32 (Console.ReadLine()); • //assignment: • Num 3 = Num1 * Num2; • // output: • Console.Write (Num1 + “ times ” + Num2 + “= ” + Num3);
Semantics (cont.). • Be careful: the assignment operator (=) looks like identity, but isn’t the same. • Total = Total + 5; // doesn’t say Total is equal to Total + 5 (impossible), but…? • An assignment statement is a command, like “Let there be light!” “Ensign, engage!”
Semantics (cont.). • So: • Num = 9; // means Make Num equal to 9. • Total = Total + 5; // means make the new value of total equal the old value plus 5. • To keep things straight, C# uses a different symbol for identity, = = • E.g. if (Num_Visits == 9) Console.Write(“Oh, no, not you again!”);
5. Lab. • We will use the Syntax_Errors.cs program under CSC250 programs.