syntax and semantics of c n.
Download
Skip this Video
Download Presentation
Syntax and semantics of C#.

Loading in 2 Seconds...

play fullscreen
1 / 20

Syntax and semantics of C#. - PowerPoint PPT Presentation


  • 65 Views
  • Uploaded on

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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Syntax and semantics of C#.' - rogan-flores


Download Now 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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
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
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
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
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 syntax1
3. Syntax.
  • For example,
  • Answer = Num + 5; // correct
  • Num + 5 = Answer; // incorrect—WHY?
syntax cont
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 cont1
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 cont2
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 cont3
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
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
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 cont1
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 cont2
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 cont3
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 cont4
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 cont5
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 cont6
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 cont7
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 cont8
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
5. Lab.
  • We will use the Syntax_Errors.cs program under CSC250 programs.
ad