Showing posts with label Unit 4. Show all posts
Showing posts with label Unit 4. Show all posts

Unit 4 - The Expression

Posted on Friday, May 7, 2010 by B[H] | 0 comments
Labels:

Expression in C++ are formed by properly combining operators, variables and constants. We have already seen some examples of simple arithmetic and logical expression. As algebra, C++ expression can be complex. Parentheses can be used to force the order of evaluation. An expression may also contain spaces for readability.
Examples of expressions.

Gross_pay – deductions
(basic_pay + hours * rate) – (socso + premium + loan)
(b * b – 4 * a * c) > 0
(gender = = ‘m’) && (age > 20)
(gender = = ‘m’ || gender = = ‘f’ ) && age >= 21

Unit 4 - The Operators

Posted on by B[H] | 0 comments
Labels:

C++ is very rich in built-in operators. Operators trigger some computations when applied to operands in an expression. There are several general classes of operators. But for now, we will present just the arithmetic, relational, logical and assignment operators.

a-Arithmetic Operators

There are seven arithmetic operators in C++:-

Photobucket

Examples of operation involving integer variables. With a=7 and b=2, the expression on the values on the right.

Photobucket

Note: You can assign a positive or negative number to a variable by using a unary +

or - . Example:
a = -20 // assigns ‘a’ a negative 20
b = +20 // assigns ‘b’ a positive 20 ( + sign normally not needed)

Unary operators operate on single value. Thus the size of operator is a unary operator. So are the decrement and increment operator.



b-Relational Operators

There are six relational operators and three logical operators in C++:-

Photobucket

The result of evaluation of a relational operation is either true(represented by 1) or false (represented by 0). For example, if a = 7 and b = 5, then a



c-Logical Operators

There are three logical operators in C++:

Photobucket

*Hierarchy of Operators
The hierarchy of operator precedence form highest to lowest is summarized below:

Photobucket

Unit 4 - Declaring Variables

Posted on by B[H] | 0 comments
Labels:

All variables in a program must be declared prior to their use. The declaration takes the form:

Photobucket

Examples of variable declarations.

Int x, y, z;
Short small_number;
Long big_number;
Unsigned positive_number;
Char ch;
Float amount, rate;

Variables may be initialized by assigning constants to them either at the time of their declaration or at a later time.


Example of variable initialization and declaration

int m, n=10;
float rate, total = 0.0;
char response= ’n’;
char color{6}=’green’;

Unit 4 - Declaring Constant

Posted on by B[H] | 0 comments
Labels:

Constant are values that do not change during program execution. They can be of type integer, character or floating point. To declare a constant, use the keyword const as in the example:-

Photobucket

a-Integer constant

Integer constant can be further categorized into four types-short integer, integer,unsigned integer and long integer.
Examples of integer constants

• Short integer constant:
-99 -35 0 45 320

• Integer constants:
9999 -1 0 555 32767

• Unsigned integer constants:
1 256 1000 60000

• Long integer constants:
222 0 33333 9999999

A constant can also be identified as being long by tagging a l or a L to the end of the number, For example , the constants 212L and 323l. will be viewed by C++ as long constants.



b-Character and string Constants

A character constant is any character enclosed between two single quotation marks (‘ and ‘ ). When the several characters are enclosed between two double quotation marks (‘ and ‘ ), it is called a string. Examples of character and string constants;

i. Character constants:
‘$’ ‘*’ ‘ ‘ ‘z’ ‘G’

ii. String constants:
“Name:” “Telephone No:” “Postcode:”

iii. Floating Point Constants
A floating point constant is a decimal number that contains the decimal point (.) or the exponent ( e or E ) or both. Here are some examples of floating point constants.
5.0 0.005 2000.0 987.123 5e-3 0.01e-2 0.2345E8 1.23E4

Unit 4 - Floating Point data types - float and double

Posted on by B[H] | 0 comments
Labels:

This type can store floating point in computer memory.

