MIS 106
Introduction to Algorithm Design and Programming
Dr. Tuğçe Ballı
Department of Management Information Systems
Kadir Has University
Lecture 1
Variables and Control Structures
What are we going to learn?
This chapter explains the notion of a variable How Variables are used throughout a program
which is a fundamental part of mathematics, with various control structures such as if
problem solving and computer programming. statements as well as for and while loops.
Lets get started..
• When we use a function in a program, it is necessary to store the result somehow so that it can
be used later in the program.
• Remember a “standard” computer only evaluates one instruction at a time from your program. It
is similar to the idea of having only one hand to perform an operation.
• If you wanted to perform pourDrink() with one hand, you would probably need to expand on the
instructions by making use of the counter top to put things down occasionally so your hands are
free.
• Since a typical computer program has only one “hand” (i.e. processor), we have to make use of
“counter tops” (i.e. storage space in computers memory) to “put down” (i.e. store) the
intermediate values/objects, so the program can continue performing other operations.
Variables
• A variable is a location in the computers memory that stores a single piece of
data.
• A single algorithm or program can use many variables to store intermediate
results.
• Important: each variable must be given a unique name so that it can be identified
later.
• Consider our one-handed algorithm for getting a drink from the refrigerator.
Recall that we need to place both glass and possibly carton on the counter top
during the algorithm.
• Have a look at steps 6-14 both the glass and cartoon are sitting on the counter.
That should help us to see that we need two unique variables to store these
objects.
Variables
• For each variable we need to choose a
meaningful variable name so that we can refer
to it later.
• Each variable that we make will have its own
counter space on the counter top.
• When we create (or declare) a variable, we are
really reserving space for an object on the
counter top.
• And as with any reservation, we need to have a
name for the reservation … which corresponds
to the label (i.e., variable name).
Variable naming conventions
• Variable names can not contain special characters like *, $, + etc.
• You can use underscore _
• You can do camel casing: sideLength ,
• OR sidelength, side_length
• Have a look at this link for more information about naming conventions:
• https://curc.readthedocs.io/en/latest/programming/coding-best-practices.html
• Variable names can contain numbers, but they can not start with numbers
• Side1 -> correct!
• Side2 - > correct!
• 1side -> incorrect!
Variables
• When a variable’s space has been reserved -declared, usually there
is nothing yet in that space … just the label.
• The term null is often used to indicate that nothing has been stored in
the variable yet (i.e., noting is at that spot on the counter top). Once
we put something in the variable (i.e., on the counter top at that
label), the item that we place there is called the value of the
variable.
• When we declare a variable (i.e., each time we reserve space for
something), we are actually taking up space in the computer’s
memory. You may already know that your phone, your flash drive,
your computer etc… all have limited memory (or storage) space.
Variables
• The computer’s memory space is called RAM, which
stands for (Random Access Memory). Typical
computers have 16GB (roughly 16 billion bytes) of
storage.
• Consider, for example, a computer with 1GB of
storage.
• Each time we declare a variable, we are reserving
space in the RAM. That is, we are using up a portion
of the computer’s available memory.
• The bigger the object that we are storing, the more
space that it takes up. So, here, we see that the carton
would take up a little more space than a glass of
orange juice.
Variables
• The labels carton and drink are called references, since they are used
to refer to a particular object.
• The reference is actually just a number within the sequence of storage
bytes in the RAM. It is also known as a memory address, because it
“sort of” represents the “address” of the object, as a real life address
uniquely identifies a home in the real world.
• There are simple kinds of objects (such as numbers and letters) called
primitives that are stored in a simpler manner.
We will look at primitive data types in Python later on.
Variables
• As with any real counter top, we can alter at any time what we place
at that location on the counter.
• Similarly, we can change a variable’s value at any time but putting a
different value there. What happens to the old value? It simply
disappears.
• In real life the object at a specific spot on the counter does not
disappear when we put a new object there at the exact same
location.
• So variables in a computer are a little different than real life. When it
comes to replacing a variable’s value with a new value, it is easiest to
understand that process as over-writing the variable’s value.
• Notice that the drink variable represents either a can of soda, an empty
glass or a glass with lemonade or orange juice in it, depending on the line
of the program.
• The ← is used to indicate that something is to be stored in the variable
(i.e., that we want to give the variable a new value).
• Notice as well how go to the counter and put soda down on counter and
go to refrigerator are all combined into one storage step as drink ← soda.
• Similarly, the go to counter and pick up drink combination as well as
the go to counter and pick up carton combination is analogous to
simply getting the value of the variable in that location.
• The functions chooseDrink(), pourDrink() and getGlass() all bring
back (i.e., return) a drink, whether it is a full glass, an empty glass or
a soda can.
• The drink is returned (from each function) back to the function that
called it.
• The return value is the value returned as a result of the function.
• The idea of a return value becomes more obvious when the function
is mathematical. For example, sine(90) is naturally understood to
return the value 1 and squareRoot(100) would naturally return the
value 10.
• One more point to mention regarding the variables is with respect to
where (and how often) they are used.
• You will notice that the drink variable is used throughout the
algorithm, whereas the carton variable is used only within the
pourDrink() function.
• Variables that are used only with a single function/procedure (e.g.,
carton) are called local variables because they are only used locally
(i.e., within the a function).
• In contrast, variables that are used across many
functions/procedures (e.g., drink) are known as global variables.
• A parameter is a piece of data that is provided to a function.
• A parameter is similar to a variable because it is a value that is used in your
program
• A parameter is different, however, in that it usually represents a value that
remains constant within the context of where it is being used.
• In general, an algorithm may have many initial parameters and like variables they
are usually given names. So inside an algorithm or computer program a
parameter will usually look just like a variable. However, you should understand
that these parameters will not change throughout the execution of the
algorithm/program.
Example
• Bob and Steve went on a vacation together. During the trip Bob paid for all the food and for the
hotel. Steve paid for the gas and for the entertainment. Write an algorithm to compute the
amount of money that Bob owes Steve (or Steve owes Bob) after the trip, assuming that they
decided to split the expenses evenly ?
• How many algorithm parameters are there ? What are they ?
50 1. each = (f+h+g+2)/2 130
100 2. stevePaid = g+e --> 110
100 3. bobPaid = f+h 150
10 4. difference = each – stevePaid 20
5. if(difference<0)
bob has to pay steve abs(difference)
6. else
steve has to pay bob difference
Abs: absolute value Turkish: mutlak deger
Example
• There are n kids in a room. As it turns out, some kids have socks on (with or
without shoes), some kids are wearing shoes (with or without socks), and some
kids are wearing both socks & shoes. Develop a computational model & algorithm
to determine how many kids are barefoot?
n=20
x=5
y=10
z=3
socksOnly = 5-3 =2
shoesOnly = 10-3 = 7
20 – 3 – 2 – 7 = 8
Example
• A team of n people work together painting houses for the summer. For each house they paint
they get $256.00. If the people work for 4 months of summer and their expenses are $152.00 per
month, how many houses must they paint for each of them to have one thousand dollars at the
end of the summer?
n=10
goal= 10000 + 608
houses= goal / 256
• Notice that the 1000, 4, 152 and 256 here are all fixed/constant values, as opposed to parameters.
• A constant is a single piece of data that does not change throughout the algorithm
• Assume that you are given a name of a person in one of the following formats:
• “Firstname Lastname”
• “Lastname, Firstname”
• “Firstname MiddleInitial. LastName”
• “Lastname, Firstname MiddleInitial.”
• You want to develop an algorithm that will determine whether or not the name is properly formatted to one of these formats.
• The output should be “YES” if properly formatted, otherwise “NO”.
• How would you write the algorithm ? Assume that the name is the only incoming parameter in the form of a bunch of consecutive characters.
In Class Exercise
• First of all we will create a notebook in Jupyter or Google colab.
• We will have a look at how to write a piece of code and run it.
• Then we will implement the examples in slides 18-19-20.
Lab Example 1:
Write a pseudocode for calculating the area of a rectangle.
Convert your pseudocode into a Python code.
• Try hardcoding values of two sides.
In Class Exercise
Lab Example 2:
Write a pseudocode for calculating the area and circumference of a circle.
Convert your pseudocode into a Python code.
• Try hardcoding value of radius.
In Class Exercise
Inputs: Length of side 1, Length of side 2
Output: Area of rectangle
Things we need to know or find out: formula of calculating area of rectangle
area = side1 * side2
CalculateArea(side1, side2)
1. area = side1 * side2
2. print area
In Class Exercise
Inputs: radius, pi
Output: Area and circumference of circle
Things we need to know or find out: formula of calculating area and circumference of circle
area = pi * radius * radius
circumference = 2 * pi * radius
CalculateAreaAndCircumference(radius, pi)
1. area = pi * radius * radius
2. circumference = 2 * pi * radius
3. print area
4. print circumference
• In each of the above examples, the parameters and variables are all numbers.
When programming, however, sometimes the variables and parameters may be
of a different nature. For example, sometimes the input to an algorithm may be in
the form of text, such as a person’s name or an address. Or perhaps the variables
come in the form of yes/no answers (i.e., true/false).