10
Syntax and Semantics, and the Program Development Process ROBERT REAVES

Syntax and Semantics, and the Program Development Process ROBERT REAVES

Embed Size (px)

Citation preview

Page 1: Syntax and Semantics, and the Program Development Process ROBERT REAVES

Syntax and Semantics, and the Program Development ProcessROBERT REAVES

Page 2: Syntax and Semantics, and the Program Development Process ROBERT REAVES

More About Output

Control vertical spacing by using the endl manipulator in an output statement.

What will the following produce? cout << “Hi There, “ << endl;

cout << endl;

cout << “Lois Lane.” << endl;

Page 3: Syntax and Semantics, and the Program Development Process ROBERT REAVES

It is possible to do in one line. cout << “Hi there, “ << endl << endl << “Lois Lane.” << endl;

Another: cout << “Hi there, “ << endl << endl

<< “Lois Lane.” << endl;

This is to show we can spread a single C++ statement onto more than one line of the program. semicolon

Page 4: Syntax and Semantics, and the Program Development Process ROBERT REAVES

Inserting Blanks Within a Line

To control horizontal spacing of output, one technique is to send extra black characters to the output stream.

cout << “* * * * * *” << endl; Produces:

* * * * * *

These are enclosed in double quotes so they print literally as they are written in the program.

What about: cout << ‘*’ << ‘*’;

Page 5: Syntax and Semantics, and the Program Development Process ROBERT REAVES

Special Characters

\” allows for quotes within a string sent to the output stream.

\n is a newline character, can use instead of endl.

\\ allows for the use of a \ within a string sent to the output stream.

What does produce? cout << ‘”’ << “’”;

cout << “\”” << ‘\’’;

Page 6: Syntax and Semantics, and the Program Development Process ROBERT REAVES

Enter Program

Compile Program

Compile-time Errors?

Run Program

Success!

Logic errors? Go back to algorithm and fix design.

No

No

Yes

Yes Figure out errors.

Page 7: Syntax and Semantics, and the Program Development Process ROBERT REAVES

Summary

Syntax of the C++ language is defined by metalanguage.

Identifiers are used in C++ to name things, some are called reserved words. Have predefined meanings in the language.

Identifiers are restricted to those not reserved by the C++ language.

Identifiers are associated with memory locations by declarations.

Page 8: Syntax and Semantics, and the Program Development Process ROBERT REAVES

Summary

Declarations may give a name to a location whose value doesn’t change, constant, and whose do change, variable.

Every constant and variable has a data type. Common ones:

int

float

char

string, from the standard library

Page 9: Syntax and Semantics, and the Program Development Process ROBERT REAVES

Summary

Program output is accomplished by means of the output stream variable cout, along with the insertion operator (<<). Sends to the standard output device.

endl manipulator tells the computer to terminate the current output line and go on to the next.

Output should be clear, understandable, and neatly arranged.

Page 10: Syntax and Semantics, and the Program Development Process ROBERT REAVES

Summary

A C++ program is a collection of one or more function definitions. One of them must be main.

Execution always begins with the main function.

Functions all cooperate to produce the desired results.