chapter 2 updated - color

8
7/27/2019 Chapter 2 Updated - Color http://slidepdf.com/reader/full/chapter-2-updated-color 1/8 10/15/2013 1 Learning outcomes Differentiate  between  variables and constants Differentiate  between character,  numeric and logical data types  ent y  operators, operan s, an  resu tants Identify  and use operators according to their precedence Set  up  and  evaluate  expressions  and  equations Understand and use predefined functions Six  Basic  Computer  Operations 1.  computer  can  receive information 2.  computer can put out information 3.  computer can perform arithmetic  4.  compu er can ass gn a  va ue o a  var a e or memory  location 5.  computer can compare two  variables  and select one or two alternate  actions 6.  computer can repeat a group of  actions 1.  A  Computer  can  receive Information When a computer  is required to receive information or input from a particular source,  whether it is a terminal, a disk or any  other device, the  verbs Read and Get are used in pseudocode.

Upload: farouk-azim

Post on 14-Apr-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 2 Updated - Color

7/27/2019 Chapter 2 Updated - Color

http://slidepdf.com/reader/full/chapter-2-updated-color 1/8

10/15/2013

1

Learning outcomes• Differentiate between  variables and constants

• Differentiate between character, numeric and logical data 

types

 • ent y  operators, operan s, an   resu tants

• Identify  and use operators according to their precedence

• Set up

 and

 evaluate

 expressions

 and

 equations

• Understand and use pre‐defined functions

Six Basic

 Computer

 Operations1. A 

 computer

 can

 receive information

2. A  computer can put out information

3. A  computer can perform arithmetic

 4. A  compu er can ass gn a  va ue o a  var a e or memory  location

5. A  computer can compare two  variables and select one 

or two alternate actions

6. A  computer can repeat a group of  actions

1. A

 Computer

 can

 receive Information

When a computer is required to receive information or input from a particular source,  whether it is a terminal, a disk or any  other device, the  verbs Read and Get are used in 

pseudocode.

Page 2: Chapter 2 Updated - Color

7/27/2019 Chapter 2 Updated - Color

http://slidepdf.com/reader/full/chapter-2-updated-color 2/8

10/15/2013

2

2. A computer can put out information

When a computer is required to supply  information or output to a device, the  verbs Print,  Write, Put, Output, or Display are used in pseudocode

Usually  an output Prompt instruction is required before an 

input Get instruction

3. A Computer can perform arithmetic Most programs require the computer to perform some sort of  

mathematical  calculation, or formula, and for these, a 

programmer may  use either actual mathematical symbols or the  words for those symbols

To be consistent  with high‐level programming languages, the following symbols can be  written in pseudocode:

+ for  Add ‐ for Subtract

* for Multiply  / for Divide

( ) for Parentheses

4. A computer can assign a value to a variable or memory location

There are three cases  where  you may   write pseudocode to 

assign a  value to a  variable or memory  location:

1. To give data an initial  value in pseudocode, the  verbs Initializeor Set are used

2. To assign a  value as a result of  some processing the symbols ‘= is

 written

3. To keep a  variable for later use, the  verbs Save or Store are used

5. A computer can compare two variables and select one or two alternate actions

An important computer operation available to the 

programmer is the ability  to compare two  variables and then, 

as a result of  the com arison  select one of  two alternate 

actions

To represent

 this

 operation

 in

 pseudocode,

 special

 keywords

 

are used: IF, THEN, and ELSE

6. A

 computer

 can

 repeat a

 group

 of 

 actions

When there is a sequence of  processing steps that need to be 

repeated, two special keywords, DO,DOWHILE and 

ENDDO, are used in pseudocode

The condition for the repetition of  a group of  actions is 

established in the DOWHILE clause, and the actions to be 

repeated are listed beneath it

Page 3: Chapter 2 Updated - Color

7/27/2019 Chapter 2 Updated - Color

http://slidepdf.com/reader/full/chapter-2-updated-color 3/8

10/15/2013

3

How we store data in a program?

Data  within a program may  be a single  variable, such as an 

