Professional Documents
Culture Documents
TOPIC 2
BASIC ELEMENTS OF COMPUTER
PROGRAMS
PART 1
PREPARED BY: PUAN HARSHIDA BINTI HASMY
LEARNING OUTCOMES
LEARNING OUTCOME
INTRODUCTION
Basic Elements of
C++ Program
Structure
○ General structure of a program includes the 6
Basic Elements of
C++ Program
Structure
BASIC C++ PROGRAM
When you execute this program, it will print out the sentence
“Hello, welcome to C++ Programming!!” on the screen
BASIC C++ PROGRAM
PREPROCESSOR DIRECTIVES
PREPROCESSOR DIRECTIVES
• iostream (for input and output statements, cin & cout)
OUTPUT SAMPLE
main function
{ BRACES }
int main()
{
cout << "Hello world!";
return 0;
}
• Braces are used to mark the beginning and end of
blocks of code in a program.
• What is contained in these braces is what the
function does when it is executed.
BASIC C++ PROGRAM
STATEMENTS
LEARNING OUTCOME
• IDENTIFIERS RULES:
• The first character of each identifier/variable MUST BE an alphabet letter
(a,b,c …, z OR A,B,C…,Z) or an underscore ‘_’.
✓ Examples : Brand1, _1stClass, sOne.
• Can only consists of letters, digits and underscore ‘_’.
• NO SYMBOL allowed (!@#?%^&*.)
• Cannot use C++ reserved words/keywords
• There must be no blank space character (Room 3, Level 1)
• Identifiers are case sensitive
✓ Examples : NO1, no1, No1,nO1 (Each one considered as different)
IDENTIFIER NAMES
IDENTIFIERS
LEGAL AND ILLEGAL IDENTIFIERS
▪ The following are LEGAL identifiers in C++:
a first TAX
ResultIn _2ndFloor isAvailable
payRate choice average
person1 max2022 x1
data type
const keyword
whitespaces
whitespaces
27
LEARNING OUTCOME
○ All the variables and constants have to be declared before they are
used in the program.
○ To declare the variables, we have to identify the categories of data and
name each uniquely.
○ It is because once we declare the variable, a memory space will be
provided.
○ So, to allocate the size, we have to base it on the variable categories.
○ Data type represents the size and type of a variable.
DATA TYPE IN C++ (CONT.)
DATA TYPE IN C++ (CONT.)
❑ Data type will explain the size of memory needed to hold the value of
a variable.
❑ Also known as a container, data type will hold values based on their
type and size, and also helps us to define the categories of the
variable.
❑ We can split them to numeric data type (whole and decimal
numbers), character data type, and logical value data type.
NUMERIC DATA TYPE
STANDARD DATA TYPES
int
float / double
char
bool
string
C++ DATA TYPES
string
data type
(AN ALTERNATIVE FOR char title[30]
Categories of
data type
NUMERIC CHARACTER
data type data type
(char)
❖ integer BOOLEAN
(int) data type ❖ Single Character
(char)
❖ floating / ❖ true / ❖ String of
decimal points false(bool) characters
(float / double) (char title[5])
DATA TYPES
35
36
CHARACTER DATA TYPE (char[size number])
LEARNING OUTCOME
When you execute this program, it will print out the sentence
“Hello, welcome to C++ Programming!!” on the screen
THE BASICS OF A C++ PROGRAM
FUNCTION
OUTPUT SAMPLE
SYNTAX ERROR
(detected by the compiler)
caused by missing ;
(semicolon) at line #7
THE BASICS OF A C++ PROGRAM
COMMENTS
• Are meant only for the human readers, not for the compiler
• Used only to allow the readability of the program.
• The compiler will ignore comments placed inside the program
{ BRACES }
int main()
{
cout << "Hello, there!";
return 0;
}
• Braces are used to mark the beginning and end of blocks of code in a
program.
• What is contained in these braces is what the function does when it is
executed.
THE BASICS OF A C++ PROGRAM
STATEMENTS
TO BE CONTINUED..
PART 2 OF TOPIC 2
TYPES OF OPERATOR:
ARITHMETIC OPERATORS
ASSIGNMENT OPERATORS
INPUT/OUTPUT STATEMENT
PREDEFINED FUNCTIONS
CONTROL STRUCTURES + ALGORITHM