Program Statements

A statement is anything in a program that actually performs some sort of work at run-time. It could compute a value or perform input or output. Variable definitions are not statements, not are preprocessor directives.

C Statements

In C, a statement is any expression followed by a semicolon. All of the following are valid statements as far as a C compiler is concerned:

    area = M_PI * radius * radius;
    printf("The area is %f.\n", area);
    5;
    area + 2;
            

The last two statements above don't accomplish anything, of course, but are nevertheless perfectly valid. The C compiler may issue a warning about a statement with no effect, but the program will compile and run.

Part of the design philosophy behind the C language was "trust the programmer". Hence, C enforces a minimal number of rules. A C compiler is not bogged down with code to forbid things in order to prevent you from shooting yourself in the foot. Trusting the programmer makes it easier for the programmer to do certain things that the compiler-writer might not have anticipated. Most languages that came before C, and some that came after, are considered somewhat obstructive by many people.

The absence of draconian rules makes the C compiler much simpler and faster. Programmers tend to learn from their mistakes anyway, so it isn't necessary for the compiler to contain extra code to police them.

Every expression in a C program has a value, including the call to the printf() function, which returns the number of characters printed, and the assignment statement above, which has a value of the number assigned. We are simply free to ignore the value if we choose. The fact that an assignment statement is an expression with a value allows us to string assignments together:

    a = b = c = 10;
            

The expression c = 10 has a value of 10, which is assigned to b, and so on. It is as if we had written the following:

    a = (b = (c = 10));
            

Fortran does not allow this, since an assignment statement has a specific syntax of:

    value = expression
            
Fortran Statements

Unlike C, where a statement is any expression followed by a semicolon, there are two distinct types of statements in Fortran, described below.

Subroutine Calls

A subroutine call statement jumps to another block of code, which could be part of the program, part of a library of subprograms stored in another file, or could be intrinsic to (built into) the Fortran language. Some commonly used intrinsic subroutines include:

  • read: Inputs data from the standard input or any other input stream.
  • write: Outputs data to the standard output, or any other specified output stream.
  • print: Outputs data to the standard output stream. The print statement is a shorthand for write.
write (*, *) 'What is the radius of the circle?'
print *, 'What is the radius of the circle?'
                
Assignment Statements

An assignment statement assigns a new value to a variable, overwriting what was previously contained at that memory address.

area = PI * radius * radius
                

Both kinds of statements can contain expressions, but neither actually is an expression in Fortran.

Practice

Note

Be sure to thoroughly review the instructions in Section 2, “Practice Problem Instructions” before doing the practice problems below.
  1. What is a statement in C?

  2. What is a statement in Fortran?