Lecture3

Uploaded from authorPOINTLite
Views:
 
Category: Entertainment
     
 

Presentation Description

No description available.

Comments

Presentation Transcript

Techniques of Programming CSCI 131, Fall 2004 Lecture 3 Arithmetic Expressions : 

Techniques of Programming CSCI 131, Fall 2004 Lecture 3 Arithmetic Expressions

Program comments: 

Program comments /*Hello World Program CSCI 131*/ #include <iostream.h> //Include I/O library int main (void) { cout << "Hello World!"; //Send to output return 0; } Program comments Comments are not translated by the compiler. They provide information to the programmer.

What does a variable declaration do?: 

What does a variable declaration do? A declaration tells the compiler to allocate enough memory to hold a value of this data type, and to associate the identifier with this location. int age_Of_Dog; float taxRate98; char middleInitial; 4 bytes for TaxRate98 1 byte for MiddleInitial

Giving a value to a variable: 

Giving a value to a variable In your program you can assign (give) a value to the variable by using the assignment operator = age_Of_Dog = 12; taxRate98 = 0.595; or by another method, such as cout << “How old is your dog?”; cin >> age_Of_Dog;

What is a Named Constant?: 

What is a Named Constant? A named constant may be a location in memory which we can refer to by an identifier, and in which a data value that cannot be changed is stored. VALID CONSTANT DECLARATIONS const char STAR = ‘*’ ; const float NORMAL_TEMP = 98.6 ; const float PI = 3.14159 ; const float TAX_RATE = 0.0725 ; const int MAX_SIZE = 50 ;

Naming Conventions: 

Naming Conventions Some standard conventions for naming variables and constants: Variables are given names that begin with a lower case letter: myName, count, firstPlace Programmer defined functions and datatypes begin with upper case letter: DealCards( ), WriteInfo( ), MyDataType Constants are given names in all upper case letters: MAX_WIDTH, PIXELS_PER_CM

Program Example: 

Program Example /*DogYears Program *Computes my age in dog years*/ #include <iostream.h> //Include I/O library const float DOG_YEAR_FACTOR = 7.0; int main (void) { float myAge = 29; float ageInDogYears; ageInDogYears = myAge * DOG_YEAR_FACTOR; cout << "You are " << ageInDogYears << " years old!" << endl; return 0; }

Using cout: 

Using cout cout refers to the output stream to standard output (usually the monitor). Text in quotes is printed out literally (This is a "string"). cout << "You are " Text not in quotes is an identifier (a variable or constant). The value of the variable (or constant) is printed: cout << ageInDogYears endl indicates the end of a line. Printout for the next item printed will start on the next line after endl is encountered.

Is Compilation the first step?: 

Is Compilation the first step? No. Before your source program is compiled, it is first examined by the preprocessor to Remove all comments from source code Handle all preprocessor directives. They begin with the # character such as #include <iostream.h> Tells preprocessor to look in the standard include directory for the header file called iostream.h and insert its contents into your source code.

Using Libraries: 

Using Libraries A library has 2 parts Interface (stored in a header file) tells what items are in the library and how to use them. Implementation (stored in another file) contains the definitions of the items in the library. #include <iostream.h> Refers to the header file for the iostream library needed for use of cin and cout.

Another Method for using libraries: 

Another Method for using libraries Newer versions of C++ allow a different method for using standard libraries: #include <iostream> using namespace std; The namespace, std, contains the definitions of cout, cin and other objects and variables used by the standard C++ libraries.

What is an Expression?: 

What is an Expression? An expression is any valid combination of operators and operands. In C++ each expression has a value. Consider the expression: myAge * DOG_YEAR_FACTOR We evaluate expressions to find their value: myAge 29.0 DOG_YEAR_FACTOR 7.0 29.0 * 7.0 203.0

Basic Arithmetic Operators: 

Basic Arithmetic Operators C++ uses the standard arithmetic operators: + - * and / These generally behave as you would expect: 4 + 3 7. However, division can be tricky...

The division operator: 

The division operator The result of the division operator depends on the type of its operands. If one or both operands has a floating point type, the result is a floating point type. Otherwise, the result is an integer type. Examples 11 / 4 has value 2 11.0 / 4.0 has value 2.75 11 / 4.0 has value 2.75

The Modulus operator: 

The Modulus operator The modulus operator % can only be used with integer type operands and always has an integer type result. Its result is the integer type remainder of an integer division. EXAMPLE 11 % 4 has value 3 because ) 4 11 R = ?

Unary Operators: 

Unary Operators Unary operators have on one operand. The unary operators, + and -, give a positive or negative value to their operand, e.g. -3.

More C++ operators: 

More C++ operators 8 int age; age = 8; age = age + 1; age 9 age

PREFIX FORM Increment operator: 

PREFIX FORM Increment operator 8 int age; age = 8; ++age; age 9 age

POSTFIX FORM Increment operator: 

POSTFIX FORM Increment operator 8 int age; age = 8; age++; age 9 age

Decrement operator: 

Decrement operator 100 int dogs; dogs = 100; dogs--; dogs 99 dogs

Which form to use?: 

Which form to use? When the increment (or decrement) operator is used in a “stand alone” statement solely to add one (or subtract one) from a variable’s value, it can be used in either prefix or postfix form. dogs-- ; --dogs ; USE EITHER

But...: 

But... When the increment (or decrement) operator is used in a statement with other operators, the prefix and postfix forms can yield different results. WE’LL SEE HOW LATER . . .

Insertion Operator: 

Insertion Operator Variable cout is predefined to denote an output stream that goes to the standard output device (display screen). The insertion operator << called “put to” takes 2 operands. The left operand is a stream expression, such as cout. The right operand is an expression of simple type or a string constant.

Output Statements: 

Output Statements SYNTAX These examples yield the same output. cout << “The answer is “ ; cout << 3 * 4 ; cout << “The answer is “ << 3 * 4 ; cout << ExprOrString << ExprOrString . . . ;