Chapter 2 Getting Started Basic Program Structure include

Chapter 2 – Getting Started


Basic Program Structure #include <iostream> using namespace std; int main( ) Header Location of header files Primary function { Marks beginning of function body object cout << “This is C++!”; string – C++ characters within statement } Lesson 2. 1 double quotes insertion operator used to print to terminator screen semicolon: statement Marks end of function body

C++ Syntax u Rules for writing statements u Semicolon serve as statement terminator u Case sensitivity u Blank spaces u Spacing u Accepted modifications Lesson 2. 1

Comments u Notes in program describing what code does u Perform no action in program u Single line comment structure – Begin with two slashes (no space between) // – Line by itself or following statement u Multiline comment structure – Uses delimiters /* comments */ starts comment Lesson 2. 2 ends comment

Creating a Banner u Set of comments at beginning of program – name – parameters used – history – author – purpose – date of program Lesson 2. 2 //************ // Name: Sally Student // Purpose: Assignment 2 // Date: 11/22/2003 // Reference: Chapter 2, #3 //************ #include <iostream>. . .

Creating New Lines in Output u Programmer u Line must specify new line feeding – n in string constant cout << “nwe can jumpnntwo lines. ”; – endl manipulator we can jump cout << endl<<“we can jump “; cout << endl <<“two lines. ”; two lines. Lesson 2. 3

Connecting Strings u 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 cout << “This will continue on same line. ”; Lesson 2. 3

Character Escape Sequences u Other escape sequences exist formatting u Full listing in Table 2. 1 u Examples: t v % ” Lesson 2. 3 horizontal tab vertical tab displays percent character displays double quote

Debugging u Error in program called bug u Process of looking for and correcting bugs u Three types of errors – Syntax – Run-time – Logic Lesson 2. 4

Syntax Errors u Mistakes by violating “grammar” rules u Diagnosed by C++ compiler u Must fix before compiler will translate code spelling cout coot << endl; int main ( )( Lesson 2. 4 mismatched parentheses

Run-Time Errors u Violation of rules during execution of program u Computer displays message during execution and execution is terminated u Error message may help locating error Lesson 2. 4

Logic Errors u Computer does not recognize u Difficult to find u Execution is complete but output is incorrect u Programmer checks for reasonable and correct output Lesson 2. 4

Debugging Example # include <iostream> #<include iostream> OK using namespace std; int main ( ); { ( cout << “Hello world!”; cout << ‘Hello world!’ cout << “Hello again”, endl; cout << “Hello again”<< endl; // Next line will output a name! cout << “Sally Student”; ccut << “Sally Student”; /* Next line will output another name */ output another name /* cout << “John Senior” ; cout << John Senior; } }

Summary Learned about: u General program structure u Rules of C++ syntax u Creating comments and program banners u Using escape sequences to format output u Debugging programs u Three types of errors: syntax, run-time, logic Chapter 2
- Slides: 15