1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture Programming in C++ - Chapter 2: Getting started

14 43 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 14
Dung lượng 363,78 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

The main contents of this chapter include all of the following: Basic program structure, C++ syntax, creating a banner, sonnecting strings, character escape sequences, debugging, run-time errors, logic errors.

Trang 1

Chapter 2 – Getting Started

Trang 2

#include <iostream>

using namespace std;

int main( )

{

    cout << “This is C++!”;

}

Header

Primary function Marks beginning of function body

C++ statement Marks end of function body

Location of header files

Lesson 2.1

object

insertion operator used to print to screen

string – characters within        double quotes

semicolon: statement terminator

Trang 3

Rules for writing statements

Semicolon serve as statement terminator Case sensitivity

Blank spaces

Spacing

Accepted modifications

Lesson 2.1

Trang 4

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

starts comment     ends comment

Trang 5

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>

.

Trang 6

Programmer must specify new line

Line feeding

– \n  in string constant

Lesson 2.3

– endl manipulator

   cout << endl<< endl <<“two lines.”;

we can jump

two lines.

Trang 7

Can use backslash at end of line to indicate  string constant to continue with next line

Lesson 2.3

cout << “This will \ continue on same line.” ; cout << “This will continue on same line.”;

is equivalent to

Trang 8

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

Trang 9

Error in program called bug

Process of looking for and correcting bugs Three types of errors

– Syntax

– Run­time

– Logic

Lesson 2.4

Trang 10

Mistakes by violating “grammar” rules

Diagnosed by C++ compiler

Must fix before compiler will translate code

Lesson 2.4

coot << endl;

int main ( ( mismatched 

parentheses )

Trang 11

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

Trang 12

Computer does not recognize

Difficult to find

Execution is complete but output is 

incorrect

Programmer checks for reasonable and  correct output

Lesson 2.4

Trang 13

#<include iostream>

using namespace std;

int main ( );

(

    cout << ‘Hello world!’

    cout << “Hello again”, endl;

    // Next line will output

       a name!

    ccut << “Sally Student”;

     /* Next line will

       output another name /*

    cout << John Senior;

}

# include <iostream>

OK using namespace std;

int main ( ) {

cout << “Hello world!”;

cout << “Hello again”<< endl; // Next line will output a name! cout << “Sally Student”;

/* Next line will        output another name */ cout << “John Senior” ;

}

Trang 14

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

Learned about:

Ngày đăng: 30/01/2020, 04:36

TỪ KHÓA LIÊN QUAN