Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Programming 12 Lesson 3 – Computer Programming Concepts By Dan Lunney.

Similar presentations


Presentation on theme: "Computer Programming 12 Lesson 3 – Computer Programming Concepts By Dan Lunney."— Presentation transcript:

1 Computer Programming 12 Lesson 3 – Computer Programming Concepts By Dan Lunney

2 Constants and Variables Constants and variables are pieces of data used by a program to solve problems Constants and variables are pieces of data used by a program to solve problems Constants – a value, alphabetical or numerical, that does not change during the processing of all the instructions in a solution Constants – a value, alphabetical or numerical, that does not change during the processing of all the instructions in a solution Variable – a value, alphabetical or numerical, that can change during the processing of all the instructions in a solution Variable – a value, alphabetical or numerical, that can change during the processing of all the instructions in a solution Both constants and variables are assigned names and values Both constants and variables are assigned names and values

3 Examples Pi = 3.14 (constant) Pi = 3.14 (constant) Radius = 0 (variable) Radius = 0 (variable) Red = “RED” (constant) Red = “RED” (constant) F_Name = “Dan” (variable) F_Name = “Dan” (variable)

4 Rules for Naming Variables Should be named according to what it represents Should be named according to what it represents No spaces allowed No spaces allowed Must start with a letter Must start with a letter Do not use a dash in name Do not use a dash in name Must be used exactly as named Must be used exactly as named Must be consistent with upper and lower case Must be consistent with upper and lower case

5 What is wrong with the following variables? Hours Worked Hours Worked CN CN Pay-Rate Pay-Rate Quantity/custom er Quantity/custom er 6%_sales_tax 6%_sales_tax Client_address_ for_clientXYZ_i n_Newfoundlan d Client_address_ for_clientXYZ_i n_Newfoundlan d Hours_Worked Hours_Worked Client_name Client_name Pay_Rate Pay_Rate Quantity_per_c ustomer Quantity_per_c ustomer Sales_Tax Sales_Tax Client_Address Client_Address

6 Data Types Each variable or constant has a data type Each variable or constant has a data type Data types can be Data types can be Numerical Numerical Characters Characters Logical (on/off or true/false) Logical (on/off or true/false)

7 Rules for Data Types Come in three common forms: numbers, characters, logical Come in three common forms: numbers, characters, logical Must be designated with the variable when originally named Must be designated with the variable when originally named Cannot be mixed (can’t save text in a numeric variable) Cannot be mixed (can’t save text in a numeric variable) Each data type has a data set i.e. numeric includes 0-9, +, -,. Each data type has a data set i.e. numeric includes 0-9, +, -,. Variables used in calculations must be numeric data type Variables used in calculations must be numeric data type

8 Examples The price of an item $4.25 The price of an item $4.25 Numeric real Numeric real The name of a company The name of a company Character string Character string A quantity A quantity Numeric integer Numeric integer Credit check (true/false) Credit check (true/false) Logical Logical Phone number Phone number Character string Character string

9 Functions Functions are small sets of instructions that perform specific tasks and return values Functions are small sets of instructions that perform specific tasks and return values They usually take the form: They usually take the form: Function_name(data) Function_name(data) The data used in a function is called a parameter and is usually stored in a variable The data used in a function is called a parameter and is usually stored in a variable

10 Functions (cont.) Functions usually are divided into classes: Functions usually are divided into classes: Mathematical functions Mathematical functions SQRT(N) SQRT(N) String functions String functions Left(S, n) Left(S, n) Conversion functions Conversion functions VALUE(String) VALUE(String) Statistical functions Statistical functions AVERAGE(list) AVERAGE(list) Utility functions Utility functions DATE() DATE()

11 Operators Operators are data connectors used in equations and expressions Operators are data connectors used in equations and expressions There are three categories There are three categories Mathematical operators Mathematical operators Relational operators Relational operators Logical operators Logical operators

12 Mathematical Operators OperatorSymbolExample OperationResultant Addition+ 3.0 + 5.2 8.2 Subtraction- 7.5 – 4.0 3.5 Multiplication* 8.0 * 5.0 40.0 Division/9.0/4.02.25 Integer division \9\42 Modulo division MOD 9 MOD 4 1 Power^3^29

13 Relational Operators OperatorSymbolExample OperationResultant Equal to = 5 = 7 FALSE Less than < 5 < 7 TRUE Greater than > 5 > 7 FALSE Less than or equal to <= 5 <= 7 TRUE Greater than or equal to >= 5 >= 7 FALSE Not equal to <> 5 <> 7 TRUE

14 Logical Operators OperatorSymbolExample OperationResultant NotNOT NOT True FALSE AndAND TRUE AND TRUE TRUE OrOR TRUE OR FALSE TRUE

15 Expressions and Equations An expression processes data An expression processes data Ex. LENGTH * WIDTH Ex. LENGTH * WIDTH An equation stores the resultant of an expression in a variable An equation stores the resultant of an expression in a variable AREA = LENGTH * WIDTH AREA = LENGTH * WIDTH Equations are often called assignment statements because the variable on the left is assigned the value of the expression on the right Equations are often called assignment statements because the variable on the left is assigned the value of the expression on the right Ex. PAY = Rate * Hours Ex. PAY = Rate * Hours

16 Sample Problem – Lesson 3 Answer the data related questions in the sample problem: Answer the data related questions in the sample problem: See sample problem sheet See sample problem sheet See sample problem sheet See sample problem sheet


Download ppt "Computer Programming 12 Lesson 3 – Computer Programming Concepts By Dan Lunney."

Similar presentations


Ads by Google