DFC 1023 PROGRAMMING
PRINCIPLES
CHAPTER 3: FUNDAMENTALS OF
PROGRAMMING LANGUAGE
WHAT IS DATA?
• Data is a collection of numbers, characters,
alphabets, and special symbols etc.
• It can be processed to produce some
meaningful information.
• It also can be modified by the computer into
useful forms for human beings.
Data is divided into:
Numeric
Integer
Float
Non Numeric
Character
String
Boolean
TYPES OF DATA
Numeric Non Numeric
Contains all types of numbers.
Data which can be used for calculation.
Contains all types of non-numbers.
INTEGER
• All positive and negative numbers including zero
and no decimal place. Example: 0, +1, -10.
• Integers are used to represent the counting of
things.
• Example: Numbers of month in a year (1,2,3…)
CHARACTER
• Consists of all letters, numbers and special
symbols.
• Characters are surrounded by single quotation
mark (‘ ‘).
• Example: ‘A’, ‘m’,’=’, ‘#’, ‘1’ or ‘ ‘.
FLOAT
• Contains all real numbers.
• The number will be stored in floating point.
• Used for metric measurement, temperature and
price.
• Example: 1.0, 234.55, 20.30, 36.7.
STRING
• A combination of one or more characters.
• A string is surrounded by double quotation marks
(“ “).
• Example: (“WELCOME TO COSMOPOINT”) or
(“8758”).
BOOLEAN (LOGICAL VALUE)
• Used in making yes-or-no decisions (T or F).
• Example: To check 2 integers using If…Else control
structure.
IDENTIFIER, VARIABLE AND CONSTANT
• IDENTIFIER is words used to represent certain program entities
(variables, function names, etc).
• KEYWORDS (reserved words) have standard, predefined meanings
and must be used only for their intended purpose. It cannot be
used as an identifier.
• VARIABLE is identifier whose value can change during the course
of execution of a program
• CONSTANTS are values that do not change during program
execution. They can be any type of integer, character or floating-
point.
IDENTIFIER
• Words used to represent certain program entities
(variables, function names, etc).
• Example:
int my_name;
my_name is an identifier used as a program variable
void CalculateTotal(int value)
CalculateTotal is an identifier used as a function name
FP101Programming Fundamentals with C++
VARIABLE
Whats?: Is a Location in memory which:
1. We can refer to by an identifier.
2. Data value that can be change
3. Where value can be stored
Common data types (fundamental, primitive or built-in)
1. int – integer numbers : 1, 2, 4,….
2. char – characters : ‘a’, ‘c’, …
3. float, double: floating point numbers: 2.5, 4.96
The value of a variable could be changed while the program is running.
declaring a variable means specifying both its name and its data type.
EXAMPLE OF VARIABLES
 int is an abbreviation for integer.
 Integer store value: 3, 102, 3211, -456, etc.
 Example variable : number_of_bars
 double represents numbers with a fractional
component.
 double store value: 1.34, 4.0, -345.6, etc.
 Example variable : one_weight ,total_weight
