DEFINING AND NAMING VARIABLES

Each variable in a program needs to be defined only once. 

Notice how I included the letter “i” at the beginning of the variable’s name to signify that variable is of an integer value. This simple trick will always tell you what type each variable is. It will also prevent you from accidentally confusing language’s commands with variable names.

Every programming language has a strictly defined set of commands. These commands cannot be used for anything else except to give instructions to the program. For a similar reason we don’t give people names like “WALK”, “RUN”, “SIT” or “JUMP”.

Besides int, another type of numeric variable is float or double which represents all numbers with a decimal point.

Some languages do not require that you define a type like int. They automatically detect that you have assigned an integer value to a variable. 

Let’s consider what happens if we divide our two integer variables: 

double dResult = 0.0

dResult =  iFirstNumber / iSecondNumber 

The result of dividing 3 by 5 is 0.6 which is a decimal number:

To store this value, we had to define a variable as non-integer, otherwise the value 0.6 couldn’t be assigned to it.

Again, notice how I prefixed variable dResult with a letter d which signals to us that we are using this variable to store a double-precision number. The computer doesn’t really care about our naming conventions as long as we follow rules for naming variables, but for us, these little descriptors can be very helpful when reading the code.

  • Here is a good moment to address the term “bug” in the program.  In the example above, when we divide iFirstNumber with the iSecondNumber number, we expect the result to be 0.6. However, some languages and compilers treat this as an “integer division” and discard the decimal part. This can introduce an error in the program called a “bug”. It might go unnoticed if the program is not thoroughly tested. We can fix this particular bug as defining at least one of the numbers as type double. Multiplying iSecondNumber with 1.0 will also work and the third option is to “type-cast” the variable like this double(iSecondNumber).

Another important variable type is string.

string sFirstWord = “Hello!”

print sFirstWord 

Strings handle everything that has to do with characters and words.

You can store numbers as strings, but they will be treated as text, not as numeric values. 

character type is defined as a string variable of length 1.

char c = “A”

This type is useful when we write a program with a requirement to run in a limited amount of memory.

A special type of variable is Boolean which can take only values true or false.

We already used a Boolean variable in our example to determine whether a given day is sunny.

Using our type descriptor syntax, we can rename this variable to bSunny to be able to easily tell that variable is of type Boolean.

Variables can have a different scopes in a program. They can be local to a specific part or global to an entire program.

Constants are variables whose value doesn’t change. Often they are global to the entire program and the compiler will warn you if you try to change its value.

const int DAYHOURS = 24

Next page ->
COMPARING VALUES

Download “Become a Programmer in a Day” on Apple Books

Leave a comment

Your email address will not be published.