Logout

4.3.8

Analyse the use of variables, constants and operators in algorithms.

Teaching Note:

For example, identify and justify the use of a constant as opposed to a variable in a given situation.

MYP Mathematics: forms of numbers, algebra—patterns and sequences, logic, algorithms.

Sample Question:

sdfsdfsf

JSR Notes:

From the Teaching Note, a constant should be used when we don't want the value to change, or indeed if a change in the variable would potentially cause a problem. But keep in mind that there are two senses of the word "constant", both the "constant variable" sense, and a "constant" in a general mathematical sense, i.e. just a "raw" number. (Look back to 4.3.6.)

Both senses are good to consider, with the view of better appreciating how and when variables should be used, by contrast.

The Use of Constants

Use of constants: the Mathematical sense of "constant"

So with "constant" numbers, the first thing is to appreciate that they are to be taken for what they are, in their non-changing state. If we use the number 274 in a line of code, each time we get to that line of code, it is 274 which is used. It's value, thus, is said to be constant.

Our analysis of why a particular number constant should be used would go something like this example: It makes sense to use the constant 274 in line 45 of our program, because that's the line where we multiply the price per seat times the number of seats in the theatre. And the number of seats in the theatre is a set value, i.e. constant. It therefore makes sense to use the constant value 274, rather than a variable.

```double ticketPrice = snr.nextDouble();
System.out.println("The maximum ticket sales is " + 274 * ticketPrice + ".");```

Compare this to a situation where we can add folding chairs to a theatre, and each time a program is run, the number of seats will potentially be different. In a case like that, a constant value would not make sense; we would have to use a variable to store the value.

Use of constants: the Variable sense of "constant"

Now consider the definition of constant, which is "variable whose value cannot change", with Java, via the reserved word final.

You may ask, if a constant variable cannot change, then why not just use the "raw" number. And that would do nicely if it was only used once. But the advantage of using a final constant instead is that if that value is used over and over again in a program, if it is changed once where it is declared, then all other instances of it are changed as well - you do not have to go through the whole program and edit other occurrences of it.

Let's take the example of MAXCLASSSIZE. Let's say there's a policy at the school that the maximum class size is 22. In that case all arrays, for example, can be made to reflect this. Like in the following lines.

```final MAXCLASSSIZE = 22; //if this is changed, all other places MAXCLASSSIZE is used also change
Student [] mathClass = new Student[MAXCLASSSIZE];
//and later on:
Student [] codingGamesClass = new Student[MAXCLASSSIZE];
//etc.
//etc.
//.......[MAXCLASSSIZE]........
//.......[MAXCLASSSIZE]........
//.......[MAXCLASSSIZE]........
//etc.
//etc.```

If later on, the School Board decides that 22 is too big for a good international school, and 20 is the absolute maximum allowed for next school year. Since the constant MAXCLASSSIZE was used throughout the program, and it is a variable (a constant variable), the value only has to be changed once, with the 22 being changed in the declaration line to 20, for example.

The Use of Variables

The first, obvious advantage, spoken to us by the name "variable" is that the value can indeed vary/change.

Also, as seen above, an advantage of using variables is that if you change the value one place in a program, the other places below in which it is used will reflect that change.

And there is one point not to overlook, variables, since they are named, make our code easier to understand. The number 32 as a constant could be the temperature outside or the number of students in the room, but if a variable called outsideTemperature is used to hold 32 one place, and a variable numStudentsInRoom is used to hold 32 in another part of the program, those variables outsideTemperature and numStudentsInRoom are meaningful and obvious.

So, summarized, the use of variables gives us the following benefits:

• the ability for a value to change/vary
• one change affects all other instance of where the value is used
• clear understanding of what the value is

For example, it makes sense to use the following two values not as constants (in either sense of the word), rather as variables:

```int outsideTemperature = 30;
int numStudentsInRoom = 30;
if(outsideTemperature < 32 && numStudentsInRoom > 22){
System.out.println("Have some students go outside and work on the picnic tables.");
}```

The Use of Operators

In terms of analyzing the use of operators, most of the time is straight-forward. You use + when you want to add two constants or variables together. You use - when you want to find the difference between two constants or variables, etc. But there are a couple of places where you need to be more thoughtful sometimes.

Division: / / and %

One situation to slow down your thinking is when doing division. If working with integers, remember that you can get (only) the whole number portion of the quotient with / and you can get (only) the remainder portion of the quotient with %. But, importantly, you do get the full truth of an integer division if you use both / and % in two separate lines.

And keep in mind that with real number division, many calculations, such as ones with repeating answers (such as 10.0 / 3) you actually do not get the full truth. Is the answer 3.3333, or 3.333333, it's actually neither, and impossible to express with a double.

Operator shortcuts: ++ += etc.

There is one other exception to the straight-forward selection of operators to use, both in terms of the problems it can cause, and the preferences of the IB. The ++ and -- and += and -= etc. operators can have unexpected results when combined in complex expressions. So it is actually better to not use them at all, outside of loops and simple counters.

***Note this for the answering of IBCS examination questions; even for simple counters, you should instead, use:

```counter = counter + 1;
```