CONSTANT
• Entities that appear in the program code as fixed values.
• Any attempt to modify a CONSTANT will result in error.
• Declared using the const qualifier.
• Also called named constants or read-only variables.
• Must be initialized with a constant expression when they
are declared and cannot be modified thereafter
• Example: const int size = 5;
NAMING CONVENTION RULES FOR
IDENTIFIER
• They are formed by combining letters, digits & underscores.
• Blank space is not allowed in an identifier.
• The first character of an identifier must be a letter.
valid invalid reason
Monthly_Salary Monthly Salary Blank space cannot be
used
Month1 1stMonth Digit cannot be used
as a first character
Email_add email@ Special characters
cannot be used
Naming convention rules for identifier
Rules Example
Can contain a mix of character and numbers. However it cannot
start with a number
H2o
First character must be a letter or underscore Number1,
_area
Can be of mixed cases including underscore character XsquAre
my_num
Cannot contain any arithmetic operators R*S+T
… or any other punctuation marks #@x%!!
Cannot be a C keyword/reserved word struct; cout;
Cannot contain a space My height
… identifiers are case sensitive Tax != tax
NAMING CONVENTION RULES FOR
IDENTIFIER
Valid Invalid Comment
X “x” Illegal character
Gross_Pay Gross pay Illegal blank
Hourly_rate Hourly-rate Illegal character -
name name@ Illegal character @
sumx2 2sumx Illegal 1st character
BASIC DATA TYPES
Keywords (also called reserved words)
Are used by the C++ language.
Must be used as they are defined in the programming
language.
Cannot be used as identifiers.
There are 5 basic data types :
i. int iv. char
ii. Float v. string
iii. double
BASIC DATA TYPE
DATA TYPE USES EXAMPLE CODE
int int is used to define integer
numbers.
int Count;
Count = 5;
float float is used to define floating
point numbers.
float Miles;
Miles = 5.6;
double A double is used to define BIG
floating point numbers. It
reserves twice the storage for the
number.
double Atoms;
Atoms= 2500000;
char A char defines characters. char Letter;
Letter = 'x';
BASIC DATA TYPES
a) int
 used to declare numeric program variables of integer type
 whole numbers, positive and negative
 Syntax:
 keyword: int
 Example:
int Bilangan;
int Num1, num2;
 Example code :
 Sample values:
4578 -4578 0
int number;
number = 12;
int name_identifier;
BASIC DATA TYPES
b) float
 fractional parts, positive and negative
 real numbers with a decimal point.
 3 type of floting point :
1. float (6 place decimal)
2. double (15 place decimal)
3. long double (19 place decimal)
 Syntax:
 Example:
 keyword: float
 Example declaring and initialing code:
 sample values
95.274 95. .265
float height;
height = 1.72;
double name_identifier;
double net_price;
BASIC DATA TYPES
c) char
 equivalent to ‘letters’ in English language
 Example of characters:
 Numeric digits: 0 - 9
 Lowercase/uppercase letters: a - z and A - Z
 Space (blank)
 Special characters: , . ; ? “ / ( ) [ ] { } * & % ^ < > etc
 single character
 Syntax:
 keyword: char
 Example code:
 sample values
‘B’ ‘d’ ‘4’ ‘?’ ‘*’
The
declared
character
must be
enclosed
within a
single
quote!
char gred;
gred = ‘A';
char name_identifier;
BASIC DATA TYPES
d) double
 used to declare floating point variable of higher precision or higher range of
numbers
 exponential numbers, positive and negative
 keyword: double
 Code example:
double valuebig;
valuebig = 12E-3;
BASIC DATA TYPE
e) String
a string is a sequence of characters enclosed in double
quotes.
string sample values:
“Hello” “Year 2000” “1234”
the empty string (null string) contains no characters and is
written as “ ”.
To declare a variable of type string:
string greeting=“Hello”;
BASIC DATA TYPES
f) Bool (Short for boolean)
 bool is a new addition to C++.
 Boolean values are either true or false.
 To declare a variable of type bool:
bool old_enough;
VARIABLES
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 ageOfDog;
float taxRateY2K;
char middleInitial;
4 bytes for taxRateY2K 1 byte for middleInitial
DECLARING VARIABLES
Before use, variables must be declared.
Why : To tells the compiler the type of data to store
Declaration syntax:
Type_name Variable_1 ,Variable_2, ... ;
Declaration Examples:
 double average, m_score, total_score;
 double moon_distance;
 int age, num_students;
 int cars_waiting;
TWO LOCATIONS FOR VARIABLE DECLARATIONS.
I M M E D I AT E LY P R I O R T O
U S E
main()
{
…
int sum;
sum = score1 + score 2;
…
}
AT T H E B E G I N N I N G
main()
{
int sum;
…
sum = score1 + score2;
…
}
Example of Declaring Variables
CONSTANT
A named constant is a location in memory that we
can refer to by an identifier, and in which a data
value that cannot be changed is stored.
VALID CONSTANT DECLARATIONS
const string STARS = “****” ;
const float NORMAL_TEMP = 98.6 ;
const char BLANK = ‘ ’ ;
const int VOTING_AGE = 18 ;
const float MAX_HOURS = 40.0 ;
DECLARING CONSTANT VARIABLES:
METHOD 1
Character constants
 A character enclosed in a single quotation mark
 Example:
