Professional Documents
Culture Documents
Chap 02
Chap 02
Marks beginning of function body object cout << This is C++!; string C++ statement characters within
double quotes insertion operator used to print to terminator semicolon: statement screen
C++ Syntax
Rules
for writing statements Semicolon serve as statement terminator Case sensitivity Blank spaces Spacing Accepted modifications
Lesson 2.1
Comments
Notes
in program describing what code does Perform no action in program Single line comment structure
Begin with two slashes (no space between) // Line by itself or following statement
Multiline
comment structure
starts comment ends comment
Creating a Banner
Set
feeding
\n in string constant cout << \nwe can jump\n\ntwo lines.; endl manipulator we can jump cout << endl<<we can jump ; cout << endl<< endl <<two lines.;
two lines. Lesson 2.3
Connecting Strings
Can
use backslash at end of line to indicate string constant to continue with next line cout << This will \ continue on same line. ; is equivalent to
escape sequences exist for formatting Full listing in Table 2.1 Examples:
\t \v \% \
Lesson 2.3
horizontal tab vertical tab displays percent character displays double quote
Debugging
Error
in program called bug Process of looking for and correcting bugs Three types of errors
Syntax Run-time Logic
Lesson 2.4
Syntax Errors
Mistakes
by violating grammar rules Diagnosed by C++ compiler Must fix before compiler will translate code spelling cout << endl; coot int main ( ) ( mismatched parentheses
Lesson 2.4
Run-Time Errors
Violation
program Computer displays message during execution and execution is terminated Error message may help locating error
Lesson 2.4
Logic Errors
Computer
does not recognize Difficult to find Execution is complete but output is incorrect Programmer checks for reasonable and correct output
Lesson 2.4
Debugging Example
#<include iostream> # include <iostream> using namespace std; OK using namespace std; int main ( ) int main ( ); { ( cout << Hello world!; cout << Hello world! cout << Hello again, endl; cout << Hello again<< endl; // Next line will output a name! // Next line will output a name! cout << Sally Student; ccut << Sally Student; /* Next line will /* Next line will output another name */ output another name /* cout << John Senior ; cout << John Senior; } }
Summary
Learned about:
General
program structure Rules of C++ syntax Creating comments and program banners Using escape sequences to format output Debugging programs Three types of errors: syntax, run-time, logic
Chapter 2