programming fundamentals. overview of previous lecture phases of c++ environment program statement...

56
Programming Fundamentals

Upload: harry-patterson

Post on 18-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

ProgrammingFundamentals

Page 2: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Overview of Previous Lecture

Phases of C++ Environment Program statement Vs Preprocessor

directive Whitespaces Comments

Page 3: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Today’s Lecture

CPU and Memory Variable Types Integer Variables Character Variables Floating Point Types Input with cin

Page 4: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

CPU and Memory

A computer acts on data according to the instructions specified in a program (like our previous C++ program) and produces the output.

This computation is done by Central Processing Unit(CPU) of the computer.

A CPU of computer performs arithmetic operations such as addition, subtraction, multiplication and division and also performs logical operation on the data such as comparing two numbers.

Page 5: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

CPU and Memory

Memory of a computer is the area where data and programs are stored.

A computer memory is made up of cells that is capable of holding information in the form of binary digits 0 and 1 (bits).

This is the way information is stored in a memory just like we memorize numbers using 0 to 9.

Page 6: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

CPU and Memory

Collection of 8 bits is called a byte. Normally the CPU does not accessed memory by

individual bits. Instead, it accesses data in a collection of bits,

typically 8 bits, 16 bit, 32 bit or 64 bit. The amount if bits on which it can operate

simultaneously is known as the word length of the computer.

Page 7: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

CPU and Memory

When we say that Pentium 4 is a 32 bit machine, it means that it simultaneously operates on 32 bit of data.

Page 8: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Program memory

When the program is compiled and linked, different parts of the program is organized in separate segments.

Our code will be in one segment. Code means the instructions to be executed That segment is called as code segment or

program memory.

Page 9: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Data segment and Stack memory

Then there is data on which the code operates, this data get stored in a segment called Data segment.

Stack memory is a part of programs memory which will be used in case of function calls

Page 10: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Program Memory

The three types of memory specified above are owned by the corresponding process or program.

The linker will give information about where to store which data to the loader

Based on these information loader will load the corresponding executable(.exe in our case)in the memory.

Page 11: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Heap memory

Heap memory is the memory which is owned by the process when a process requests for extra memory.

These requests are done by the malloc and calloc function calls.

Page 12: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Program vs Process

A program needs memory when it gets executed and a program under execution is a process.

Page 13: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

CPU and Memory

Data is stored in the memory at physical memory locations.

These locations are known as the memory address. 

It is very difficult for humans to remember the memory addresses in numbers like 46735, so instead we name a memory address by a variable.

Page 14: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Variables

Variables are the most fundamental part of any language.

A variable has a symbolic name and can be given a variety of values.

Variables are located in particular places in the computer’s memory.

When a variable is given a value, that value is actually placed in the memory space assigned to the variable.

Page 15: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Variable types

Most popular languages use the same general variable types, such as integers, floating-point numbers, and characters

Page 16: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Integer variables

Integer variables represent integer numbers like 1, 30,000, and –27.

Such numbers are used for counting discrete numbers of objects, like 11 pencils or 99 bottles of beer.

Unlike floating point numbers, integers have no fractional part; you can express the idea of four using integers, but not four and one-half.

Page 17: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Contdd . . . .

The amount of memory occupied by the integer types is system dependent.

On a 32-bit system such as Windows, an int occupies 4 bytes (which is 32 bits) of memory.

This allows an int to hold numbers in the range from –2,147,483,648 to 2,147,483,647.

Page 18: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

OUTPUT

Page 19: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Compile time error

Page 20: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Defining Integer Variables

Page 21: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Here’s a program that defines and uses several variables of type int:

Page 22: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Explanation

The statements

int var1;

int var2;

define two integer variables, var1 and var2

• The keyword int signals the type of variable. These statements, which are called declarations,

must terminate with a semicolon, like other program statements.

Page 23: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Contdd . . . .

You must declare a variable before using it. However, you can place variable declarations

anywhere in a program.

Page 24: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Declarations and Definitions

A declaration introduces a variable’s name (such as var1) into a program and specifies its type (such as int).

However, if a declaration also sets aside memory for the variable, it is also called a definition.

Page 25: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Variable Names

The names given to variables (and other program features) are called identifiers.

What are the rules for writing identifiers? You can use upper- and lowercase letters, and

the digits from 1 to 9. You can also use the underscore (_). The first character must be a letter or underscore.

Page 26: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Contdd . . . .

Identifiers can be as long as you like, but most compilers will only recognize the first few hundred characters.

The compiler distinguishes between upper- and lowercase letters, so Var is not the same as var or VAR.

You can’t use a C++ keyword as a variable name.

Page 27: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Keyword

A keyword is a predefined word with a special meaning.

int, class, if, and while are examples of keywords.

Page 28: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Example program

Page 29: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Assignment Statements

The statements

assign values to the two variables. The equal sign (=) causes the value on the right

to be assigned to the variable on the left.

Page 30: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Integer Constants

The number 20 is an integer constant. Constants don’t change during the course of the

program. An integer constant consists of numerical digits. There must be no decimal point in an integer

constant, and it must lie within the range of integers.

Page 31: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Example program

Page 32: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Output Variations

The statement

displays a string constant. The next statement

displays the value of the variable var2.

OUTPUT???

Page 33: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

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 34: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Manipulators

Manipulators are instructions to the output stream that modify the output in various ways; we’ll see more of them as we go along.

Page 35: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

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 36: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

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 37: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments
Page 38: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

As you may already know, 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 39: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

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 40: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Character variables can be assigned character constants as values.

Page 41: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

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 42: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Escape Sequences

Page 43: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

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 44: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Example

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

Page 45: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

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 46: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Kinds of floating-point variables

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

Page 47: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Type float

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

It occupies 4 bytes (32 bits) in memory.

Page 48: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments
Page 49: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Here’s a sample interaction with the program:

Page 50: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

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.7x10308. Type long double is compiler-dependent but is often the same as double.

Page 51: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

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 52: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

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 53: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

Input with cin

Page 54: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

The statement

causes the program to wait for the user to type in a number.

The resulting number is placed in the variable ftemp.

Input with cin

Page 55: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments

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 56: Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments