Website for the College study materials and cheats you can use

Translate

DISCUSSION UNIT 1 Introduction to Programming

PROGRAMMING 1

                                                                                                                            CHICK ETIEN NDEH

                                                                                                                   University of the People

DISCUSSION UNIT 1

Introduction to Programming



Briefly explain what is meant by the syntax and the semantics of a programming language. Give an example to illustrate the difference between a syntax error and a semantics error.



Any syntax of a programing language shows its grammar, and the semantics is its meaning. A program with a syntax error cannot be compiled reason been that during the code an error courses the program not to give any output.

while a program with a semantic error can be compiled and run but gives an incorrect result. In this case, the code will run smoothly but will give a different meaning rather than the output expected.

According to (Arnab S IITian, JavaHolic, Raconteur 2017 ) Syntax errors occur during the parsing of input code and are caused by grammatically incorrect statements. Typical errors might be an illegal character in the input, a missing operator, two operators in a row, two statements on the same line with no intervening semicolon, unbalanced parentheses, a misplaced reserved word, etc.


Semantic errors occur during the execution of the code after it has been parsed as grammatically correct. These have to do not with how statements are constructed, but with what they mean. Such things as incorrect variable types or sizes, nonexistent variables, subscripts out of range, and the like, are semantic errors.


 A missing semicolon in a program is an example of a syntax error because the compiler will find the error and report it. If N is an integer variable, then the statement "frac = 1/N;" is probably an error of semantics. The value of 1/N will be 0 for any N greater than 1. It's likely that the programmer meant to say 1.0/N.



Splitting a string over two lines: In most cases, Java doesn’t care if your code appears on one or more lines. However, if you split a string across lines so that the string contains a newline character, then the compiler will object.

The answer is to end the string on the first line with a double quote, add a plus sign to tell the compiler you want to concatenate (add) this string with another string, and then continue the string on the next line like this (John P. M  Java eLearning Kit For Dummies April 2014.p01, para 2)


      System.out.print("This is a really long " +
            "string that appears on two lines.");
Semantic errors

Use of a non-initialized variable:

int i;

i++; // the variable i is not initialized

 Errors in expressions:

String s = "...";

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


References

John P. M  Java eLearning Kit For Dummies April 2014.p01,para 2 retrieved from https://www.dummies.com/programming/java/syntactical-errors-in-java/
Arnab S IITian, JavaHolic, Raconteur 2017 retrieved from https://www.quora.com/What-are-the-differences-between-syntax-errors-and-semantic-errors

0 comments:

Post a Comment

Popular Posts

Categories

Contact Form

Name

Email *

Message *