Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bill Tucker Austin Community College COSC 1315

Similar presentations


Presentation on theme: "Bill Tucker Austin Community College COSC 1315"— Presentation transcript:

1 Bill Tucker Austin Community College COSC 1315
Chapter 2 Lecture Notes Bill Tucker Austin Community College COSC 1315 Copyright © 2002 W. A. Tucker Copyright © 2002 W. A. Tucker

2 C++ Syntax C++ is case sensitive
This means that “Include” is not the same as “include” All C++ statements end with a semi-colon Copyright © 2002 W. A. Tucker

3 Reserved Words There are approximately 50 reserved words within C++
All reserved words are lowercase A reserved word will usually show up on the screen in blue Reserved words will be introduced throughout this course Copyright © 2002 W. A. Tucker

4 C++ Syntax Comments The compiler ignores all comments
Comments are usually shown in green on the screen // Everything after the // until the line return is a comment This is the preferred method for single line comments /* */ Everything between the /* and */ is a comment This work well for multiple line comments, to comment out a block of code when debugging a program, and to add sample output at the end of a program Copyright © 2002 W. A. Tucker

5 C++ Syntax compiler directive #include <iostream>
begins with a # and is processed by the pre-processor #include <iostream> This directive tells the pre-processor to go find a library called iostream The iostream include library provides the necessary code that supports the console input and output The iostream include library is the 1998 ANSI standard for console input and output and is NOT the same as iostream.h (which some tutors might tell you to use) NOTE: include is a reserved word Copyright © 2002 W. A. Tucker

6 C++ Syntax using statement using namespace std;
This statement provides access to what is essentially a library of code that supports the standard names supported by the 1998 ANSI standards. This statement should follow all of the #include compiler directives NOTE: using and namespace are both reserved words. Copyright © 2002 W. A. Tucker

