0% found this document useful (0 votes)
2 views25 pages

Algorithm 2 - Variables and Data Types

Uploaded by

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

Algorithm 2 - Variables and Data Types

Uploaded by

aura
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introducere in IT

Algorithms - Variables and


Data Types

Introducere in IT 1
Agenda

➢ What is a variable
➢ Data types
➢ Assigning a value to a variable
➢ Comparing variable values

Introducere in IT 2
Tools

➢ Visual Studio Code

Introducere in IT 3
What is a variable

Introducere in IT 4
What is a variable

A variable is a series of alphanumeric characters that is used as a name for the


area of the computer memory that stores a value.

Example:
shoeSize = 3

● “shoeSize” is the name of the variable


● “3” is the value of the variable

Introducere in IT 5
Data types

Introducere in IT 6
Data types

A variable can store data of different types.

Example:
● numericValue = 3.14
● textValue = “Ana likes apples”
● booleanValue = true
● booleanValue = false

A boolean variable can have only 2 values: true or false.

All values are stored in the computer memory as binary numbers.

Introducere in IT 7
Data types

A variable can also store collection of values.

Example:
● arrayOfNumbers = [ 5, 9, 12, 22 ]
● arrayOfWords = [ “keyboard”, “mouse”, “monitor” ]
● arrayOfBooleans = [ true, false, false, true]

Each value in the array has a position.

The position of the first element in the array is 0.

Introducere in IT 8
Data types

We can use the position (sometimes named index) of a value to retrieve a single
value from the array.

Example:
0 1 2 3 <= position / index of the value
arrayOfNumbers = [ 5, 9, 12, 22 ]

The statement “arrayOfNumbers[0]” returns the value 5

The statement “arrayOfNumbers[2]” returns the value 12

Introducere in IT 9
Data types

0 1 2
arrayOfWords = [ “keyboard”, “mouse”, “monitor” ]

The statement “arrayOfWords[0]” returns the value “keyboard”

The statement “arrayOfWords[2]” returns the value “monitor”

The statement “arrayOfWords[3]” returns the value undefined (or generates an


error)

Introducere in IT 10
Assigning a value to a variable

Introducere in IT 11
Assigning a value to a variable

To set (assign) a value to a variable we use the “=” symbol.

Example:
a=5

When we assign a value to a variable the value is saved as a binary value in the
computer memory.

Each memory block from the computer has an address. We will use the variable
name as an alias for the memory address where the value is stored.

Introducere in IT 12
Assigning a value to a variable

Algorithm: Computer memory:


a=5 0xf1dc8006 0101 Value 5 in
binary
b=3 0xf1dc800a 0011

Value 3 in
binary

Memory address of the


that stores the value of
the variable named “a” Memory address of the
that stores the value of
the variable named “b”

Introducere in IT 13
Assigning a value to a variable

A the result of an expression can also be assigned as the value of a variable

Example:
b=2+3

The expression is separated by the “=” symbol in 2 parts


The left part is always the variable name (“b” in the example above)
The right part can be a value or another expression (“2+3” in the example above)

The right part is evaluated first.


That means that the computer will calculate the result of 2+3 and replace it with
the value 5.
Then the calculated value will be assigned to the variable b.

Introducere in IT 14
Assigning a value to a variable

If the right side expression contains a variable the variable will be replaced with it’s
value.

Example:
a=5
b=a+3

1. The computer will replace the variable “a” with it’s value resulting in the
equivalent expression “b = 5 + 3”
2. The computer will calculated and replace the add operation with it’s result
thus giving the equivalent expression “b = 8”
3. The computer will save the value 8 at the computer memory address
reserved for the variable names “b”

Introducere in IT 15
Assigning a value to a variable

The value of a variable can be updated based on the initial variable value.

Example:
a=5
a=a+3

1. The computer will replace the variable “a” with it’s value resulting in the
equivalent expression “a = 5 + 3”
2. The computer will calculated and replace the add operation with it’s result
thus giving the equivalent expression “a = 8”
3. The computer will save the value 8 at the computer memory address
reserved for the variable names “a”

Introducere in IT 16
Comparing variable values

Introducere in IT 17
Comparing variable values

Values can be compared between each other. The result of a comparison is


always a value of type boolean

Example:
a=5<3

After the statement above is computed the variable “a” will have the value “false”

Example:
a=5
b=3<a

After the statement above is computed the variable “b” will have the value “true”

Introducere in IT 18
Comparing variable values

We use the “==” sign to indicate that we are comparing to values. (asignment is
indicated with the symbol “=”)

Example:
x=3
y=3
a = x == y

1. The computer will replace the variable “x” and “y” with theyr value resulting in
the equivalent expression “a = 3 == 3”
2. The computer will replace the comparison with it’s result resulting in the
equivalent expression “a = true”

Introducere in IT 19
Comparing variable values

Other comparison symbols are:

● < (smaller then)


● <= (smaller or equal then)
● > (greather then)
● >= (greather or equal then)
● == (equals)
● != (not equal)

Introducere in IT 20
Comparing variable values

Multiple comparison can be changed together using boolean operators.

AND operator
A B C (A AND B)
false false false
false true false
true false false
true true true

Sometimes AND is represented with “&&”

Introducere in IT 21
Comparing variable values

OR operator:
A B C (A OR B)
false false false
false true true
true false true
true true true

Sometimes OR is represented with ||

Introducere in IT 22
Comparing variable values

NOT operator:
A B (NOT A)
false true
true false

Sometimes NOT is represented with !

Introducere in IT 23
Comparing variable values

Example:
a=3
b=a+1
c = NOT(a == b) AND (b > (a -1))

Steps taken by the computing machine for the example above:


1. a = 3
2. b = 4
3. c = NOT(3 == 4) AND (4 > (3-1))
4. c = NOT(false) AND (4 > (2))
5. c = true AND true
6. c = true

Introducere in IT 24
Introducere in IT

Thank you!

Introducere in IT 25

You might also like