2D. Programming Language Syntax and Layout

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?

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;
    
    The same rule applies regardless of what kinds of statements are done in sequence. For example, in
      {
        x = y;
      }
      y = w;
    
    statement
      {
        x = y;
      }
    
    is immediately followed by
      y = w;
    
    Those two statements each begin in the same column.

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

I will not read a very poorly indented program. If you submit a program without any indentation, you will up to lose 50% of the possible score for that assignment for extremely poor indentation

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.

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 you indent your program? Answer