Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.

Similar presentations


Presentation on theme: "Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing."— Presentation transcript:

1 Chapter 2 Creating a C++ Program

2 Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing 2.Selection 3.Looping 4.Sub-program

3 Basics All programs must have at least one function All programs must have at least one function We will learn to write many functions We will learn to write many functions We will build more complex programs from simple functions We will build more complex programs from simple functions

4 Parts of the Programs Identifiers Identifiers –A name associated with a function or data object –It is used to reference that function or data object

5 How to Name Identifiers Must start with a letter or underscore Must start with a letter or underscore Followed by a letter, number or underscores Followed by a letter, number or underscores Cannot be a reserved word Cannot be a reserved word

6 Quiz Write true if the identifier’s name good and false otherwise: Write true if the identifier’s name good and false otherwise:_Fred3DArrayR2D2Silly_Name Good Name xBadNameId3nt1f13rBonus:intconst_cast

7 Examples of Reserved Words int int double double const const return return bool bool if if while while for for

8 Data and Data Types Data type Data type –Determines how the data is represented in the computer and how the computer can operate on it Data Data –What all computers use to compute

9 Data Types Two Kinds Two Kinds –Built-in –User Defined Example of Built-in Example of Built-in –int –double –bool –char

10 char Data Type Any single alphanumeric character Any single alphanumeric character –Example –‘A’ –‘a’ –‘B’ –‘1’ –‘^’

11 string Data Type Not a built-in type Not a built-in type Any sequence of characters Any sequence of characters –Example –“Dave” “C++” “ 9 “ Strings cannot span more than one line Strings cannot span more than one line Null String Null String –“”

12 Naming Elements Use a Declaration Use a Declaration int empNum; int empNum; Variables Variables –A location in memory, referenced by an identifier, that contains a data value that can be changed Constants Constants –A location in memory, referenced by an identifier, that contains a data value that cannot be changed

13 Examples int Counter; int Counter; double taxRate, Percent; double taxRate, Percent; const char MIDDLEINITIAL = ‘P’; const char MIDDLEINITIAL = ‘P’; const bool NOTTRUE = “false”; const bool NOTTRUE = “false”;

14 Executable Statements Assignment Statements Assignment Statements –lastName = “Roszak”; Output Statements Output Statements –cout << “Hello World”;

15 Non-executable Statements Comments Comments –//This is how you indicate a comment –/*Or you can do it this way*/

16 Blocks Groups of lines that are to be grouped together between curly braces Groups of lines that are to be grouped together between curly braces Example Example –int main() { cout << “Hello World; }

17 Pre-processor Before the program is compiled, a program called a pre-processor parsers the code looking for directives; things to do Before the program is compiled, a program called a pre-processor parsers the code looking for directives; things to do Example Example –#include –#include –#ifndef –#endif

18 Introduction to Namespaces int main() { cout << “Happy Birthday” << endl; return 0; }

19 More Namespaces #include #include int main() { cout << “Happy Birthday” << endl; return 0; }

20 Still More #include #include int main() { std::cout << “Happy Birthday” << std::endl; return 0; }

21 Yet More #include #include using std::cout; using std::endl; int main() { cout << “Happy Birthday” << endl; return 0; }

22 One More #include #include using namespace std; int main() { cout << “Happy Birthday” << endl; return 0; }

23 More Output What will the following lines produce? What will the following lines produce? –cout << “Hi there. “ << endl; –cout << endl; –cout <<“What are you doing?” << endl;

24 More Output What will these produce? What will these produce? –cout << “Hi there. “ << endl << endl <<“What are you doing?” << endl; How about these? How about these? –cout <<“Hi there. \n\nWhat are you doing?\n”


Download ppt "Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing."

Similar presentations


Ads by Google