Enumeration
 Values are given as a list
 Example:
const char letter = ‘n’;
const char number = ‘1’;
enum Language
{
Malay, English, Arabic
};
DECLARING CONSTANT VARIABLES:
METHOD 2
o You may also associate constant using #define
preprocessor directive.
o Example:
#include <iostream.h>
#define pi 3.412
void main(void)
{
double height, radius, base, volume;
cout <<“Enter the height and radius of the cone:”;
cin >>height >> radius;
base = pi * radius * radius;
volume = (1.0/3.0) * base * height;
cout <<“nThe volume of a cone is ” << volume;
}
INITIALIZING VARIABLES
Declaring a variable does not give it a value.
 Giving a variable its first value is initializing the variable.
Method 1: Variables are initialized in assignment statements
double mpg; // declare the variable
mpg = 26.3; // initialize the variable
Method 2: Declaration and initialization can be combined
using two methods:
 Method 1:
double mpg = 26.3, area = 0.0 , volume;
 Method 2:
double mpg(26.3), area(0.0), volume;
KEYWORDS
Keywords (also called reserved words)
 Reserved words in the C++ language for specific use.
 Must be used as they are defined in the programming language.
 Keywords that identify language entities such as statements, data types,
language attributes, etc.
 Have special meaning to the compiler, cannot be used as identifiers (variable,
function name).
 Should be typed in lowercase.
 Cannot be used as identifiers or variable names.
 Example: const, double, int, main, void,printf, while,
for, else (etc..)
C/C++ KEYWORDS
auto break case char const
continue default do double else
enum extern float for goto
if int long register return
short signed sizeof static struct
switch typedef union unsigned void
volatile while
C++ only keywords
asm bool catch class const_cast
delete dynamic_cast explicit false friend
inline mutable namespace new operator
private protected public reinterpret_cast
static_cast template this throw true
try typeid typename using virtual
wchar_t

