Secret Code

Variables and Conditional Statements

Oliver Higgins

Issue 2, August 2017

Last month we introduced the overview of code through the “Hello World” article. This month we will take a look and explain variables and conditional statements. Without these two things, our lives would be boring and automated. They are 2 of the most fundamental things in programming for they allow us to do something. We struggled to come up with any program that did not contain at least one of each of these. Okay, “Hello World” is one, kind of.

Let’s start with variables. This is probably a poor descriptor if you have not done any programming before. Simply put, a program needs to do something. It needs to somehow get information from the outside world and look at it, manipulate it, and then give it back to the user. It requires somewhere to hold this information.

If we are to step back a minute and look at the world around us, I have a coffee cup in front of me. It is canary yellow. (Actually it’s Big Bird yellow). The only thing I am defining in the context of the coffee cup is its colour. At home, I have a blue one I bought from the same shop. It is blue, (Cookie Monster blue, to be precise). Everything else about these coffee cups is the same. Now it may seem as though I am spelling this out, but hear me out.

A NOTE ABOUT SPELLING: You’ll notice that in Australia, we use British English, which spells “colour” with a “u”. Since code was developed with US-English spelling, all code languages (that we’re aware of) will drop the “u” and spell it “color”. This applies to web languages such as HTML / CSS / PHP which regularly deal with colour, as well as C, Python, Java. We may use “colour” in the text, but if it’s in the code, we’ll always use “color” to avoid confusion. Unfortunately code interpreters don’t yet have artificial intelligence, so will rarely assume “colour” equals “color”.

We need to write a program that makes coffee cups. The users have informed us that in addition to yellow and blue they would like red ones. So the program for this is pretty simple:

  • Start program
  • User enters coffee cup colour
  • Coffee cup appears

This program is quite simple and has one variable being the colour. When we program, we need to have a place to define and hold this information. We commonly refer to this as a “variable”. A variable is something that we – the user or the program – can change during the program’s execution. In the example below, we define the variable “color” and then ask the user to enter the colour that they would like.

Python example (you can run in IDLE):

  color=input("Enter the color you would like: ")
  print(color)  

This code takes a “string” or a set of characters from the user, and stores it in the variable named “color”. We can then recall the contents of the variable at any time, by referencing the name that we gave it – in this case, “color”.

DIFFERENT TYPES OF VARIABLES

Let’s take a moment to look at the common variables you will use. Depending on your chosen language, the declaration and instantiation will be different. Some languages will figure out what you want by what data you put in it the first time, where in others you may have to be explicit.

STRING/CHAR

“Hello World” This is the string that we discussed last month. Put simply it’s a string of characters. Depending on the system the string variable itself is made up of a sequence of single characters. These are referred to as a CHAR. Standard C is explicit in defining an array of CHARs, as opposed to a string (although modern compilers including Arduino’s Processing and Python allow a string).

A string is one of the most common variables you will use. This is how we will give real-world feedback to the users; for example, “Hello” in the example above. The only limitation on this array is the available memory on your chosen processor.

INTEGER, LONG AND BYTE

1 or 73 or 42 or 7854 or -598. These are all integers. They are whole numbers and are often referred to as counting numbers. They are used for counters, loops and basic data elements. They occupy 16 bits and are used to store whole numbers from -32768 to 32767. If your numbers exceed this range, then you will need to use a long.

A long is an integer that occupies 32 bits and, as such, can hold a much greater number range (please note: I am sticking with the general rule of ANSI C. Most modern languages are a minimum of twice this size for a long).

A byte stores an 8-bit unsigned number, from 0 to 255. This is often used for storing things like colour codes. If your memory is at a premium in your project, you should consider if your number will exceed this.

FLOAT AND DOUBLE

In simplest terms, a float is a number that is a fraction. Variables declared of type float will have numbers on both sides of the decimal point. The term “float” is short for “floating point”. Internally floating point numbers are expressed as fractions of base 2 math, and can represent values ranging from approximately 1.5 × 10−45 to 3.4 × 1038 with a precision of seven digits. This is defined as single precision and occupies 32 bits of the system memory. Modern systems will allow you to simply define a float, to store any number with that requires a decimal; however, if your function requires more precision I would consult your languages guide.

A NOTE ABOUT VARIABLES: Don’t waste space. One thing we repeatedly see in code is the programmer using strings when they do not need to. It is much faster and uses less memory to use the correct variable type. The best example I have seen is where somebody declared a string to be used for “yes” or “no”. While this may have meaning for the programmer or an end-user, they were using it to compare an “IF” statement. A 3-letter string will increase the UNO’s global usage by 14 bytes, and its sketch memory by 4%. In addition to this memory waste, it will make using the comparison much slower, as the processes need to evaluate so much more data. In many cases, a basic integer or even a BYTE will suffice.

Double is a very similar construct to float, but it occupies 64 bits so offers much greater decimal precision.

Declarations

Now depending on your language you will need to declare the variable. In C you must be explicit. That is you must tell.

  Arduino
  int x;  
  Python
  x=0  