Syntax : double name_identifier
Example: double pendapatan_bersih,cukai_pendapatan;
double nilai;

Memory storage that use for variable pendapatan_bersih be able to store floating point data. But int and char data types only can be stored according to computer types. The three floating points data in C++ such as;

1. float
2. double
3. long double


The comparison of the three types are range value and digits.

Float : 1.175494e-38 to 3.402823e+38 (6 place decimal )
Double : 2.225074e308 to 1.797693e+308 (15 place decimal)
Long double : 3.362103e-4932 to 1.189731e+4932 (19 place dec.)

Unit - Character data types ( char )

Posted on by B[H] | 0 comments
Labels:

Storage word char is use to declare variable character.

Syantax char name_identifier
example: char huruf;
char lagi;


This declaration means that variable lagi and huruf will given a memory storage for character data type. Character variable store printed data or un printed data in character set of computer includes;
• small and capital letters
• decimal digits (0-9)
• special character

This character represent in 1 byte (8 bit) computer memory. Internal representative for character value decided by encoder character (ASCII or EBCDIC) that is used in the computer.

Unit 4 - Integer data types ( int )

Posted on by B[H] | 0 comments
Labels:

Key words int is use to declare variable integer number

Syntax int name_identifier
example : int bilangan ;
int nom1,nom2;

This declaration means that the compiler was asked to hold integer data name bilangan, nom1 and nom 2. Integer data types is classified as sign (+) and unsigned (-).For all the computer system, data integer is refer to 0 and 1 bits. There are three types of integer short int, int, long int. The comparison of this integer range in value.

Short int : -32768 to 32767
Int : -32768 to 32767
Long int : -2147483648 to 2147483648
Unsigned short int: 65535
Unsigned int: 65535
Unsigned long int : 4294967295

Unit - Basic Data Types

Posted on by B[H] | 0 comments
Labels:

There are four main data types in programming C++ :-

i. int
ii. char
iii. double
iv. float

For additional of basic data types programming C++ can support another built-in data types such as short, long, signed and unsigned. The various data types, the number of bits required to store them.

Photobucket

Unit 4 - INTRODUCTION TO DATA TYPES, OPERATOR AND EXPRESSION IN C++

Posted on by B[H] | 0 comments
Labels:

The C++ character set includes the keyboard characters, namely the uppercase letter A - Z, the lower letters a – z , the digits 0 – 9 , and other special characters (such as!, #, [, ] , ,&,<,?, and * ). The blank space is also a character in the set.
Identifiers are simply references to memory location which can hold values. They are formed by combining letters (both upper and lowercase), digits and the underscore( _ ). The first character of an identifier, however, must be a letter. The blank character is not permitted in an identifier. Although identifiers can be formed by freely combining the letters, digits and underscores, common sense tells us that
we should give them suggestive names, that is, names that reflect the data items that they are going to store. Identifiers can be of any length although in practice they seldom exceed 25 characters.

Example of valid and invalid identifiers:

Photobucket

C++ identifiers are case-sensitive, meaning, lower and uppercase letter in identifiers are treated as different characters. This means we cannot freely mix the lower and uppercase letter to refer to the same identifier. For example, identifiers HOURLY_RATE, Hourly_Rate, Hourly_rate and hourly_rate are all different, they refer to different memory locations.
Variables are identifiers whose value may change during the course of execution of a program.
Keywords in C++, also called reserved words, have standard, predefined meanings and must be used only for their intended purpose. Programmers therefore should not use these keywords as identifiers.

Unit 4 - Data Types, Operator And Expression

Posted on by B[H] | 0 comments
Labels:

General Objective : To Introduce and Use the data types, operator and
expression in C++ Programming.


Specific Objectives : At the end of the unit you will be able to :

- describe the essential data type : int,char,float and double
- declare constant and variable
- able to use constant and variable
- explain the arithmetic operator types
- explain the arithmetic, relational and logical operator types
- able to used operators to construct expression