Never confuse education with intelligence, you can have a PhD and still be an idiot.
- Richard Feynman -



Variables and types

From Juneday education
Jump to: navigation, search

Computer introduction

NOTE_TO_SELVES:Why is this header named Computer introduction?

Meta information about this chapter

Expand using link to the right to see the full content.

Introduction

Variables is a basic building block when developing software in programming languages such as Java and C. In these languages it is important to have a good understanding of what a variable is and how it relates to memory - both as a way to understand how a computer program works and when learning about pointer or references later on.

We need to give the computer a bit of help on how to interpret the variable. We do this by using the type concept, which tells how big the variable is (how much space in memory is used) and how to use the memory for a variable.

It is not enough having variables and types. We, of course, need use them to store values and later on retrieve values. In this lecture we go through how to initialize and assign variables values.

Being able to manage conversion between type, automatic and explicit, are important for a developer. This lecture gives an overview and introduction to type cast.


Purpose

To give the student a basic understanding of variables, types and type cast. This understanding will be used as a basis for future learning.

Types are used in many languages, to different degrees. Being familiar with the type concept is important regardless of language and this presentation gives the student a foundation for future studies.

Declaring variables is done all over the source code of imperative programs (since the only way to create a variable is to declare it).

Without assignment there would not be much a program written in languages such as C, C++ and Java could do. The concept is relatively easy to understand and use but important enough for a separate lecture.


Requirements

The student should be familiar with memory.

Goal

The goal of this presentation is to motivate and make the student familiar with the variable concept as well as discuss the way we humans use variables in every day life.

The student shall be able to .

The student shall be able:

  • motivate why variables are needed
  • use variables
  • use and reflect over the type concept
  • convert/cast between different types
  • understand the meaning of an assignment
  • know that the = sign does NOT refer to mathematical equality
  • understand how to use assignments with typed variables
  • use assignment when writing programs
  • be familiar with the type cast concept
  • know the implications of switching between types
  • be able to convert between types


Concepts

Instructions to the teacher

Common problems

Variables are very important to understand. Without this understanding we have found it hard for the students to:

  • to understand functions and their parameter/argument list
  • pointers and reference variables


Videos

All videos in this chapter:

See below for individual links to the videos.

Variables

Description

Exercises

  1. We have a variable nrOfGirls that has the value 14. We also have a variable called nrOfBoys with the value 13. If you were asked to add the values of the two variables together, what would be the result?
  2. Assume a person knows the address of the memory where nrOfGirls is stored. This person writes the value 123 in that memory. What would be the sum of nrOfGirls and nrOfBoys if you were to add them together again?


Solutions

Expand using link to the right to see the full content.

  1. 27. The nrOfGirls has the value 13 and nrOfBoys has the value 14. Adding the values would be adding 13 and 14, which is 27.
  2. 137. The nrOfGirls now has the value 123 and nrOfBoys has the value 14 Adding the values would be adding 123 and 14, which is 137.


Presentation

Presentation: (pdf)

Videos

Variable (eng) (swe)

Links

No further reading is required.

Types

Description

Exercises

  1. If you want to count the number of goals in a game of handball, what would be a good type for a variable?
  2. If you want to store the name of a person in a variable, what would be a good type for that name?
  3. You want to store the mean value of the results on an exam. Can you recommend any type?
  4. Add an integer variable with the value 1 with another integer variable with the value. What is the resulting sum?

Solutions

Expand using link to the right to see the full content.


  1. Some kind of integer variable. Decimals are not needed - it wouldn't make sense to keep track of a fraction of a goal - e g 1/3 of a goal. The authors do not know of any game that has ended 27.25 - 23. So to keep things simple and to skip the unnecessary, let's use an integer. In Java and C you can use int. Some languages also support unsigned types, which can only be 0 or high. Such a type would be even better since the value (the number of goals) can not be negative.
  2. String. Note: Text is handled different in programming languages. As an example, the concept of String in the C programming language the concept of String is completely different

from Java. However, in this exercise we can use an idea of a String type as a type storing text.

  1. It's not unlikely that we'll end up with decimals so a type that supports that would be good. SO let's say a double
  2. Adding 1 and 1 is simple. The result is 2.

Presentation

Presentation: (pdf)

Videos

Types (eng) (swe)

Links

Declaration

Description

Exercises

  1. Declare an int variable called nrStudent
  2. Imagine we have two variables with exactly the same name. What would be meaning of that? If you find this question hard to answer you're most likely thinking correctly. Don't spend too much time on this question.


Solutions

Expand using link to the right to see the full content.

  1. int nrStudent
  2. If it would be possible to declare two variables in the same scope, which is not possible in the languages known by the authors of these lectures and exercises, it is hard to come up with a reasonable interpretation of what it means to use one of the two variables. This is the reason why it is not allowed to use the same variable name twice. At least for now... So the meaning of this would be ... well, hard to come up with.

Presentation

Presentation: (pdf)

Videos

Declaration (eng) (swe)

Links

Wikipedia links for the keen student:


Assignment

Description

