### What does a computer program contain: statements and variables

• Computer programming: solving a problem with a computer

• Goal of computer programming:

 We write a computer program to solve a problem or perform a task

• In order to solve a problem, we need:

 An algorithm (detailed steps that solve the problem) Information !!!

The basic fact of life is that without information, we cannot complete the task !!!

• Example: compute the sum of 2 numbers

 Algorithm: perform an addition Information: the value of the 2 numbers

Without the value of the 2 numbers, we cannot perform the addition operation !!!

• Computer program = an algorithm + (lots of) information

• A computer program consists of:

 computer instructions that tells a computer precisely what to do to accomplish the job (Lots of) information to guide the algorithm to do the job

• This fact is expressed in the title of a classical Computer Science book:

• The title of this classical Computer Science book reflects the fact that to solve a problem (write a program) with a computer, you need:

 An algorithm, and Information (that is stored in data structure)

• Statements: expressing the steps in an algorithm

• Statements:

 Statement = an instruction written in a (High level) Programming Language A statement tells a computer in very precise terms what exactly it must do

Example of statements:

 Store the value 26 in the memory cell named "A" Add the number A to the number B If the number A ≥ 0 then do this otherwise do that

• Variable: storing the information

• Variables:

• A variable = a identifiable memory cell stores information that is necessary to solve a problem

• A variable behaves like a piece of paper that is identified by a unique name where we can record (a small amount of) information

Example:

• Data structure:

 A data structure = a collection of variables that store related information

Example:

 One variable stores the name of a person Another variable stores the phone number of a same person These 2 variables together would form a data structure

• Data types

• Previously discussed:

• There are different kinds of information

E.g.: gender information, marital status information, etc

• Each kind of information uses its own information encoding method

Example:

 Gender information: 0 = male and 1 = female Marital status information: 0 = single, 1 = married, and so on...

• Information is stored in variables.

• Consequently, there are different types of variables:

 Numerical variables, that are used to store data such as: age, salary, and so on. Textual variables, that are used to store alpha-numerical data such as text in books, news paper articles, webpages, and so on. Boolean (logical) variables, that are used to store the logical value TRUE and FALSE Etc.

• Fact:

 Different types of variables use different encoding methods to represent/encode the information

• Data type:

• Data type = all the values that use the same encoding method

In other words:

 One data type uses one (same) encoding method (to represent the same kind of information)

• A programming language always provides a number of built-in data types to get your started

Common built-in data types:

 Single precision floating point numbers Double precision floating point numbers Integer numbers A character type A logical data type

The built-in data types are called primitive data types

• In addition, a modern programming language allows the programmer to define his/her own data types

These are called user-defined types.

• Creating variables: Variable definition

• Recall:

 A variable is a storage location for information (data) It is like a (small) piece of paper where you can write a number You can also erase an esisting number and write a new number on the paper (= variable).

• A variable in a program is created by a

 Variable definition clause

• In modern programming languages, we must "define" every variable in the computer program

• Effect of a variable definition:

• Some memory cells will be reserved to store the information

 Each variable definition must use a unique name to identify the variable.

• So: a variable definition clause will tell the computer to create the variable that will be identified by that (unique) name

This is like telling the computer to:

 get a new sheet of paper and tag the new sheet of paper with the name of the variable