integer or a character, or a group item (sometimes called an 

aggrega e , suc   as an array, s ruc ure, or a  e

Data naming

 convention

Name a  variable according to  what it represents (meaningful names)

Do not use spaces in a  variable name

Start a  variable  with a letter

  o not use any  mat emat ca  operator  n  var a e name

Be consistent  when using upper and lower case characters.

Importance of  Naming Convention Allows several programmers to  work on the same 

project  without conflict in using  variable’s name.

Allows program to be easily  read due to consistency  of   variable’s name  within a com an .

Code easily  to maintain.

Software should perform more efficient.

Increase performance

 expectation.

Produce clean,  well‐ written program.

Example of  Incorrect Variable Name

Data Item IncorrectVariable Name

Problem Correction

Hours worked hours worked Space betweenwords

hours_worked

Name of Client cn Does not definedata item

client_name

Rate of pay pay-rate Usingmathematicaloperator

pay_rate

Constants

• A  constant is a data item  with a name and a  value that remain 

the same (can never change) during the execution of  the 

program

 • .g. const  oat  = 3.142xxx

• Any  item  whose  value remains static during execution should 

be defined as a constant

• const float govt_tax = 0.05  for Govt. Tax = 5%

• const float sales_tax = 0.1 for Sales Tax = 10%

• const float service_charge = 0.1 for Service Charge = 10%

Variables

• A   variable is the name given to a collection of  memory  cells, designed to store a particular data item

• It is called a  variable because the  value stored in those memory  cells may  change or  vary  as the program executes

• E.g. total_sales = ????, (student_name = ????)• depends on amount of  items bought …..

• Total amount, Total Bill, Total Discount, etc…

Page 4: Chapter 2 Updated - Color

7/27/2019 Chapter 2 Updated - Color

http://slidepdf.com/reader/full/chapter-2-updated-color 4/8

10/15/2013

4

Data Types

Data are unorganized facts

 At the beginning of  a program, the programmer must clearly  define the form or type of  data to be collected for the  variables created 

The data types can be

• Primitive Data Types, or

• Composite Data 

1. Primitive Data Types

• An primitive data item is one containing a single  variable 

that is always treated as a unit

• A  data t e consists of  a set of  data  values and a set of  

operations that can be performed on those  values

• The most

 common

 elementary 

 data

 types

 are:

• integer  * character * boolean

• float * double

2. Composite Types

• The data items that it contains are its components,  which 

may  be primitive data items or another data structure

•  

* Records * File  (for permanent storage)

*  Array  * String

1. Data Type ‐ Numeric

• Numeric data

• Integers 

• Real numbers

 nteger :  w o e num er, suc   as 5297 or ‐3 7

can be positive or negative

Real number : also floating numbers ,

 which are  whole number plus

decimal part. e.g. 3.671234

2. Data Type  – Character/Alphanumeric

• ALL single

 digit

 numbers,

 letters,

 and

 special

 characters

 available in computer. (placed  within quotation marks)

• E.g.: ‘A’, ‘a’, ‘1’, ‘@’, ‘#’, ‘+’

• Uppercase and lowercase considered different character

• Cannot be used for calculation

• ‘A’+ ‘1’ = ???

• One of  more character can be combined into a “string”

• E.g. : “a”, “abc”, “Hello  World 123 !!”

Page 5: Chapter 2 Updated - Color

7/27/2019 Chapter 2 Updated - Color

http://slidepdf.com/reader/full/chapter-2-updated-color 5/8

10/15/2013

5

3. Data Type  – Logical/Boolean

• Have only  two  values – true or false

• Implementation in programming languages may  differ

‘ ’  ‘ ’  ‘ ’  ‘ ’  • ,  ,  yes,  no, 0, 1, e c

• Mainly  used as a control flag or switch

• E.g.

• credit_ok = true

• cheque_cleared = “Y”

Rules for Data Types Most commonly  used data types : numeric (integer and real 

number), character(including character string), and 

logical.

Pro rammer desi nates the data t e durin   ro rammin  

process

Use numeric data type for numeric item calculation. Other 

numeric item

 should

 be

 designated

 in

 character

 or

 

character‐string data type even if  data are all numbers, as in Zip Code

Data Validation

• Data should always undergo a  validation check before it is processed by  a program

• Different t es of  data re uire different checks – for exam le: 

*  Correct type * Correct range

*  Correct length *  Completeness

*  Correct date

Why 

Input 

Must 

Be 

Validated• Risk of  data entry  errors is high

• Large  volume of  data entered

• Human error keying in data

• Invalid input leads to inaccurate output• For example, salary  reported incorrectly  if  entered as 23000 instead 

of  32000 

• Input error can cause program interrupt• For example, spaces entered for numeric field used in arithmetic 

operation

Page 6: Chapter 2 Updated - Color

7/27/2019 Chapter 2 Updated - Color

http://slidepdf.com/reader/full/chapter-2-updated-color 6/8

10/15/2013

6

Operators Data connectors  within expressions and equations.

Tell computer how to process data

What type of  processing is to be done  

Relational (to program decisions)

Logical  to connect relational expressions to perform operations on logical data

Operators and Computer symbol

Mathematical Operator Result

 Addition + 3 + 5 8

Relational Operator Result

Equal to = 5=6 FALSE

Less than < 5<7 TRUE

Greater than > 3>1 TRUE

Subtraction - 7.5 - 2 5.5

Multiplication * 4 * 2.5 10

Division / 60 / 3 20

modulo division % 13 / 7 5

Less thanor equal to <= 3<=3 TRUE

Greater thanor equal to >= 4>=7 FALSE

Not equal to != 5!=3 TRUE

Logical Operator Result

Not ! ! True FALSE

 And && true AND false FALSE

Or || true OR false TRUE

Operator and Computer Symbol Example

int a = 5;

float  b = 3.2;

a =  + 6;  (variable a  will hold  value = 11)

a = 5 % 2;  (variable a  will hold  value = 1)

b = 5 / 2; (variable b  will hold  value = 2)

b+=3; ( b = b + 3)

a‐=2; (a = a ‐ 2)

Expressions and Equations

Expression An expression processes data (the operand) through the use 

of  operators

E.g. *  eng    w

Equation

An equation stores the result of  an expression in a memory  location through the equal (=) sign the assignment operator

Page 7: Chapter 2 Updated - Color

7/27/2019 Chapter 2 Updated - Color

http://slidepdf.com/reader/full/chapter-2-updated-color 7/8

10/15/2013

7

Setting Up Expression and Equation

X+6

X(3Y+4) – 4Y

Y + 3 is equal to X(Z + 5)

X is less than Y + 5

Evaluating Mathematical Expression

Let’s say   X  = 2,  Y  = 3, Z = 6

5 * (X + Y) – 4 * Y/(Z+6)

Y + 3 == X * (Z + 5)

X < (Y + 5)

Evaluating a Logical Expression

Example:  A   AND B OR  C  AND  A 

 A=True B=False C=True

Operation Resultant

1. A AND B False

2. C AND A True

3. Resultant of 1 ORresultant of 2

True

Function 

Function is a block of  program statement that  will perform 

specific task and in some of  the case, return the results to 

the program.

There are several rules about function in C program1) Every  C program consist of  one or more functions

