100 likes | 209 Vues
Learn about controlling output spacing, special characters, and program development in C++. Understand syntax, semantics, and the program development process with summaries on identifiers, data types, and functions.
E N D
Syntax and Semantics, and the Program Development Process Robert reaves
More About Output • Control vertical spacing by using the endlmanipulator in an output statement. • What will the following produce? • cout << “Hi There, “ << endl; • cout << endl; • cout << “Lois Lane.” << endl;
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
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 << ‘*’ << ‘*’;
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 << “\”” << ‘\’’;
Enter Program Compile Program Compile-time Errors? Yes Figure out errors. No Run Program Yes Logic errors? Go back to algorithm and fix design. No Success!
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.
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
Summary • Program output is accomplished by means of the output stream variable cout, along with the insertion operator (<<). • Sends to the standard output device. • endlmanipulator tells the computer to terminate the current output line and go on to the next. • Output should be clear, understandable, and neatly arranged.
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.