40
Programming Fundamentals

Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Embed Size (px)

Citation preview

Page 1: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

ProgrammingFundamentals

Page 2: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Summary of previous lectures

Programming Language Phases of C++ Environment Variables and Data Types

Page 3: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Today’s Lecture

Manipulators Char type Floating-Point Types Input with cin

Page 4: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Manipulators

Manipulators are instructions to the output stream that modify the output in various ways.

Page 5: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

The endl Manipulator

This causes a linefeed to be inserted into the stream, so that subsequent text is displayed on the next line.

It has the same effect as sending the ‘\n’ character.

Its an example of a manipulator.

Page 6: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types
Page 7: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Other Integer Types

There are several numerical integer types besides type int.

The two most common types are long and short. Type char is an integer type as well.

Page 8: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Character Variables

Type char stores integers that range in value from –128 to 127.

Variables of this type occupy only 1 byte (eight bits) of memory.

Character variables are sometimes used to store numbers that confine themselves to this limited range, but they are much more commonly used to store ASCII characters.

Page 9: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

ASCII characters

Page 10: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Character Variables

The ASCII character set is a way of representing characters such as ‘a’, ‘B’, ‘$’, ‘3’, and so on, as numbers.

These numbers range from 0 to 127.

Page 11: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Character Constants

Character constants use single quotation marks around a character, like ‘a’ and ‘b’.

Note: This differ from string constants, which use double quotation marks.

When the C++ compiler encounters such a character constant, it translates it into the corresponding ASCII code. The constant ‘a’ appearing in a program, for example, will be translated into 97

Page 12: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Character variables can be assigned character constants as values.

Character Constant

Horizontal Tab

Character Constant

Escape Sequence for new line, alternative to endl manipulator

Page 13: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Output

Page 14: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Example

ASCII code for ‘A’

ASCII code for horizontal tab

ASCII code for ‘B’

No endl or ‘\n’ in source code so cursor blinking after last displayed charcter

Page 15: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Initialization

Variables can be initialized at the same time they are defined. In this program two variables of type char—charvar1 and charvar2—are initialized to the character constants ‘A’ and ‘\t’.

Page 16: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Escape Sequences

Page 17: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Escape Sequences

Since the backslash, the single quotation marks, and the double quotation marks all have specialized meanings when used in constants, they must be represented by escape sequences when we want to display them as characters.

Page 18: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Example

Here’s an example of a quoted phrase in a string constant:

Page 19: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Floating Point Types

Floating-point variables represent numbers with a decimal place—like 3.1415927, 0.0000625,and –10.2.

They have both an integer part, to the left of the decimal point, and a fractional part, to the right.

Page 20: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Kinds of floating-point variables

There are three kinds of floating-point variables in C++: type float, type double, and type long double.

Page 21: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Type float

Type float stores numbers in the range of about 3.4x10^–38 to 3.4x10^38.

It occupies 4 bytes (32 bits) in memory.

Page 22: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Example

Constant variable

Input with

cin

Page 23: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Here’s a sample interaction with the program:

Page 24: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Type double and long double

The larger floating point types, double and long double, are similar to float except that they require more memory space and provide a wider range of values and more precision.

Type double requires 8 bytes of storage and handles numbers in the range from 1.7x10^–308 to 1.7x10^308. Type long double is compiler-dependent but is often the same as double.

Page 25: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Floating-Point Constants

The number 3.14159F is an example of a floating-point constant.

The decimal point signals that it is a floating-point constant, and not an integer, and the F specifies that it’s type float, rather than double or long double.

The number is written in normal decimal notation. You don’t need a suffix letter with constants of type double; it’s the default. With type long double, use the letter L.

Page 26: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Contdd . . . .

You can also write floating-point constants using exponential notation.

Exponential notation is a way of writing large numbers without having to write out a lot of zeros.

For example,1,000,000,000 can be written as 1.0E9 in exponential notation.

Page 27: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

The const Qualifier

The keyword const (for constant) precedes the data type of a variable.

It specifies that the value of a variable will not change throughout the program.

Any attempt to alter the value of a variable defined with this qualifier will elicit an error message from the compiler.

Page 28: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Example

Compile-Time Error

Page 29: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

The #define Directive

Constants can also be specified using the preprocessor directive #define.

Syntax is

# define identifier replacement-text For example, the line

appearing at the beginning of your program specifies that the identifier PI will be replaced by the text 3.14159 throughout the program.

Page 30: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

The statement cin >> rad causes the program to wait for the user to type in a number.

The resulting number is placed in the variable rad.

Input with cin

Page 31: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Input with cin

The keyword cin (pronounced “C in”) is an object, predefined in C++ to correspond to the standard input stream.

This stream represents data coming from the keyboard.

The >> is the extraction or get from operator. It takes the value from the stream object on its left

and places it in the variable on its right.

Page 32: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types
Page 33: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Variables Defined at Point of Use

Note that in this example variables are defined at point of use (e.g.; area).

You can define variables throughout a program, not just at the beginning.

Page 34: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Cascading <<

The program first sends the phrase Area is: to cout, then it sends the value of area, and finally the endl manipulator.

The extraction operator >> can be cascaded with cin in the same way, allowing the user to enter a series of values.

Page 35: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Example: Input with cin Example1

Page 36: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

OUTPUT

Page 37: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

Cascading Example

Page 38: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

OUTPUT

Two numbers separated by space

Page 39: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

OUTPUT

Two numbers separated by line

Page 40: Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types

QUESTIONS????