3A. Programming Language Syntax


Syntax

The syntax of a language tells what the language looks like. What is the required word order? What do symbols mean and how are they used?

Programming languages have a rigid syntax, and you can only write programs effectively if you know the syntax, at least of the part of the language that you use. Never try guessing what the syntax is. Stay within what you know. As you learn new things, you can incorporate them into your programs.


Reserved words

Some words are reserved. For example, in C++, word if can only be used to start an if-statement. You cannot have a variable called if.


Free form

Like many programming languages, C++ is free-form. That means that in most contexts, as far as the compiler is concerned:


Indentation

Human readers need more help than compilers in understanding a computer program. Use free-formness to advantage by using indentation to lay out a program. We will see particular ways to indent programs, but here are three simple rules:

  1. If a line is "part of" a prior line, then it must be indented more than the beginning of the prior line. For example, we will see how to use printf later. A long printf line should be broken into more than one line.

      printf("I have two cats, and their names are %s and %s\n",
             firstCatName, secondCatName);
    
  2. A group of statements that are enclosed in braces must be indented more than the braces, as in:

      {
        x = y;
        y = w;
      }
    

  3. If two statements are done one after the other, they must be indented the same amount, as in:

      x = y;
      y = w;
    

When you indent a C++ program, it is conventional to indent 2 spaces per level of indentation.

Indentation is important, and you cannot ignore it. Indentation improves readability, so keep your program well indented while you are working on it. The standards for this course require a submission to be well indented. I read your submissions, and poorly indented programs are difficult to read. If you submit a program without any indentation, expect to lose 50% of the possible score for that assignment.


Line length

Text editors for writing software are not document formatters like Microsoft Word. Since C++ is free-form, there is no need for long lines, and long lines make software difficult to read and difficult to print. For this course, the standards require you to limit lines to 80 characters. If you go beyond that a little on a few lines, you will not be penalized. But if you use any lines that are more than 100 characters long, you will certainly be penalized.


Comments

A comment is ignored by the compiler. It is intended to help human readers. In C++, there are two forms of comments.

  1. Form

      /* This is a comment.
         it can extend over multiple lines */
    
    is useful for longer comments. It begins with /* and ends with */. You cannot embed one long comment inside another; the first */ after /* ends the comment.

  2. Comment form

      // This is a comment.
    
    ends at the end of the line. (It is an exception to the rule that a line break is equivalent to a space.)

Indent comments the same amount as the code that surrounds them.


Case of letters

C++ and Java are both case-sensitive: letters r and R are considered different letters. You will need to be consistent in the names that you use. TheRose and therose are different names.


Java and C++

Java is a newer language than C++. It was designed to be easy for C++ programmers to learn by making much of its syntax identical to C++ syntax.

But it works both ways. If you know Java, it is easy to learn basic C++ because of those similarities in syntax.


Exercises

  1. Can you have a variable called while in a C++ program? Answer

  2. Can a C++ expression extend over multiple lines? Answer

  3. Are you required to indent your program well? Answer

  4. When should I indent my program? Answer