Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements.

Similar presentations


Presentation on theme: "Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements."— Presentation transcript:

1 Chapter 2: Introduction to C++

2 Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements Variables (Memory locations)

3 Parts of a C++ Program Comments//A simple program Preprocessor directives #include Namespacesusing namespace std; Main functionint main ( ) Braces{ and } Output statementscout << “Programming is fun”; String literals (constants) “Programming is fun”

4 Parts of a C++ Program Semicolons (end statements) ; Return statementreturn 0; Special characters// # ( ) { } “ “ ; Must put the parts together correctly!

5 // A simple C++ program #include using namespace std; int main() { cout << "Programming is great fun!"; return 0; }

6 The cout Object cout outputs to standard output (monitor) cout is a stream object – works with streams of data –data flows from your program to the monitor Uses the << operator (inserts data into the stream cout) Examples: –cout << “Programming is great fun. “; –cout << “Programming is “ << “great fun.”; –cout << “Programming is “; cout << “great fun.”;

7 More Examples using cout –Using endl cout << “Select an option below” << endl; cout << “Ham” << endl; cout << “Tuna” << endl; –Using escape sequences cout << “Select an option below\n” ; cout << “\tHam\n” ; cout << “\tTuna\n” ;

8 2.4 Standard and PreStandard C++ We’ll use standard C++ We’ll use namespaces

9 2. 5 Variables, Constants and Assignment Variables represent storage locations in computer’s memory whose values can vary (or change). Constants represent storage locations in computer’s memory whose values cannot change (but must stay constant). An assignment statement is ONE way we can store values in variables.

10 // This program has a variable. #include using namespace std; int main() { int number; number = 5; cout << "The value of number is " << "number" << endl; cout << "The value of number is " << number << endl; number = 7; cout << "Now the value of number is " << number << endl; return 0; } Variable definition An assignment of a value to a variable

11 Variable definition –Tells the computer the name and type of data a memory location will hold. –Usually place at the beginning of the main function –Note: They end with a ; Variables have several attributes –Name –Type –Value –purpose

12 Assignment Statement Made up three parts –Variable on the left-hand side that will get a new value –Assignment operator (=) –Expression on the right-hand side that is evaluated Examples: number = 5; average = sum / 10;

13 Input cin inputs from standard input (keyboard) cin is a stream object – works with streams of data –data flows from the keyboard to the program Uses the >> operator (removes data from the stream cin to store in a variable) Usually preceded by a prompt. Example: –cout << “Enter a number “; //note: no endl –cin >> number;

14 Constants Literal constants –“Programming is fun.” –5 –10 –0.0825 Defined constants –const double rate = 0.0825; –These go after using namespace std;

15 Another Simple Program

16 Identifiers Identifier is a programmer-defined name that represents some element of a program. Name should indicate what the element is is used for! int itemOrdered; Rules: –Cannot use a keyword (Table 2-4) –1 st character is a letter or underscore ( _ ) –Other characters are letters, digits or _ –Case sensitive –Style issue: start with lowercase, successive words capitalized.

17 Data Types All variables have a type –int itemsOrdered; –double interestEarned; Many different types in C++ –int, double, char, bool, etc. –Ranges of values they hold in Table 2-6 –Also a string, but will need to add directive #include

18 Examples char letter; letter = ‘A’ double average; average = sum /10; cout << “The average is “ << average << endl; string name; name = “Stringfellow”; cout << “My name is << name << endl;


Download ppt "Chapter 2: Introduction to C++. Language Elements Keywords Programmer-defined symbols (identifiers) Operators Punctuation Syntax Lines and Statements."

Similar presentations


Ads by Google