2) One of  them must be named main

3) Program  will start and end  with the main function

4) main function is the “boss” of  all functions, if  there is any  additional functions, they  are subordinate to main.

5) main can call other function to do some tasks. 

Function Q:  Why   we need to have several functions in a program? 

A: There are actually  many  reasons for creating more than 

one functions in a program, as  we learn along,  you  will know the reasons behind this. But for the moment  ust tr  

to imagine, is it better to have one person to do all the  job? 

Or have several people to split the  job?

Page 8: Chapter 2 Updated - Color

7/27/2019 Chapter 2 Updated - Color

http://slidepdf.com/reader/full/chapter-2-updated-color 8/8

10/15/2013

8

Function Creating different functions in a program is like having 

different departments in a company. 

In real life, a company   will have different departments, each de artment is s ecialize in  erformin   certain task. 

E.g. HR  department handle the employees related task, Finance department handle the company  funds. 

In programming, each function is specially  designed to 

solve different problem. 

Types of  Function Predefined Function

Usually  built in  with the compiler

Written by  the developer to solve some common task 

Self ‐defined Function (chapter 4)

In most of  the situation,  we can’t solve all the problem 

by  using pre‐defined function.  We need to  write our own function.

A  customized instructions that  written by  ourselves are 

known as Self ‐defined Function 

Predefined Function Mathematical   functions.

Contains most of  the science, business and mathematical  functions such as square root, random numbers and power.

String  functions.

Used to

 manipulate

 string,

 for

 example

 copy 

 part

 of 

 the

 string

 

into another  variable, find the length or the number of  characters in the string.

Predefined Function Conversion  functions.

Used to convert data from one data type to another .

  .

Used to calculate things such as maximum  values, minimum 

 values, and so forth.

Utility  functions.

Used to access information outside the program and the language in the computer system such as include date and 

time functions.

Summary 6 basic computer operations

Constants and  Variables

Data types

Operators

Expressions and Equations