Exercises

  1. Declare an integer variable, temperature, and assign it the value 13
  2. Now, the declaration and assignment should be done on the same line.
  3. After the code in (1), assign the value 31 to the temperature variable.
  4. So far we have assigned values to variables. Is that all we can do? No, of course not. But we haven't learned so much about things like functions, expressions and so on. Still we will ask you to assign a calculated value to a variable. Let's first start with agreeing on the fact that 10 + 3 equals 13. If you don't agree we ask you to go to your teacher.. Declare an integer variable, temperature, and assign it the value 12. Declaration and assignment should be done on the same line.
  5. After the code in the previous exercise, assign the temperature variable 10 + 3
  6. Declare an integer variable, count, on one line and assign it the value 12
  7. After the code in the previous exercise, assign the value 31 to the count variable.
  8. Let's use the variable itself on the right side of the assignment operator (=). Assign the variable count the value of itself (the current value) + 10,
    If we as an example say x=x+10 the system starts by checking the right side (x + 10) and calculates (or evaluates) that. If we assume that x had the value 13, then the right side will become 23 since 13 + 10 is 23. After this is done the system will continue by assigning that value, 23, to the x variable.
  9. Let's assume we have two variables as below:
      int nrGirls =  13;
    
      int nrBoys  =  15;
    
      int nrStudents;
    
    Assign nrStudents the sum of the nrGirls and nrBoys. You should let the computer do the calculation using the +.

Solutions

Expand using link to the right to see the full content.

  1. Let's declare an integer variable. The first thing we need to is to specify the type. In this case we were asked to declare an integer so let's use the built in integer type in C and Java. It's called int. The next thing we should specify is the name we want to use, let's go for temperature. Ok, finally we should assign the variable a value, to be more specific 13. So let's use the assignment operator. And as always C and Java wants you to be explicit and specify when your statement is finished. We do this with a ;
      int temperature;
    
      temperature = 13;
    
  2. Let's do the declaration and assignment in one line:
      int temperature = 13;
    
  3. The above is really the same as (1). We're asked to assign a new value. We do this in the same way as in (1), but since the variable temperature is already declared we don't, fact is we can not, again so we're skipping the declaration.
      int temperature =  13;
    
      temperature = 31;
    
  4. This is something we've done before, so let's simply write.
      int temperature =  12;
    
  5. Ok, time to practice on using the + (which is an [operator], more on this later).
      int temperature =  10 + 3;
    
    In the code above, the right hand side (10 + 3) calculated first. After the calculation the assignment is done. The variable now has the value 13.
  6. This should be fairly easy by now:
      int count = 12;
    
  7. Ok , let's assign the variable a new value:
     count = 31;
    
  8. Ok, now we're going to have some fun. If someone asked you to add 3 to the digit written on a post-it note you would have no problem doing that. That's what we're doing on the right side of the assignment operator. And then if someone asked to replace the value on the post it note with the newly calculated sun you'd probably have no problem doing that either, assuming you have a pen. So let's do this in code. Languages such as C and Java starts of by calculating the sum on the right side, just as we did in the example with the post it note. And then the sum is assigned to the variable `temperature`.
     temperature = temperature + 10 ;
    
  9.   nrStudents = nrGirls + nrBoys ;
    
    Again, the right hand side of the assignment operator (=) is calculated before the assignment so the sum of of nrGirls (which has the value 15) and nrBoys (which has the value 13) is calculated to 28 and then the nrStudents is assigned that value (28).

Presentation

Presentation: (pdf)

Videos

Assignment (eng) (swe)

Links

Wikipedia links for the keen student:


Type cast

Description

Exercises

Throughout the following exercises it is assumed that we have the following types present in our language.

      byte -128 through +127
      int  -2 147 483 648 through +2 147 483 647
  1. Will the following code work?
      byte nrStudents;
    
      nrStudents = 123;
    
  2. Will the following code work?
       byte nrStudents;
    
       nrStudents = 789;
    
  3. Will the following code work?
       int nrStudents;
    
       nrStudents = 123;
    
  4. Will the following code work?
       int nrStudents;
    
       nrStudents = 789;
    
  5. Will the following code work?
       int maleStudents = 12;
    
       int femaleStudents = 12;
    
       int nrStudents;
    
       byte allStudents;
    
       nrStudents = maleStudents + femaleStudents;
    
       allStudents = nrStudents;
    
  6. Make an explicit type cast to tell the compiler you're ok with the conversion.
  7. Will the following code work? This is a question which actually requires a bit of thinking :)
       int maleStudents = 12;
    
       int femaleStudents = 12;
    
       int nrStudents;
    
       nrStudents = maleStudents + femaleStudents;
    
  8. Will the following code work? This is a question which actually requires a bit of thinking :)
       int maleStudents = 2147483647;
    
       int femaleStudents = 2147483647;
    
       int nrStudents;
    
       nrStudents = maleStudents + femaleStudents;
    


Solutions

Expand using link to the right to see the full content.

  1. Yes
  2. No. 789 is a bigger value than a byte can store. The compilers in some languages allow this, but a so called wrap around will occur.
  3. Yes
  4. Yes
  5. No. allStudents is a byte and storing an int (or the sum of of two ints) is not possible. Adding an int value to an int value might also be problematic if the sum is higher than the max value of an int or smaller than the min value of an int.
  6. allStudents = (byte) nrStudents
  7. It will work. When the program executes the sum will be calculated to 24, which is possible to store in an int.
  8. It will NOT work. When the program executes the sum will be calculated to a value bigger than the max value of an int.


Presentation

Presentation: (pdf)

Videos

Typecast (eng) (swe)

Links

Wikipedia links: