if...else Program Control

Of course isdigit is no use to us unless we can make the program go one way if the return value is true, and another way if it is false. The way this is done is by means of an if...else construction. This is used by placing a condition which must be satisfied, in parentheses, after an if statement, much as we did for a while statement. Then we follow this by a set of statements in curly braces which will be executed if the condition is met. Again this is similar to what we did with the while statement. Following this, we can have an optional else statement followed by a further set of statements in curly braces, which will be executed if the condition was not met.

It is time to put everything together and see how this works. Here is a code fragment giving a basic outline of our calculate function:

void calculate(char * inString)
{
    char * strPointer = inString;
    int numLength;
	
    while (strPointer[0] != '\0')
    {
        if (isdigit(strPointer[0]))
        {
            numLength = strspn(strPointer,"0123456789");
            //process a number
            strPointer+= numLength;
        } else
        {
            //process a non-digit
            strPointer++;
        }
    }
	
    return;
}

Firstly we have defined our pointer which will keep track of where we are up to in the string. At first we simply set it to point to the start on our input string, by setting it equal to instring which already points there. Next we define an int to hold a length. We use this with the strspn function to record the number of characters which follow that are not non-digits, i.e. that are digits.

Notice how our calculate function looks like a mini program, even having its own variables strPointer and numLength, which are only accessible inside the function itself. These variables are called local variables and the technical term for that part of the program for which they are accessible is their scope.

The while loop keeps looping until the final carriage return is located and inside the loop is our if...else construct. In order to pass an actual character to isdigit notice that we do not set its parameter equal to strPointer, which is just a pointer to a character, but we set it equal to strPointer[0] which gives us the actual character at position 0 of this array. (Note that array locations in C are numbered starting from 0 not 1.)

All our function actually does so far is set the variable numLength equal to the length of the number at the current position within the string (if isdigit returned true indicating that a digit was found at that position) and it moves the pointer along further in the string by adding numLength to it (if a number was found) or by adding 1 to it (otherwise). Notice the strange way that we increment strPointer. The two notations used are just C shorthands for the much more self explanatory but redundant

strPointer = strPointer + numLength;

and

strPointer = strPointer + 1;

respectively.

In the next lesson, we will fill in the parts that are covered by comments for now.