0% found this document useful (0 votes)
5 views

Csc121- Topic 3 Algorithm Design for Sequence Control Structure (1)

The document discusses algorithm design focusing on data types, operators, identifiers, variables, constants, and simple statements in programming. It explains different data types such as textual, numeric, alphanumeric, and standard data types, along with their characteristics and usage in programming. Additionally, it covers operators for arithmetic, relational, and logical operations, as well as the structure of input, output, and assignment statements in algorithms.

Uploaded by

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

Csc121- Topic 3 Algorithm Design for Sequence Control Structure (1)

The document discusses algorithm design focusing on data types, operators, identifiers, variables, constants, and simple statements in programming. It explains different data types such as textual, numeric, alphanumeric, and standard data types, along with their characteristics and usage in programming. Additionally, it covers operators for arithmetic, relational, and logical operations, as well as the structure of input, output, and assignment statements in algorithms.

Uploaded by

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

TOPIC 3 : ALGORITHM

DESIGN FOR SEQUENCE


CONTROL STRUCTURE
Data Type, Data & Information
 Data are raw materials entered into a computer to be
processed in order to produce meaningful information.

Data is a collection of unprocessed


items, which can include text,
numbers, images, audio, and video.

Information conveys meaning


and is useful to people.
Data type
 Textual data type consists of number or numeric, letter
(small or capital letter) and special symbols or special
characters.
 Textual data can be further divided into two types:
 Simple or primitive data type
 Data that has a single value and cannot be further
broken into small values.
 Complex or composite data type
 Can be broken down further to a few primitive
data. It means that a complex or composite data
are actually a collection of primitive data.
Data type
 Numeric data can be used in an arithmetic
calculation.
 Can be divided into:
 Integer
 A whole number, not involving decimal places.
 Example: 123, 45, 765
 Real number
 Involves decimal places.
 Examples: 1.23, 45.0, 76.5
Data Type
 Alphanumeric data cannot be used in arithmetic
calculation.
 Can be divided to a:
 Single character
 A primitive data type that can be used to store one
character only such as a letter, number, punctuation
mark or other symbol.
 A string
 A composite data type, a sequence of characters.
 Can be broken down into characters.
 Example data of string is “DM52” and it can be broken
down to separate characters of ‘D’, ‘M’, ‘5’ and ‘2’.
Operators and identifiers
How Data is Stored in a Computer
Memory?

 Assume that we want to involve data about mark,


average, total, grade and name in a program.
 Each variables has the values of 90, 70.0, 210, A and Siti
Alia.
 These data are stored in the computer memory as follows:
Mark Average Several types of data are observed.
90 70.0 The data type for variables mark and
total are integer, for variable grade is
Name a single character, for the variable
Siti Alia average is a real number and for
variable name is a string.
Grade Total
A 210

 The small rectangle represents a memory cell.


