DEV Community

Tiago Mateus
Tiago Mateus

Posted on

Syntax, Semantics, and Scope in the Universe of Programming

Syntax: The set of rules of a given programming language, For exemple, in Java, to declare a varible of type text, you use String, while in C# use string. The simple variotion in lowercase and uppercase is a spcific syntax of a rule of each language.

Semantics: It refers to the meaning of the code — whether what has been written makes logical sense. For example, if a programmer defines a method called sum(), it should perform an actual sum, not something unrelated.

Scope: Scope is simply the context or space that defines where a certain piece of code or a set of variables can be accessed. See the example below:

public class Scope {
    String globalName = "Tiago";

    public void printGlobalName() {
        System.out.print(globalName);
    }

    public void printLocalName() {
        String localName = "Mateus";
        System.out.print(localName);
    }
}
Enter fullscreen mode Exit fullscreen mode

In the example above, we see two variables belonging to different scopes: one belongs to the class-level (global scope) — globalName, and the other belongs to the method-level (local scope) — localName.

Top comments (0)