Saturday, October 23, 2010

Project 12 Program Errors

For the project 12 subjects I will be comparing Java, C++ and Python. All being object oriented languages they share many things in common, but differ on several points as well.

All three languages provide for try catch statements. The purpose of these statements is to isolate segments of code that may, while the program is running, cause some severe error that would otherwise disrupt the execution of the program, such as a division by 0. By using the exception statements the programmer can prevent sections of code that may cause an issue from terminating the program. Though the exact syntax for exception throwing in the different languages is different and there each language offers a few different options with try catch type statements the basic concept is the same between all three. Reserved words in the languages may only be used for a single purpose, whatever it happens to have been made to do and can not be used as variable or any other identifier names. Common reserved words are things such as: if, while, else, and, except, break and so on. Attempting to use any of these as identifiers will cause errors in compilation.

Java syntax is derived from C and C++, however unlike C++ it is almost exclusively object-oriented. There are no global functions or variables, all code belongs to classes and all values are objects, with the exception of primitive types. Java is case sensitive with everything so a call to System and another call to system will not point to the same place, and will cause an error during compilation. Often times in Java the compiler will not point to where the actual issue is, and you will need to look around to figure out what in the world it wants. Also: fixing one simple issue such as a missing semicolon may cause dozens of other errors to pop up because the compiler hasn't checked those parts because of that first error.

In both Java and C++ lines of code do not necessarily need to be separated, the computer will still be able to read them fine. Example: 


int main ()
{
  cout << " Hello World!";
  return 0;
}


int main () { cout << "Hello World!"; return 0; }
 
Both of these code segments will run properly. However, in Python lines and indentation are rigidly enforced because Python does not end lines with semicolons, as C++ and Java do, so there is nothing directly in the code that allows the computer to know the end of a line other than the return usage at the end.

if True:
    print "True"
else:
  print "False"

if True:
    print "Answer"
    print "True"
else:
    print "Answer"
  print "False"

The first example would run fine, however the second would not because the second line under else: is not indented properly.

No comments:

Post a Comment