Identifier
• One feature present in all computer languages is the identifier – allow us to name data
and other objects in the program.
• Identifier is used to define names to represent variables, constant and name of
function.
• Each piece of data in the computer is stored at a unique address.
• If we didn’t have identifiers that we could use to symbolically represent data locations,
we would have to know and use data addresses to manipulate them.
• Instead, we simply give data identifier names and let the compiler keep track of where
they are physically located.
• Different programming language use different rules to form identifier.
• In C++, consists of letters (capital letter A through Z, the lowercase letter a to z), digits
(0 through 9), and the underscore (no other symbols are permitted to form a identifier.
• Besides, the 1st character cannot be a digit - must be begin with a letter or underscore.
• Good identifier names are descriptive (by combining two or more words) but short.

8
Variables
 Variables are memory locations, whose contents can
vary or differ over time.
 Names that refer to memory locations, that can hold
values
 It is the ability of memory variable to change in value
that makes computers and programming worthwhile.
 Because one memory location can be used over and
over again with different values, you can write
program instructions once and then use them for
thousands of separate calculations.
 Every computer programming language has its own
set of rules for naming variables.
Constant
 Constant are memory location, whose
content is not allowed to change during
program execution.
 Holds data that remains the same as the
program runs.
 Allow us to give a name to a value that is
used several times in a program.
 Once the variable is declared constant, the
value cannot be changed and the variable
cannot be assigned to another value.
11
Rules of naming identifier, variable
and constant
 The names follow only two rules:
 Names must be one word.
 The name can contain letters, digits, underscores, or other with the exception of
space.
 Names should have some appropriate meaning.
 This is not a rule of any programming language.
 As long as the correct numeric result is placed in the variable, its actual name doesn’t
really matter.
 However, it’s much easier to follow the logic of a program if you use appropriate
meaning of variable name.
 You might think that you will remember how you intended to use a cryptic variable
name within a program, but six years later when a program requires changes, you and
other programmers working with you, will appreciate clear as well as descriptive
variable names.
12 Standard data types

Standard
Data Types

void int char float bool


13
Standard data types
Data type Description

void ▪ The void type has no values and no operations.


▪ In other words, both the set of values and the set of operations are
empty.

Integer (int) ▪ An integer type is a number without a fraction part.


▪ It is also known as an integral number.
▪ C++ supports three different sizes of the integer data type: short
int, and long int (defines these data types so that they can be
organized from the smallest to the largest)

Character (char) ▪ A character is any value that can be represented in the computer’s
alphabet.
▪ Most computers use ASCII alphabet.
▪ Most of the personal, mini-, and mainframe computers use one
byte to store the char data types.
(Note: A byte is 8 bits. With 8 bits, there are 256 different values in
the char set)
14 … Standard data types
Data type Description

float ▪ A floating-point type is a number with a fractional


part.
▪ The C++ language support three different sizes of
floating-point data types: float, double and long
double (so that can be organized from smallest to
largest).
▪ Although the physical size of floating-point type is
machine dependent, many computer support the
sizes float (4 bytes), double (8 bytes) and long
double (10 bytes).
Boolean (bool) ▪ Logical or Boolean data consists of only two values:
true and false.
▪ True (1) and false (0).
15 Operators

Operators

Arithmetic Relational Logical


Arithmetic operator
16
 One of the most important uses of a computer is its ability to
calculate.
 To perform arithmetic operations (manipulate integral and
floating-point data types).
 The arithmetic operators:

Operat Operation
or
+ Addition
- Subtraction Integral data type
* Multiplication
/ Division
Floating-point data type
% Modulus (Remainder)
-- Decrement by 1
Integral data type
++ Increment by 1
17 Relational operator
 To compare the values of two operands.
 The evaluation result is either TRUE (1) or FALSE (0).
 The relational operators:

Operator Operation
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equal to
!= Not equal to
18 Logical operator
 When there is more than one relational expression at a
time, logical operator are used to perform the evaluation.
 The evaluation result is either TRUE (1) or FALSE (0).
 The logical operators:

Operator Operation
&& AND
|| OR
! NOT
19
OPERATOR PRECEDENCE
 When more than one arithmetic operator is used in an
expression, C++ uses the operator precedence rules to
evaluate the expression.
Operator Category Operator
(evaluated from left to
right) Highest
Parentheses ()
Multiply, Division, Modulus * / %
Add, Subtract + -
Relational Operators < <= >
>=
Equality Operators == !=
Lowest
Logical AND &&
Logical OR ||
The multiplication, division, and modulus operators are evaluated
before addition and subtraction operators. Operators having the
same level of precedence are evaluated from left to right.
Grouping is allowed for clarity.
Simple Statements
Simple Statements

 A statement is a step in algorithm.


 It is an instructions that tells the computer what to
do.
 Simple statements consist of:
 Input statement
 Output statement
 Assignment statement
Input Statement

 Purpose: To read the data entered by the user and


then store the value(s) into the variable(s) mentioned.
 A variable gets a value using input statement or using
assignment statement.
 Output statement is usually used together with input
statement to guide the user as to what kind of data
she/he has to enter.
 The word read or input or get or prompt is used to
implement input statement.
Input Statement (cont.)

 The format to write input statement are as follows:


 To read one data: Read variable
 To read many data: Read variable1, variable2, …
 For example:
 Display “Enter your name: ”
 Read name
 Display “Enter the mark: ”
 Read mark
Output Statement

 Purpose: To display or convey information to the user


through the computer screen.
 The form to write output statement are as follows:
 Display a message
 Display any message that we want the user to
see on the screen
 Display the value of variable(s)
 Display the content of a variable on the screen.
Output Statement (cont.)
 The format to write output statement:
 Display “what ever message to write”
 Display variable
 For example:
 Display “The average is: ”, average
 Display “My name is: ”, name
 Display “Enter your metric number: ”
 The above output statements will display the following information on
the screen:
 The average is: 70.00
 My name is: Siti Alia
 Enter your metric number:
Output Statement (cont.)
 A statement Display newline is a special output statement to instruct a
computer to go to the next line before the content of the next output
statement gets executed.
 For example:
 Display “The average is: ”, average
 Display newline
 Display “My name is: ”, name
 Display newline
 Display “Enter your metric number: ”
 The above output statements will display the following information on the
screen:
 The average is: 70.00
 My name is: Siti Alia
 Enter your metric number:
Assignment Statement
 Purpose: To assign data or value to a variable.
 It directly assigned a value to the variable or the result
of a calculation in the program.
 We can give value to the variable that stores a numeric
value using three ways:
 Assign a fixed value to a variable
 Copy the value of a variable to another variable
 A result of a calculation using a formula
Assignment Statement (cont.)
 Words that represent assignment statement: set or
initialize
 Symbols that represent assignment statement: = or
 For example:
 Initialize count to zero
 Count 0
 count = 0
 Set count to zero
 Set grade with ‘A’
 Set name to “Hawa Adam”
Assignment Statement (cont.)

🠶 We can copy the value of a variable to another variable


by writing these statements:
i. Set number2 with number1
It will cause the content at
ii. number2 number1 variable number1 to be copied to
location number2

iii.number1 = number2 It will cause the content at


variable number2 to be copied
to variable number1
Assignment Statement (cont.)

 Examples of the assignment statement involving


calculation:
 area = length x width
 Multiply variable length with width and store the
result into variable area
 Add 5 to variable cnt
 cnt = cnt + 5
 Divide sum with 5 and set the result to average
y=

You might also like