POLITEKNIK MALAYSIA

  • 1.
    DFC 1023 PROGRAMMING PRINCIPLES CHAPTER3: FUNDAMENTALS OF PROGRAMMING LANGUAGE
  • 2.
    WHAT IS DATA? •Data is a collection of numbers, characters, alphabets, and special symbols etc. • It can be processed to produce some meaningful information. • It also can be modified by the computer into useful forms for human beings.
  • 3.
    Data is dividedinto: Numeric Integer Float Non Numeric Character String Boolean
  • 4.
    TYPES OF DATA NumericNon Numeric Contains all types of numbers. Data which can be used for calculation. Contains all types of non-numbers. INTEGER • All positive and negative numbers including zero and no decimal place. Example: 0, +1, -10. • Integers are used to represent the counting of things. • Example: Numbers of month in a year (1,2,3…) CHARACTER • Consists of all letters, numbers and special symbols. • Characters are surrounded by single quotation mark (‘ ‘). • Example: ‘A’, ‘m’,’=’, ‘#’, ‘1’ or ‘ ‘. FLOAT • Contains all real numbers. • The number will be stored in floating point. • Used for metric measurement, temperature and price. • Example: 1.0, 234.55, 20.30, 36.7. STRING • A combination of one or more characters. • A string is surrounded by double quotation marks (“ “). • Example: (“WELCOME TO COSMOPOINT”) or (“8758”). BOOLEAN (LOGICAL VALUE) • Used in making yes-or-no decisions (T or F). • Example: To check 2 integers using If…Else control structure.
  • 5.
    IDENTIFIER, VARIABLE ANDCONSTANT • IDENTIFIER is words used to represent certain program entities (variables, function names, etc). • KEYWORDS (reserved words) have standard, predefined meanings and must be used only for their intended purpose. It cannot be used as an identifier. • VARIABLE is identifier whose value can change during the course of execution of a program • CONSTANTS are values that do not change during program execution. They can be any type of integer, character or floating- point.
  • 6.
    IDENTIFIER • Words usedto represent certain program entities (variables, function names, etc). • Example: int my_name; my_name is an identifier used as a program variable void CalculateTotal(int value) CalculateTotal is an identifier used as a function name FP101Programming Fundamentals with C++
  • 7.
    VARIABLE Whats?: Is aLocation in memory which: 1. We can refer to by an identifier. 2. Data value that can be change 3. Where value can be stored Common data types (fundamental, primitive or built-in) 1. int – integer numbers : 1, 2, 4,…. 2. char – characters : ‘a’, ‘c’, … 3. float, double: floating point numbers: 2.5, 4.96 The value of a variable could be changed while the program is running. declaring a variable means specifying both its name and its data type.
  • 8.
    EXAMPLE OF VARIABLES int is an abbreviation for integer.  Integer store value: 3, 102, 3211, -456, etc.  Example variable : number_of_bars  double represents numbers with a fractional component.  double store value: 1.34, 4.0, -345.6, etc.  Example variable : one_weight ,total_weight
  • 9.
    CONSTANT • Entities thatappear in the program code as fixed values. • Any attempt to modify a CONSTANT will result in error. • Declared using the const qualifier. • Also called named constants or read-only variables. • Must be initialized with a constant expression when they are declared and cannot be modified thereafter • Example: const int size = 5;
  • 10.
    NAMING CONVENTION RULESFOR IDENTIFIER • They are formed by combining letters, digits & underscores. • Blank space is not allowed in an identifier. • The first character of an identifier must be a letter. valid invalid reason Monthly_Salary Monthly Salary Blank space cannot be used Month1 1stMonth Digit cannot be used as a first character Email_add email@ Special characters cannot be used
  • 11.
    Naming convention rulesfor identifier Rules Example Can contain a mix of character and numbers. However it cannot start with a number H2o First character must be a letter or underscore Number1, _area Can be of mixed cases including underscore character XsquAre my_num Cannot contain any arithmetic operators R*S+T … or any other punctuation marks #@x%!! Cannot be a C keyword/reserved word struct; cout; Cannot contain a space My height … identifiers are case sensitive Tax != tax
  • 12.
    NAMING CONVENTION RULESFOR IDENTIFIER Valid Invalid Comment X “x” Illegal character Gross_Pay Gross pay Illegal blank Hourly_rate Hourly-rate Illegal character - name name@ Illegal character @ sumx2 2sumx Illegal 1st character
  • 13.
    BASIC DATA TYPES Keywords(also called reserved words) Are used by the C++ language. Must be used as they are defined in the programming language. Cannot be used as identifiers. There are 5 basic data types : i. int iv. char ii. Float v. string iii. double
  • 14.
    BASIC DATA TYPE DATATYPE USES EXAMPLE CODE int int is used to define integer numbers. int Count; Count = 5; float float is used to define floating point numbers. float Miles; Miles = 5.6; double A double is used to define BIG floating point numbers. It reserves twice the storage for the number. double Atoms; Atoms= 2500000; char A char defines characters. char Letter; Letter = 'x';
  • 15.
    BASIC DATA TYPES a)int  used to declare numeric program variables of integer type  whole numbers, positive and negative  Syntax:  keyword: int  Example: int Bilangan; int Num1, num2;  Example code :  Sample values: 4578 -4578 0 int number; number = 12; int name_identifier;
  • 16.
    BASIC DATA TYPES b)float  fractional parts, positive and negative  real numbers with a decimal point.  3 type of floting point : 1. float (6 place decimal) 2. double (15 place decimal) 3. long double (19 place decimal)  Syntax:  Example:  keyword: float  Example declaring and initialing code:  sample values 95.274 95. .265 float height; height = 1.72; double name_identifier; double net_price;
  • 17.
    BASIC DATA TYPES c)char  equivalent to ‘letters’ in English language  Example of characters:  Numeric digits: 0 - 9  Lowercase/uppercase letters: a - z and A - Z  Space (blank)  Special characters: , . ; ? “ / ( ) [ ] { } * & % ^ < > etc  single character  Syntax:  keyword: char  Example code:  sample values ‘B’ ‘d’ ‘4’ ‘?’ ‘*’ The declared character must be enclosed within a single quote! char gred; gred = ‘A'; char name_identifier;
  • 18.
    BASIC DATA TYPES d)double  used to declare floating point variable of higher precision or higher range of numbers  exponential numbers, positive and negative  keyword: double  Code example: double valuebig; valuebig = 12E-3;
  • 19.
    BASIC DATA TYPE e)String a string is a sequence of characters enclosed in double quotes. string sample values: “Hello” “Year 2000” “1234” the empty string (null string) contains no characters and is written as “ ”. To declare a variable of type string: string greeting=“Hello”;
  • 20.
    BASIC DATA TYPES f)Bool (Short for boolean)  bool is a new addition to C++.  Boolean values are either true or false.  To declare a variable of type bool: bool old_enough;
  • 21.
    VARIABLES A declaration tellsthe compiler to allocate enough memory to hold a value of this data type and to associate the identifier with this location. int ageOfDog; float taxRateY2K; char middleInitial; 4 bytes for taxRateY2K 1 byte for middleInitial
  • 22.
    DECLARING VARIABLES Before use,variables must be declared. Why : To tells the compiler the type of data to store Declaration syntax: Type_name Variable_1 ,Variable_2, ... ; Declaration Examples:  double average, m_score, total_score;  double moon_distance;  int age, num_students;  int cars_waiting;
  • 23.
    TWO LOCATIONS FORVARIABLE DECLARATIONS. I M M E D I AT E LY P R I O R T O U S E main() { … int sum; sum = score1 + score 2; … } AT T H E B E G I N N I N G main() { int sum; … sum = score1 + score2; … } Example of Declaring Variables
  • 24.
    CONSTANT A named constantis a location in memory that we can refer to by an identifier, and in which a data value that cannot be changed is stored. VALID CONSTANT DECLARATIONS const string STARS = “****” ; const float NORMAL_TEMP = 98.6 ; const char BLANK = ‘ ’ ; const int VOTING_AGE = 18 ; const float MAX_HOURS = 40.0 ;
  • 25.
    DECLARING CONSTANT VARIABLES: METHOD1 Character constants  A character enclosed in a single quotation mark  Example: Enumeration  Values are given as a list  Example: const char letter = ‘n’; const char number = ‘1’; enum Language { Malay, English, Arabic };
  • 26.
    DECLARING CONSTANT VARIABLES: METHOD2 o You may also associate constant using #define preprocessor directive. o Example: #include <iostream.h> #define pi 3.412 void main(void) { double height, radius, base, volume; cout <<“Enter the height and radius of the cone:”; cin >>height >> radius; base = pi * radius * radius; volume = (1.0/3.0) * base * height; cout <<“nThe volume of a cone is ” << volume; }
  • 27.
    INITIALIZING VARIABLES Declaring avariable does not give it a value.  Giving a variable its first value is initializing the variable. Method 1: Variables are initialized in assignment statements double mpg; // declare the variable mpg = 26.3; // initialize the variable Method 2: Declaration and initialization can be combined using two methods:  Method 1: double mpg = 26.3, area = 0.0 , volume;  Method 2: double mpg(26.3), area(0.0), volume;
  • 28.
    KEYWORDS Keywords (also calledreserved words)  Reserved words in the C++ language for specific use.  Must be used as they are defined in the programming language.  Keywords that identify language entities such as statements, data types, language attributes, etc.  Have special meaning to the compiler, cannot be used as identifiers (variable, function name).  Should be typed in lowercase.  Cannot be used as identifiers or variable names.  Example: const, double, int, main, void,printf, while, for, else (etc..)
  • 29.
    C/C++ KEYWORDS auto breakcase char const continue default do double else enum extern float for goto if int long register return short signed sizeof static struct switch typedef union unsigned void volatile while C++ only keywords asm bool catch class const_cast delete dynamic_cast explicit false friend inline mutable namespace new operator private protected public reinterpret_cast static_cast template this throw true try typeid typename using virtual wchar_t