7 C++ Syntax Every C++ program begins execution at a function called “main” Function definitions have two parts Function header EX: int main() Body of the function All of the code that is contained between the open and close curly braces EX: int main() { // body of the function } Copyright © 2002 W. A. Tucker

8 KEY Programming Concepts
Every value that a programmer uses within a program falls into one three categories Named Variable Named Constant Literal Constant Every value has a data type associated with that value This association is either declared or implied Every value is stored in the memory of the computer at some memory address Copyright © 2002 W. A. Tucker

9 C++ Syntax Declaration statements
Every value that a programmer wants to refer to by a name must first be declared with a declaration statement Declaration statements have the ability to perform three tasks They define a name (called an identifier) that the compiler will recognize They allocate the proper amount of memory for each identifier and map the contents to that specific address Optionally, they may initialize (at program load time) the contents of the identifier Copyright © 2002 W. A. Tucker

10 Identifier Names Rules Names in C++ are case sensitive
Names may be very long (255 characters) Names may contain letters, numbers and only the special character underscore Names may NOT begin with a number Names must begin with a letter (at this stage in your C++ knowledge) Names may NOT contain blanks Names may not be a reserved word Copyright © 2002 W. A. Tucker

11 Identifier Names Style Conventions Use names that are descriptive
Standard abbreviations are fine Begin all variables with a lower case letter For variable names that contain more than one word, capitalize the first letter of every word after the first word (this is called the “Hungarian notation”) EX: a variable called miles per hour would be named milesPerHour For named constants, capitalize the entire name, and use the underscore to separate each word EX: a constant named kilometers per mile would be named KM_PER_MILE Copyright © 2002 W. A. Tucker

12 Data Types There are more than 10 different data types supported by C++ For this course you only need to use FIVE data types whole numbers (no decimal point) real numbers (with a decimal point) character (single character) strings of characters boolean values (true or false) Copyright © 2002 W. A. Tucker

13 Specific Data Types int is the data type (reserved word) to define a whole number double is the data type (reserved word) to define a real number char is the data type (reserved word) to define a single character string is the user defined data type (class) to define a string of characters bool is the data type (reserved word) to define a boolean value Copyright © 2002 W. A. Tucker

14 C++ Syntax More on Declaration Statements
The general format of a declaration statement is data_type identifier; EX: double miles; Optionally, it may initialize the value (at load time) [NOTE: This is NOT an executable instruction.] data_type identifier = initial_value; EX: double miles = 0; Copyright © 2002 W. A. Tucker

15 Literal Values Every literal value has an implied data type
int – whole numbers EX: double – real numbers EX: char – characters EX: ‘A’ string – strings of characters EX: “This is a string of characters” bool – boolean value EX: true or false Copyright © 2002 W. A. Tucker

16 Named Variables The following are examples of declaring named variables: int loopCount = 0; double hourlyWage = 0; char courseGrade = ‘A’; string message = “Test Message”; bool raining = false; NOTE: These are all variables and their initial values may be changed once the program begins execution. Copyright © 2002 W. A. Tucker

17 Named Constants The following are examples of declaring named constants: const int MONTHS_PER_YR = 12; const double PI = ; const char YES = ‘Y’; const string MSG1 = “This is message one”; const bool TRUE = true; NOTE: These are all constants and their initial values may NOT be changed once the program begins execution. Copyright © 2002 W. A. Tucker

18 Executable Instructions
Executable instructions are instructions that are executed while the program is running. They may use variables, named constants and literal constants but they can only change the value of a variable. The miles.cpp program contains several executable instructions Assignment statements Input / Output statements return statement Copyright © 2002 W. A. Tucker

19 Assignment Statement Assignment statements
Assignment statements have a variable name on the left hand side of an equals sign and some expression or value on the right hand side of the equals sign. Assignment statements are NOT mathematical identities. First, the right hand side is evaluated based upon the value of the expression AT THAT MOMENT IN TIME. Next, the result is assigned to the variable name that is on the left hand side of the equals sign. Copyright © 2002 W. A. Tucker

20 Input / Output Statements
Console input (cin) EX: cin >> miles; The value entered by the user on the keyboard (console input) is stored into the variable named miles Console output (cout) EX: cout << kms; The current value of kms is displayed on the monitor (console output) NOTE: Both cin and cout require the #include <iostream> compiler directive Copyright © 2002 W. A. Tucker

21 Stream Input/Output All input and output is treated like a stream (or flow) of characters. Output Stream The characters that are inserted into the output stream are added to what was there already. Copyright © 2002 W. A. Tucker

22 Output Formatting A line return may be added to the output stream using endl (an I/O manipulator inside iostream) The following two sets of instructions produce the same output. cout << “The distance in kilometers is “; cout << kms; cout << endl; OR cout << “The distance in kilometers is “ << kms << endl; NOTE: Without the blank space after “is”, the phrase and number will run together on the output. Copyright © 2002 W. A. Tucker

23 return statement The return statement is used to return a value to the program that “called” the current program being executed. In the case of the main function, the “calling program” is the operating system. Returning an integer value of zero indicates that the program had a normal execution. EX: return 0; // program ended normally There will be more information about the return statement when we cover functions. Copyright © 2002 W. A. Tucker

24 Putting it all Together
A C++ program will have the following general format // header block // compiler directives #include <iostream> using namespace std; int main () { // body of main function return 0; } // end of main function Copyright © 2002 W. A. Tucker

25 Arithmetic Expressions
The mathematical operators in C++ are Addition (+) Subtraction (-) Multiplication (*) Division (/) Modulus (%) Copyright © 2002 W. A. Tucker

26 Integer Arithmetic Division of two integers will create a result that is an integer 6 / 3 will evaluate to an integer 2 7 / 3 will also evaluate to an integer 2 Modulus operator calculates the integer remainder 6 % 3 will evaluate to an integer 0 7 % 3 will evaluate to an integer 1 The combination of these two operations is very useful in certain applications (ie: ATM machine) Copyright © 2002 W. A. Tucker

27 Mixed Type Arithmetic Expressions containing both integer and double values will evaluate to a double Order of operations (simplified) is Parenthesis Unary operation (+ and – of just one term) Multiplication, Division and Modulus Addition and Subtraction (of two or more terms) Expressions are evaluated left to right for the same order of operations (except unary operations) Copyright © 2002 W. A. Tucker

28 Evaluate the Following
3 * 6 / * 10 / 4 3 * 6 / * 10 / 4 3 * 6 / * 10.0 / 4 (3 * 6 / 8) + (3 * 10 ) / 4 3 * (6 / 8 + 3) * 10 / 4 NOTE: When in doubt about how to write an expression, use extra parenthesis Copyright © 2002 W. A. Tucker


Download ppt "Bill Tucker Austin Community College COSC 1315"

Similar presentations


Ads by Google