1 / 20

Syntax and semantics of C#.

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.

Télécharger la présentation

Syntax and semantics of 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. Syntax and semantics of C#. 1. Review. 2. Syntax vs. semantics. 3. Syntax. 4. Semantics. 5. Lab.

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

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

  4. 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”).

  5. 3. Syntax. • For example, • Answer = Num + 5; // correct • Num + 5 = Answer; // incorrect—WHY?

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

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

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

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

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

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

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

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

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

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

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

  17. 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);

  18. 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!”

  19. 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!”);

  20. 5. Lab. • We will use the Syntax_Errors.cs program under CSC250 programs.

More Related