Categories:

Flag Variables Defined and Uses

A flag variable, in its simplest form, is a variable you define to have one value until some condition is true, in which case you change the variable's value.  It is a variable you can use to control the flow of a function or statement, allowing you to check for certain conditions while your function progresses.

Here's a function I developed for an ongoing mathematics in JavaScript project.  It uses flag variables extensively, but it is quite long.  I'll break it down for you as we proceed through the lessons.

function ineq(arg1,sign,arg2) {
    var temp = 0
    var temp3 = 0
    if (arguments.length%2==1) {
        var temp2 = false
        } else {
        var temp2 = true
        }
    var ineqs = new Array()
    for (temp = 0; (temp2==true)&&(temp < arguments.length); temp++) {
        if (temp%2 == 0) {
            ineqs[temp] = arguments[temp]
            if (ineqs[temp][0]=="NaN") {
                temp2 = false
                }
            } else {
            ineqs[temp] = arguments[temp]
            temp2 = false 
            for (temp3 = 0; temp3 < INEQOP.length; temp3++) {
                if (INEQOP[temp3]==ineqs[temp]) {
                    temp2 = true
                    }
                }
            }
        }
    for (temp = 1; (temp < ineqs.length)&&(temp2 == true); temp+=2) {
        temp2 = false
        if ((ineqs[temp]==LT)&&(ineqs[temp-1].isLesser(ineqs[temp+1]))) {
            temp2 = true
            }
        if ((ineqs[temp]==EQ)&&(ineqs[temp-1].isEqual(ineqs[temp+1]))) {
            temp2 = true
            }
        if ((ineqs[temp]==GT)&&(ineqs[temp-1].isGreater(ineqs[temp+1]))) {
            temp2 = true
            }
        if ((ineqs[temp]==LTE)&&(!ineqs[temp-1].isGreater(ineqs[temp+1]))) {
            temp2 = true
            }
        if ((ineqs[temp]==IEQ)&&(!ineqs[temp-1].isEqual(ineqs[temp+1]))) {
            temp2 = true
            }
        if ((ineqs[temp]==GTE)&&(!ineqs[temp-1].isLesser(ineqs[temp+1]))) {
            temp2 = true
            }
        }
    return temp2
    }

That's a very ugly function, and rather strange. Note the temp2 variable, however. This variable is the one I use as a flag to indicate throughout the function whether or not to continue on. I have three basic uses of the temp2 variable.

  1. Evaluating a condition and setting the flag variable value appropriately:
    
        if (arguments.length%2==1) {
            var temp2 = false
            } else {
            var temp2 = true
            }
    
  2. Pre-emptively setting the flag variable, expecting it to change later:
    
                temp2 = false 
                for (temp3 = 0; temp3 < INEQOP.length; temp3++) {
                    if (INEQOP[temp3]==ineqs[temp]) {
                        temp2 = true
                        }
                    }
  3. Evaluating a condition of the flag variable as a condition for executing a set of statements:
for (temp = 0; (temp2==true)&&(temp < arguments.length); temp++) {

The idea is to use the flag variable mainly as a memory of other conditions which the function checks earlier in its execution. By using the flag, I can avoid lengthy and redundant statements. If I did not use this flag, the second line above would look like:

for (temp = 0; (temp < arguments.length)&&(arguments.length%2!=1); temp++) {

That would be logically correct for just the first pass through this for-loop. But imagine the second pass. I have many conditions to check, and without a flag variable, I must keep adding conditions to the for-loop. Since I cannot guarantee exactly what arguments.length will be, I must use a for-loop, but by the second time around without the flag, the for-loop must look like this:

for (temp = 0; (temp < arguments.length)&&(arguments.length%2!=1)&&(ineqs[0][0]!="NaN")&&...

Obviously, a flag variable is critical!!!

How should I validate a form or a function?