0% found this document useful (0 votes)
95 views4 pages

Discussion Forum Unit 1

Syntax refers to the structure and rules of a programming language, while semantics refers to the meaning and behavior of programs. A syntax error occurs when the code has invalid grammar, like using the wrong case for a variable type. A semantic error means the code structure is valid but the program logic is incorrect, such as performing an invalid operation on incompatible types. The document provides Java examples of each type of error to illustrate the difference between syntax and semantics.

Uploaded by

Jisca Noubissi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views4 pages

Discussion Forum Unit 1

Syntax refers to the structure and rules of a programming language, while semantics refers to the meaning and behavior of programs. A syntax error occurs when the code has invalid grammar, like using the wrong case for a variable type. A semantic error means the code structure is valid but the program logic is incorrect, such as performing an invalid operation on incompatible types. The document provides Java examples of each type of error to illustrate the difference between syntax and semantics.

Uploaded by

Jisca Noubissi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Discussion Forum Unit 1

Hello everyone

Development environment used for this discussion: NetBeans IDE 8.2

What is meant by the syntax and the semantics of a programming


language?
Before getting into the differences between Syntax and Semantics, we first need to
understand what a programming language is. A programming language is a formal language
comprising a set of instructions that produce various kinds of output. Programming languages are
used in computer programming to implement algorithms. Simply put, according to TechTerms
(Programming Language Definition, 2011)”A programming language is a set of commands,
instructions, and other syntax use to create a software program.”

Syntax is basically defined as the structure of a program. Every language whether Formal
or natural has general rules concerning the formation of words and sentences and that is exactly
what syntax is. It is the rules that determine what is allowed in the programming environments,
the rules that define the structure of a language.

Whereas Semantics is the meaning of the programs and how they behave. According to
(Eck. 2019)” the semantics of a programming language is the set of rules that determine the
meaning of a program written in that language. A semantically correct program is one that does
what you want it to”
You see, Syntax only cannot make up for the whole program. In order to have a good
functioning program as we like, we also need the Semantics. Both are equally important for the
program in a programming language. Now, let’s talk about the difference between a syntax
error and a semantic error

Difference between a syntax error and a semantic error and Java


examples to illustrate the difference

A syntax error is an error that appears in the script or source code of a program. It is a
grammatical mistake that is sometimes limited to a single character
For example:

code
public class Syntaxerror {

public static void main(String[] args) {


int age = 19;
string school = "UOPeople";
// this will give an error because string is suppose to have a capital letter S
System.out.println("The student age is " + age + "and the student attends" +school);

Output

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code -


cannot find symbol

symbol: class string

location: class syntaxerror.Syntaxerror

at syntaxerror.Syntaxerror.main(Syntaxerror.java:17)

C:\Users\yunji1\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java
returned: 1

BUILD FAILED (total time: 1 second)

As you can see on the above example, my program faced an error, and that error is that I
have written the‘s’ of string with a small letter instead of a capital letter.

Let’s look at the correct code:

int age = 19;

String school = "UOPeople";

// You can see that my string has a capital letter now

System.out.println("The student age is " + age + "and the student attends" +school);

Output:

run:

The student age is 19 and the student attends UOPeople

BUILD SUCCESSFUL (total time: 0 seconds)


A semantic error happens when the syntax of the code is correct but the usage of the
code is not correct, it is an improper use of java statements.

Example:

Code

public static void main(String[] args) {

//first example

//I have attributed a String to and int wich result in a error

// the types String and int are not compatible

int age = "food";

System.out.println(age);

//second example

//Use of a non-initialized variable

int number;

number++

//third example

// Errors in expressions

String s = "...";

int a = 5 - s;// the - operator does not support arguments of type String

With the above examples, you can see the illustrated difference between a syntax and a
semantic error.
References

Programming Language Definition. (2011, September 23). TechTerms.

https://techterms.com/definition/programming_language

Eck, D. J. (2019). Introduction to programming using Java, version 8.1. Hobart and William

Smith College. http://math.hws.edu/javanotes.

You might also like