[go: up one dir, main page]

0% found this document useful (0 votes)
84 views13 pages

CSC 126 Chapter 2 PART 1

The document discusses the basic elements of computer programs including identifiers, variables, and variable declaration. Identifiers are used to name variables, constants, and functions and have specific rules for naming. Variables refer to memory locations that can hold values and must be declared before use by specifying the data type. Multiple variables can be declared in a single statement.

Uploaded by

2023875542
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views13 pages

CSC 126 Chapter 2 PART 1

The document discusses the basic elements of computer programs including identifiers, variables, and variable declaration. Identifiers are used to name variables, constants, and functions and have specific rules for naming. Variables refer to memory locations that can hold values and must be declared before use by specifying the data type. Multiple variables can be declared in a single statement.

Uploaded by

2023875542
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

CHAPTER 2:

BASIC ELEMENTS OF COMPUTER


PROGRAMS

Fundamentals of Algorithms and Computer Problem


Solving
IDENTIFIER
• Identifiers are used to define names to represent
variables, constant and name of function.
• Identifiers are formed by combining letters (both
uppercase and lowercase), digits and the underscore (_).
• There are 4 rules for naming identifier.
• The rules for naming identifiers are as follows:

1. The first character of an identifier must be a


letter or an underscore ( _ ).
Example of VALID identifiers:

Form1 _2nd_class side_3

Example of INVALID identifiers:

3number 8tv1 1_to_0


IDENTIFIER
2. ONLY letters, digits or underscores may follow the
initial letter.

SPECIAL CHARACTERS such as #, $, %, @, !, “, -


blank or whitespaces and etc. are NOT
ALLOWED;
Either used the UNDERSCORE to separate the
words in a name consisting of multiple words or
capitalize the first letter of one or more
words.

Example of VALID identifiers:

MilesToKilo kilo_to_miles kilo2miles

Example of INVALID identifiers:


Total$ km/hour comm.%
IDENTIFIER
3. Must NOT be keywords or reserved
words.

• A keyword or reserve word is a word that is set


aside by the language for a special purpose and
can only be used in a specified manner.
• For example: C++ keywords such as cin , cout,
return cannot be used as identifier name.
• Example of keywords or reserved words:

• asm, auto, bool, break, case, catch, char, class, const,


const_cast, continue, default, delete, do, double,
dynamic_cast, else, enum, explicit, extern, false, float, for,
friend, goto, if, inline, int, long, mutable, namespace, new,
operator, private, protected, public, register,
reinterpret_cast, return, short, signed, sizeof, static,
static_cast, struct, switch, template, this, throw, true, try,
typedef, typeid, typename, union, unsigned, using, virtual,
void, volatile, wchar_t
IDENTIFIER
4. Identifiers are CASE-SENSITIVE

• For example
• NUM1, num1, Num1 are three different identifiers.

5. Identifiers must be MNEMONIC or


MEANINGFUL CODE or word that reflects
the data items they store.
• For example, TOTAL scores the result of an arithmetic
operation; PI stores the PI value (3.142) and etc.
• Examples of meaningful identifiers:
salary value name total

• Examples of valid identifiers but are not meaningful:

R3s4 n1 nike hardy


IDENTIFIER

More examples:
VALID INVALID REASON INVALID

x “x” Character “ not


allowed
sumx2 2sumx Begin with a number

hourly_rate hourly-rate Hyphen not allowed

name name@ Character @ not


allowed
GROSS_PAY GROSS PAY Illegal blank
Variable is an identifier
that refers to MEMORY
LOCATION, which can
HOLD VALUES.

Variable can only store


VARIABLE ONE VALUE AT ONE
TIME.

Thus, the CONTENT OF


VARIABLE may change
during the program
execution.
VARIABLE
VARIABLE DECLARATION

• ALL VARIABLES MUST BE DECLARED BEFORE they can be used in the


program.

• The GENERAL FORM of the declaration is:

data_type variable_name;

int nombor;
VARIABLE
int num; reserving a memory location called num to
hold an integer value
float value; reserving a memory location called value to
hold a floating-point number
double total; reserving a memory location called total to
hold a double-precision number
char grade; reserving a memory location called grade to
hold a character
char name[20]; reserving a memory location called name to
hold 20 characters
long datnum; reserving a memory location datnum to hold a
long integer
VARIABLE
VARIABLE DECLARATION

• One variable is declared, it contains NO USEFUL VALUE (garbage).


• To make it useful, the variable MUST BE GIVEN A VALUE, either by the
ASSIGNMENT STATEMENT or INPUT FROM THE USER, before it is used.
• This value serves as the initial value to the variable.
• Example of variable initialization:

num=10; sets the initial value of 10 into the variable


num using assignment statement
cin>>number1; set the initial value into the variable number
using input statement
VARIABLE
VARIABLE DECLARATION

• It is a good programming practice to DECLARE AND INITIALIZE a variable


using assignment statement on the same line. This will also reduce the
length of a program.
• Example of variable declaration AND initialization

int num=10; //declaration num as integer variable and


sets the value of 10 into num
float number = 5.5; //declare number as a float variable and
sets the value of 5.5 into number
VARIABLE
MULTIPLE VARIABLES DECLARATION
• Variables having the same data type can be grouped together and declared using a
single-declaration statement.
• For example, the following declaration statements:
int num1;
int num2;
int sum;
• Can be written a s a single statement
int num1, num2, sum;

• Another example to assign values to the variable num1, num2 and sum with the initial
10, 20, 0 respectively, the C++ statement can be written as:
int num1 = 10, num2 = 20, total = 0;

• The following example gives all the variables the value 0


score1 = score2= score3 =0;
REFERENCES:

Problem solving With C++ (Norizan


Mohamad & Mazidah Puteh (UiTM)

Liang, Y.D., Introduction to Programming


with C++, 2nd edition, Pearson Higher
Education, 2010

You might also like