Chapter 2 - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 2

Description:

... to continue with next line Character Escape Sequences Other escape sequences exist for formatting Full listing in Table 2 ... statement terminator Lesson 2.1 ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 16
Provided by: RalphFTo7
Learn more at: https://cse.buffalo.edu
Category:

less

Transcript and Presenter's Notes

Title: Chapter 2


1
Chapter 2 Getting Started
2
(No Transcript)
3
Basic Program Structure
  • include ltiostreamgt
  • using namespace std
  • int main( )
  • cout ltlt This is C!

Lesson 2.1
4
C Syntax
  • Rules for writing statements
  • Semicolon serve as statement terminator
  • Case sensitivity
  • Blank spaces
  • Spacing
  • Accepted modifications

Lesson 2.1
5
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
  • Uses delimiters / comments /

Lesson 2.2
6
Creating a Banner
  • Set of comments at beginning of program
  • name
  • parameters used
  • history
  • author
  • purpose
  • date of program

Lesson 2.2
7
Creating New Lines in Output
  • Programmer must specify new line
  • Line feeding
  • \n in string constant
  • cout ltlt \nwe can jump\n\ntwo lines.
  • endl manipulator
  • cout ltlt endlltltwe can jump
  • cout ltlt endlltlt endl ltlttwo lines.

we can jump two lines.
Lesson 2.3
8
Connecting Strings
  • Can use backslash at end of line to indicate
    string constant to continue with next line

cout ltlt This will \ continue on same line.
is equivalent to
cout ltlt This will continue on same line.
Lesson 2.3
9
Character Escape Sequences
  • Other escape sequences exist for formatting
  • Full listing in Table 2.1
  • Examples
  • \t horizontal tab
  • \v vertical tab
  • \ displays percent character
  • \ displays double quote

Lesson 2.3
10
Debugging
  • Error in program called bug
  • Process of looking for and correcting bugs
  • Three types of errors
  • Syntax
  • Run-time
  • Logic

Lesson 2.4
11
Syntax Errors
  • Mistakes by violating grammar rules
  • Diagnosed by C compiler
  • Must fix before compiler will translate code

coot ltlt endl
cout
int main ( (
)
Lesson 2.4
12
Run-Time Errors
  • Violation of rules during execution of program
  • Computer displays message during execution and
    execution is terminated
  • Error message may help locating error

Lesson 2.4
13
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
14
Debugging Example
ltinclude iostreamgt using namespace std int main
( ) ( cout ltlt Hello world! cout ltlt
Hello again, endl // Next line will
output a name! ccut ltlt Sally
Student / Next line will
output another name / cout ltlt John Senior
include ltiostreamgt
OK
using namespace std
int main ( )

cout ltlt Hello world!
cout ltlt Hello againltlt endl
// Next line will output a name!
cout ltlt Sally Student
/ Next line will output another name
/
cout ltlt John Senior

15
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
Write a Comment
User Comments (0)
About PowerShow.com