Section 3 - Program Statements

2A.3.1 Declarations and Executables

For your first programs, you can think of the statements within main()'s braces as organized into two parts:

  1. the declarations/definitions - I sometimes use one or the other word, but technically they have different meanings. For simple programs, there will be no difference.
  2. the executable statements.

Every statement, whether it is a declaration or an executable statement is set apart from the rest with either semicolons, ";", or braces, "{ ...}." We will only consider semicolons in this lecture.

The form of the executable statement part is:

<statement> ;
<statement> ;
<statement> ;

and so on. Remember that these statements might be on one line, like

<statement> ; <statement> ;

or there may be one statement on several lines:

<sta
teme
nt> ;

2A.3.2 Declarations and Executables

Typical statements are found inside the main() method:

int main()
{
int x;
double y;

x = -234;
y = 3.1345;

cout << "X is " << x << " and Y is " << y << endl;
}

The first two are type declarations, or just declarations. They identify the variables x and y to the compiler as objects that you will use to hold data. The last three are executables; they do something with the variable objects x and y that are defined above.

It is good programming to place your declarations first, then separate them by a blank line from your executables (like I did above). You can have additional blank lines as needed for clarity in later parts of the program, but don't forget this one important division.

The above example will generate the following output on the console screen:

X is -234 and Y is 3.1345  

The exact meaning of each statement is something you won't completely understand yet, but it would be good for you to try to see why those C++ statements might result in the above output.

2A.3.3 Comments

In addition to statements, you can put comments in your program. Comments are not compiled into the executable file (the .exe file)  but are used by the programmer to document the code in the source file only (the .cpp file). Comments are set off from the rest of the program using the  syntax, /* and */, which can enclose any comment, even ones that run across multiple lines:

int x;    /* x will hold the temperature of the radiator */
float y;  /* y will be used to describe the percentage
accuracy of the gaming strategy */

/* the next line initializes x to the lowest temperature */
x = -234;
y = 3.1234;   /* at first the strategy is very imperfect */
dog =    /* 1.1  this doesn't work so I use: */       1.2;

Or you can use another type of comment: a double forward slash, //, which is used to begin a comment that automatically ends with the newline:

y = 3.1234;  // The next line is not in comment
z = 4;

To comment out several lines, it is more efficient to use /* */, but it is easier to see which lines are commented out if you put a // at the beginning of each line.

This is the basic structure of a C++ Program.  In the next sections we learn the individual components that comprise statements and how to write meaningful statements that combine to form meaningful programs.