meljun cortes c programming

23
INTRODUCTION TO C++ PROGRAMMING

Upload: meljun-cortes

Post on 13-Jul-2015

86 views

Category:

Technology


1 download

TRANSCRIPT

INTRODUCTION TO C++ PROGRAMMING

OBJECTIVES

1. Understand the basic components of a C++ program. 2. Classify identifiers, keywords, constants, and variables. 3. Enumerate and apply the basic data types of C++. 4. Identify the different operators to be used. 5. Know the precedence of operators.

A Simple C++ Program

/* This is a simple C++ program *//* declaration of header file */#include <iostream.h> /* start of main program */void main(void){

cout << "Welcome to C++!!!\n";}

Basic Components of a C++ Code

HEADER FILES Functions Statements The { } Symbols The /* */ symbols

Identifiers, Keywords, Constants, and Variables

IDENTIFIERS (NAMING CONVENTIONS) - Names are made up of letters and digits.

- The first character must be a letter or an underscore (_). However, an underscore is not recommended to be used as the first character in a name since some of C++’s internal identifiers begin with underscores.

- C++ is case-sensitive, i.e., the lower-case letter ‘a’ is not the same as the uppercase letter ‘A’. - At least the first 31 characters of a name are significant.

Identifiers, Keywords, Constants, and VariablesKEYWORDS

Identifiers, Keywords, Constants, and Variables

CONSTANTS Constants are entities whose value does not

change. A constant can either be numeric constant or a

literal constant. In C/C++, a numeric constant can be an integer or floating point number, while a literal constant can be a single character or a string, i.e. a constant with more than one character.

A single character is written such that it is enclosed in a pair of single quotes.

A string is written enclosed in a pair of double quotes

Identifiers, Keywords, Constants, and Variables

VARIABLES A program is made of data and instructions to

manipulate those data. Note that data have to be stored somewhere, and

thus will need some memory space in the Random Access Memory (RAM).

In a C++ program, a variable is the entity that holds data.

A variable, as the name suggests, is a varying entity depending on the actual data it holds. Without variables, it would be impossible to store data.

Identifiers, Keywords, Constants, and Variables

A variable has the following characteristics: 1. a symbolic name;2. an associated physical memory space (portion

in a RAM)3. a data type4. a value that depends on the data type5. a scope6. a lifetime

Basic Data Types in C++

Data Type specifies the kind of values can be

assumed by a variable of that type the range of values that can be assumed

by a variable of that type and the amount of memory (in bytes)

needed by a variable to store a value of that type.

Basic Data Types in C++Five basic data types in C++

char – the character data type. The char data type is used to represent/store/manipulate character data values.

int – the integer data type. The int data type is used to represent/store/manipulate signed whole numbers. The range of values that can be assumed by an int value is from -2147483648 to 2147483647.

f loat – the single precision floating point data type. The float data type is used to store single precision signed real numbers. The appropriate range of values that can be assumed by a float value is from 3.4 X 10 -38 to 3.4 X 1038.

double – the double precision floating point data type. The double data type is used to store double precision signed real numbers. The appropriate range of values that can be assumed by a double value is from 1.7 X 10-308 to 1.7 X 10308.

bool – the Boolean data type. The bool data type is used to represent Boolean values true or false.

Variable Declaration A variable declaration is an “action” by which a

variable is “introduced” to a program/function. All variables in a C++ program must be declared.

If you forgot to do so, the compiler will report a syntax error.

The syntax for declaring a variable is as follows:<data type> <variable name>;

A semicolon signifies the end of a declaration.

Examples: char ch; bool b; int i; float f; double d;

More Examples: char ch1, ch2; bool b1, b2; int x, y, z; float degree_celsius, degree_fahrenheit, degree_kelvin; double numerator, denominator;

Operators Operators are symbols representing operations

that can be performed on constants and variables. There are four basic operations available in C++ language.1. Assignment Operation2. Arithmetic Operation3. Relational Operation4. Logical Operation

The assignment operator is denoted by the equal symbol (=). It is used to store (i.e., assign) a value to a variable. The syntax of an assignment operation is:

<variable name> = <expression>;In the syntax above, variable name can be any valid identifier in C++, while expression can be a constant, variable or a valid expression.

THE ASSIGNMENT OPERATOR The assignment operation is also a statement,

thus a semicolon should terminate it. ch1 = ‘Z’; ch2 = ch1; x = 5; y = x;

If in some cases we assign a value whose data type is different from the data type of the receiving variable, the data type of the value will be converted to the data type of the variable (either demoted or promoted).

THE ARITHMETIC OPERATORS

There are some important notes to remember in the use of C++ operators:

Assigning a variable to a constant is syntax error. For example:

12345 = x; Assigning the value of a variable to a variable of the same

name is syntactically correct, but practically useless and does not make any sense. For example:

a = 12345;a = a; // a get the value of a, which is 12345

It is usual in programming to assign a value of an expression to a variable, wherein the old value of the variable is also used in the expression. For example:

a = 50;a = a + 1;

The statement a = 50; means the value 50 is assigned to a. The next statement adds 1 to this value resulting to 51, which is then assigned as the new value of a.

THE RELATIONAL/COMPARISON

OPERATORS

THE RELATIONAL OPERATORS (cont.)

The relational operators can be performed on all the basic data types. In C++, the result of a relational operation is either a 0 (zero) or a 1 (one). A zero means that the relation is FALSE and a one means that it is TRUE. All the relational/comparison operators are binary operators.It is important to remember that the test for equality uses two equal symbols. Forgetting one of the equal sign is a very common logical error.

THE LOGICAL OPERATORS

The logical operators are normally used in conjunction with relational operators to test for multiple conditions. The logical NOT is a unary operator, it is used with only one operand to its right. The remaining operators are binary operators. The logical NOT operation is performed before logical AND, before logical OR.

THE PRECEDENCE OF OPERATORS