0% found this document useful (0 votes)
58 views55 pages

(CSC128 032023) Topic 2 Basic Elements of Computer Program Part A

This document discusses the basic elements of computer programs in C++, including identifiers, variables, constants, data types, and basic program structure. It explains that every C++ program must have a main function, preprocessor directives, comments, and statements. It also covers numeric and character data types in C++ like int, float, double, char, bool, and string. The goal is to understand the fundamental building blocks needed to write C++ programs.
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)
58 views55 pages

(CSC128 032023) Topic 2 Basic Elements of Computer Program Part A

This document discusses the basic elements of computer programs in C++, including identifiers, variables, constants, data types, and basic program structure. It explains that every C++ program must have a main function, preprocessor directives, comments, and statements. It also covers numeric and character data types in C++ like int, float, double, char, bool, and string. The goal is to understand the fundamental building blocks needed to write C++ programs.
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/ 55

CSC128

TOPIC 2
BASIC ELEMENTS OF COMPUTER
PROGRAMS

PART 1
PREPARED BY: PUAN HARSHIDA BINTI HASMY
LEARNING OUTCOMES

Understand more about identifier, variable, constant,


reserved word and data types
Able to identify the basic elements needed in writing a
program.
Understand and apply the input/output statement.
Justify the process of writing the program and error
handling.
Arithmetic operators, precedence, and mathematical
expression
4

LEARNING OUTCOME

To identify basic elements of a computer


program.
5

INTRODUCTION

Basic Elements of
C++ Program
Structure
○ General structure of a program includes the 6

elements used in the programming steps.


INTRODUCTION

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

• The preprocessors directives is also known as


header file in C++ .
• All preprocessor directives in C++ begin with #,
and they do not need to end with a semicolon(;)
because this is not a statement in C++.
BASIC C++ PROGRAM
main FUNCTION
Every C++ program has a function called main
The execution of all C++ programs begins with the main function,
regardless of where the function is actually located within the code.

main function

• The word main, must be typed in lowercase letters.


• A function is simply a block of code that perform tasks.
• Every C++ program has a function called main.
BASIC 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

Single line comment: Begin


with //

Multiple line comments:


Enclosed between
/* and */
BASIC C++ PROGRAM

{ 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

• A function should consist of a sequence of statements the


perform the work of the function.
• It consist of instructions or command which make the
program work.
• Three (3) types of statements:-
1. Input statement → cin >> statement;
2. Output statement → cout << statement;
3. Operation/arithmetic statement → Mathematical operation
(* , - , + , / , %)
13

LEARNING OUTCOME

To introduce variable identifiers, constants


and reserved words in C++
WHAT IS AN IDENTIFIER ? (VARIABLE)
Represent storage location in the computer’s memory to store or hold values.
A symbolic name in the form of a letter, word or phrase that linked to a value.
Every variable stored in the computer’s memory has a name, a value and a
type.
Before it is used, it must be declared with its data type .
Example : int i = 10; ( i is the variable name which stored integer value 10
inside computer memory)
IDENTIFIERS
• All C++ variables must be identified with unique names.
• These unique names are called identifiers.
• An identifier is a name of things that appear in a program,
includes:
• variables / constants / functions
• All identifiers must obey C++’s rules for identifiers.
• Consists of letters, digits and underscore character ( _ )
• No space or comma is allowed
• Must only begin with letter or underscore ( _ )
IDENTIFIERS (CONT.) 16

Rules to write identifier name.


LIST OF KEYWORDS/RESERVED WORDS
AND SPECIAL SYMBOLS
WHAT ARE THE IDENTIFIER RULES??

• 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

▪ Meanwhile, these are the ILLEGAL identifiers


IDENTIFIERS (VARIABLE)
▪ Can only store 1 value at a time
▪ Value of a variable may change during program
execution.
Syntax: ▪ Must be declared before used.
data_type variable;
int number1;
Example: int number2;
int x; int number3;
(three different variables with the same data
int number1, number2, number3; type int can be combined together)
char _codeMI7 ;
float after_sale_price;
Please take note of the coma (,) to separate the variable list &
Semicolon (;) to end the variable declaration
IDENTIFIERS (VARIABLE)
IDENTIFIERS (VARIABLE)
Variable: what happen when a variable is declared??

○ The compiler allocates a memory space for the variable.


○ In the memory space, name, data type and value of the variable
are stored.
○ ex. float length = 6.0;
IDENTIFIERS (CONSTANT)
• Values that are fixed
• The content inside it will not change throughout the program
execution
• Usually constant name is capital to distinguish from other
variables.
• The syntax to declare a constant:
const dataType variableName = value;
For example:

const float PI = 3.142; ←variable PI is used to


… represent a constant
cin >> number; value of 3.142
area = 2 * PI * radius; ←legal statement
PI = number;←illegal statement because trying to
change constant value with a new value
CONSTANT DECLARATION
Syntax:
const data_type CONSTANT_NAME = value;

data type
const keyword

const float TAX = 0.06;


Constant_name value
WHITESPACES
▪ Every C++ program contains whitespaces
▪ Include blanks, tabs, and newline characters
▪ Used to separate special symbols, reserved words, and
identifiers
▪ Proper utilization of whitespaces is important
▪ Can be used to make the program more readable

whitespaces

whitespaces
27

LEARNING OUTCOME

To introduce data and data types (integers,


floating point data, character,boolean)
DATA TYPE IN C++

○ 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

char var[0] (to replace string)

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])