BEST PRACTICES FOR NAMING YOUR VARIABLES

  1. Declare your variables with data if possible.
  2. Use meaningful names such as Xpos or userName, not XX4 and y3e. A variable name should define its use.
  3. Avoid system keywords such as date, time, equals or data.
  4. Don’t reuse variables within the same functions or class.
  5. Don’t use non-ASCII chars in variable names. It may work fine now, but may crash on other systems.
  6. Don’t use long names. Seriously you have to retype this...
  7. Stick to your naming conventions and case (i.e. don’t use UserName, userName, username, USERNAME). Many languages are case sensitive so this will lead to debugging issues.
  8. Declare it before you use it. This may seem simple, but you would be surprised how often this error happens.

VARIABLE SCOPE

So does it matter where you declare a variable? Yeah, it sure does! There are two important things you need to be mindful of. Firstly, you must declare a variable before you use it (kind of). Secondly, when you declare a variable makes a difference as to how it is accessible in the program. Take this example in C/Arduino:

C/Processing/Arduino
int x = 0; 
void setup() { 
 int y = 0; 
 x++; 
  }
  
void loop() { 
 int z = 0; 
 x++; 

The first declaration is for an integer called X. This is a global variable and can be accessed and modified from any point in the program. Y is declared in the setup routine. It is only in existence while this is executing, we then increment the X variable by 1. Once complete, Y ceases to exist and is gone, along with its contents. Z is declared in the loop routine. At this stage, we can access X as it is global to the loop, but Y has since been dumped. If we were to print X here, it would be 1. The X++ line increments the X variable, even though it was not declared in this scope. If we tried to call Z from setup or Y from the loop, the program would crash, but we can use X at any time.

As a general rule, variable declarations should be not null. If you know what you are going to put in it (i.e. a number), put 0 in before you use the variable. This will ensure that you do not get any stray data. Not all compilers clean up after themselves, so they may just allocate a new piece of memory for you when you declare the variable. If you read back, the variable before is used, so it is not uncommon to find random numbers. Primarily this happens in C, and while it will probably not be an issue for you, best practice will ensure it does not happen.

CONDITIONAL STATEMENTS

We are all individuals. I’m not.

We need to make decisions, and even the simplest program will need to evaluate some factors. The “IF” statement is the simplest form of a conditional statement. Think of it as simply asking the question; in our early example, we could ask the computer to do something based on the colour of the cup.

The English for this would be “if the colour is blue then tell the user, they are awesome.”

PSEUDO CODE:

If Color = blue then
   Print "You are awesome"

PYTHON:

if Color == "Blue":
   print("You are awesome")

ARDUINO:

if(Color=="Blue"){
  Serial.printIn("You are aweome:);
  }

So what if we need to do something, if it’s not blue? We have a second part of the statement called “ELSE”. This allows the programmer to send the code direction elsewhere, based upon the conditions we have put in front of it.

The English for this would be, “if the colour is blue, then tell the user they are awesome. Otherwise tell the user that they could be more awesome had they chosen blue.”

PSEUDO CODE:

If Color = blue then
 Print "You are awesome"
Else
 Print "You could be more awesome had you chosen blue."

PYTHON:

if Color == "Blue":
 print("You are awesome")
else:
 print("You could be more awesome had you chosen blue.")

ARDUINO:

if(Color=="Blue"){
  Serial.printIn("You are awesome");
}else{
  Serial.printIn("You could be more awesome had you chosen blue.");
  }

Note the double equals sign in the “IF” statement. This means, we are comparing two things not assigning a value, as we did earlier with the variables.

Understanding Binary

To better understand how variables work, you need to understand some basic binary function. Digital systems (Micros/PCs, etc.) store their information in a binary format. It is expressed in 0s and 1s. This is base 2 math. As humans, we use base 10 math, which is much easier for us to view but very hard for a computer to store, express and process. A simple example would be if we want to store the number 4. 4 in base 10 is simply 4 (base 10 counts to ten, then continues 9-10-11); however, using base 2, 4 would be expressed as 100.

Each of these numbers presents a single bit. The first number on the far right-hand side is 1. Now is the bit switched on or off? Is it 1 or 0? In this case, it is off. We move on to the 2: is it 1 or 0? It’s 0, so we move on. Next, we have 4: is it 1 or 0? It’s 1, so we have a 4. Finally, we have 8, which is 0: so the only number is 4.

0 1 0 0
8 4 2 1

A more complex number would be 7:

0 1 1 1
8 4 2 1

How about 12:

1 1 0 0
8 4 2 1

The modern systems will use an 8-bit base 2 system, to store its information. This 8-bit number is referred to as a byte.

0 1 0 0 0 1 0 0
128 64 32 16 8 4 2 1

This will allow you to store 256 numbers (0-255)

Now you probably want to use numbers much bigger than this, so you can continue it out to a 2-byte number or 16-bit number. This will allow you to store a number as big as 65535. This is referred to as an “integer”. It is a whole number (i.e. no decimal points or fractions). To make it more useful, it uses a sign bit as a representation of positive or negative number. This results in the number range of -32768 to 32767.

So why is all this relevant? On modern computers with GBs of RAM, it problem doesn’t matter, but when you are working with a micro, such as the UNO or the Nano, then any memory saves will result in a major performance enhancement; be that through available RAM or processing time. It is much faster for the processor to process numbers than text because each text character occupies more space.