This document discusses the basic elements of programming languages and sequential structures. It covers data types, variables, input/output statements, arithmetic expressions, assignment statements, and function calls. Data can be manipulated using variables, which have attributes like type, lifespan, and scope. Common statements include input to read data, process statements like assignments, and output statements to display results. Expressions are used to evaluate values. Programs follow a sequential structure where statements are executed in the order they are written.
1. Chapter 2 :
Basic Element of Programming Language and
Sequential Structure
2. Lecture Outline
Basic Element of Computer Program
- General Overview
Data Manipulation
- Standard data type (int, float, double, char, char[ ])
- Variable
Computer Statement
- Statement (constant, identifier)
- Input/output statement
- Arithmetic expression
- Assignment concept
3. Basic Element of Computer Program
Problem Solving using Computer
Instruction/Steps 2 Data
Input /output
Comp. Statements Variable
•Data type
•Predefine/user define
2 3 4 •Identifier
Sequence Selection Iteration •Size
•Range
•Lifespan
•Scope
•Independent
•Method Functions 5
Procedural Programming vs Comp. Program
Object-oriented Programming
6
4. Data Manipulation
Types of data :
1. Input data – key in by the user (need to solve the problem)
2. Output data – process by the program and display to the user
3. Temporary data – used by the program in the process of obtaining
the output data.
Data is presented by a variable in a computer program.
Variable is a location in the computer’s memory where a value can be stored
for use by a program.
Elements of the variable :
Data Type (Pre Defined / User Defined)
Lifespan – How long will the variable exist?
Scope –Where can the variable being used?
Identifier – Rules in using identifier
5. Data Manipulation (cont.)
Pre Define Data Type (Size, modifier and range)
Example : int, float, char and double
Data Type Modifiers Size (bytes) Range
int Short, unsigned 2 0 to 65,535
Short, signed 2 -32,768 to 32, 767
Long, unsigned 4 0 to
4,294,967,295
Long, signed 4 -2,147,483,683 to
2,147,483,687
char 1 256 values
float 4 1.2e-38 to 3.4e38
double 8 2.2e-308 to
1.8e308
User Defined Data type – defined by user
Example: object and array
6. Computer Statement
Program is a list of instruction that represented by the computer statement.
Language element than can be used in constructing high-level language programming form
computer statement is called as Token.
6 types of token in C++ programming language :
Reserved word
Identifier
Constant
String Literal
Punctuation
Operator
7. Computer Statement (cont.)
Type of
Token Description Example
Reserved Word that has a special meaning to compiler main( ), int,
word strcpy
Must be typed in the correct location
Must be spell correctly (lower case letter)
Must be used in the right context
Name which is used in a computer program
Identifier Other than reserved word Salary, SUM,
Mainly to name variables and function PRO_10
Rules :
consists of letters A..Z, a..z, 0..9, and _
1st character must be a letter
Case sensitive
length : < 32 character, recommended
3 – 8 characters
Meaningful name
8. Computer Statement (cont.)
Type of Token Description Example
Constants Item with a fixed value (not be changed
in any statement)
Literal Constant – value typed directly, int pie = 3.142
wherever it is needed
Symbolic Constant – represented by a #define max 10.9
name using a preprocessor directive of const int size = 5
keyword const
String Literals Sequence of character surrounded by
“ n The minimum
double quotation marks value is :”
String literals may contain printable
characters as a, n.
9. Computer Statement (cont.)
Type of
Token Description Example
Punctuator Separators in C++ [ ], ( ), ;, *, #
To limit the various syntactical units in
programming language
Result in computation or action when +, -, *, <>, !=
Operator applied to variables or other elements in an
expression
Operator act on operand
Unary operator – operator that require one A++, C—
operand Total = sum + salary
Binary operator – operator that require two cout << (grade > 60 ?
operand “ Passed” : “Failed”)
Trenary operator – operator that required 3
operand (conditional operator (?:)
10. Computer Statement (cont.)
Several token will form a Statement.
Statement is an instructions to the computer.
Computer statement is a specification of an action to be executed by the
computer. It is cause the processor to do something.
Example :
An input statement will input value and placed it to a variable
An output statement will print message or result to the user on the computer
screen
Compound statement is a list of statement enclosed in braces { } that can
contain declaration and any type of computer statement.
11. Computer Statement (cont.)
Types of computer statement
Input statement
Process statement
Output statement
Input statement – read the value for one or more variable from the input/user.
Format : Pre Defined data type variable
cin >> variable1 >> variable2 …. >>variablen;
Example :
cin>> first >> middle >> last;
12. Input Statement
Input statement for user defined data type variable
Example: array – use getline command
string name;
char name[20];
Format :
// I) use the string data type
getline (cin, variable_name)
or
// II) user know the maximum number of characters will be used
cin .getline (variable_name, number_of_characters);
Example
getline(cin, name)
cin.getline(name,20)
13. Output Statement
Output statement - print the value of one or more expressions.
Format :
cout << expression1 << expression2 << … << expressionn;
Example :
cout << ringgit;
cout << “ Total value : “ << ringgit<<endl;
Print format :
endl – cursor go to next line
n - cursor go to next line
t - the print out will tab the printout
etc ..
14. Process Statement
Process statement – to ask processor to an action
Types of process statement :
Assignment statement
Function call statement
Assignment statement – to store the given value or value of an
expression in a variable
Format :
variable = expression;
Example :
sum = ringgit * 0.01 ;
ringgit = 100;
15. Assignment statement
Assignment statement also can use to the user defined data type such as
array of character (string copy – strcpy)
Purpose – to copy the content from one string variable to other string
variable (and make sure the size of the variable are same)
Format:
strcpy (new_variable_name,old_variable_name)
Example:
char old[20], new[20];
strcpy (new, old)
16. Function Call statement
Function call statement- used to execute the statement in a particular
function.
Purpose : The result of calling a function and supplying the values for the
function parameters.
Format :
function_name( expression1, expression2,…
expressionn);
Example:
cout << calculatepower( ); // with output statement
cout<<pow ( z+y, n); / /with parameter
value =sqrt(x);
total = calculate ( x, y ); // with assignment statement
17. Expression Concepts
Programmers should know how to construct the expression and how to get the
value.
The expression should syntactically correct and meaningful combination of
operators and operand.
Type of expression
Regular expression
Arithmetic expression
Logical Expression (discuss in chapter 3)
Regular expression – describing numbers and denotes any digit
between 0 – 9.
Example : x = 20;
18. Arithmetic Expression
Arithmetic expression – using arithmetic operator such as +, -, / etc
Example :
sum = no1 + no2
total = (no1 – 10) + ( no3 – 20)
C++ Arithmetic Algebraic expression C++ expression
Operation Operator
Addition + F+5 F+5
Subtraction - P-C P-C
Multiplication x BxM P*M
Division / X / Y or X ÷ Y X/Y
Modulus % R mod S R%S
Modulus - operator must applied to integer only give the remainder
of the integer division
19. Arithmetic Expression (cont.)
Precedence of arithmetic operators
Operator Operation Order of evaluation
() Parentheses Evaluated first, left to right
*, /, or % Multiplication Evaluated last, if there are
Division several, left to right.
Modulus
+ or - Addition Evaluated last, if there are
Subtraction several, left to right.
20. Sequential Structure
Sequence structure – the computer executes C++ statements one after the
other in order in which they are written.
Sequential Structure
Input Statement Process Statement Output Statement
Assignment Function Call
statement Statement