STRING OF CHARACTERS (“TEXT”) :-


( keyword = char variable[size];)
◦ A string = a sequence of characters that terminated by a null character, ‘\0’ which has the value
zero.
◦ The null character denotes the end of a string and is added to the string by compiler
automatically.
◦ Example, to declare a string text that could hold 5 characters, the following statement is written.

char text[6] = “Hello”;


char staffName[20] = “Umar Bin Khalid”

Note: the double quotes (“ “) and single quotes (‘ ‘)

◦ Enclosed in double quotation marks (“ “).


◦ Position of first character is 0, the position of the second is 1, and so on
CHARACTER DATA TYPE (char[size number])
38
39
VARIABLE AND CONSTANT DECLARATION

○ The process of clarifying the variable name and data type of a


variable is known as variable declaration.
○ The declaration is a C++ statement, so it should end with a semicolon
(;).
○ In the declaration statement, we should write the data type, followed
by the variable name.
○ The general way to write a variable declaration is as follows:
Data type variable_name;
❑ Examples: • char code[6];
• int age; • bool status;
• double total_Price; • float weight ;
• char gender; • string name ;
VARIABLE DECLARATION

Makes a variable, named price, of data


float price; type float
CONSTANT VARIABLE DECLARATION

○ Constant variables must also be declared before they can be used.


○ A constant variable is a variable that has a value that never changes.
○ To declare the constant variable, we will use the literal value, which means that
we are using the constant value. A literal value is any fixed or constant value
used in a program.
○ The syntax of a declaration statement for a constant variable is:
const data type variable name = literal or actual value;
○ Examples:
• const double PI = 3.142 ;
• const char[20] COMPANY_NAME = “ABC Interprise”;
43

LEARNING OUTCOME

To introduce 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
THE BASICS OF A C++ PROGRAM

FUNCTION

Every C++ program has a function called main

• A function is a code module / block of code that perform a single task.


• Every C++ program has a function called main.
• The word main, must be typed in lowercase letters.
T H E B AS I C S O F A C + + P R O G R AM
PREPROCESSOR DIRECTIVES
PREPROCESSOR DIRECTIVES
• iostream (for input and output statements)
• cstring (for handling string data)

OUTPUT SAMPLE

• The preprocessors directives is also known as header file in C++ .


• All preprocessor directives in C++ begin with #, and they do not need to end with a
semicolon(;) because this is not a statement in C++.
THE BASICS OF A C++ PROGRAM
WHAT IS SYNTAX RULES?
• Rules that determines whether the statements (instructions) within the
program are valid or not

• If valid, this means that the statements are accepted by the


programming language

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

Single line comment: Begin


with //

Multiple line comments:


Enclosed between
/* and */
THE BASICS OF A C++ 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

• A function should consist of a sequence of statements


the perform the work of the function.
• It consist of instructions or command which make the
program work.
• Three (3) types of statements:-
1. Input statement → cin >> statement;
2. Output statement → cout << statement;
3. Operation statement → Mathematical operation
*,-,+,/,%
THE BASICS OF A C++ PROGRAM
/* This program is to compute the sum of two integer
Comment
numbers */

Preprocessor #include <iostream> //Preprocessor to handle cout and cin


Directive statement
using namespace std;

Function int main() //This is the beginning of a function named main


{
Begin Block
int no1, no2, sum; // Variable declaration

cout << “\nEnter first number: “ ; //This a prompt


cin >> no1; //Input from console
cout << “ \nEnter second number: “ ;
Function Body
cin >> no2; //Input from console

sum = no1 + no2; //Adding no1 and no2


cout << “\nSum = “ << sum ; //Output to console
return 0;
End Block }

Semicolon ; = delimiter . Use to end of statement or line.


EXERCISE
EXERCISE
53
54

Thanks! End of Part 1 of TOPIC 2.


Any questions?
55

TO BE CONTINUED..
PART 2 OF TOPIC 2
TYPES OF OPERATOR:
ARITHMETIC OPERATORS
ASSIGNMENT OPERATORS
INPUT/OUTPUT STATEMENT
PREDEFINED FUNCTIONS
CONTROL STRUCTURES + ALGORITHM

You might also like