Wednesday, January 9, 2013

Loops

Loops

C gives you a choice of three types of loop, while, do while and for.
  • The while loop keeps repeating an action until an associated test returns false. This is useful where the programmer does not know in advance how many times the loop will be traversed.
  • The do while loops is similar, but the test occurs after the loop body is executed. This ensures that the loop body is run at least once.
  • The for loop is frequently used, usually where the loop will be traversed a fixed number of times. It is very flexible, and novice programmers should take care not to abuse the power it offers.
  • The while Loop

    The while loop repeats a statement until the test at the top proves false.
    As an example, here is a function to return the length of a string.  Remember that the string is represented as an array of characters terminated by a null character '\0'.

    
    int string_length(char string[])
    {       int i = 0;
    
            while (string[i] != '\0')
                    i++;
    
            return(i);
    }
    The string is passed to the function as an argument. The size of the array is not specified, the function will work for a string of any size.The while loop is used to look at the characters in the string one at a time until the null character is found. Then the loop is exited and the index of the null is returned. While the character isn't null, the index is incremented and the test is repeated.

    The do while Loop:

    This is very similar to the while loop except that the test occurs at the end of the loop body. This guarantees that the loop is executed at least once before continuing. Such a setup is frequently used where data is to be read. The test then verifies the data, and loops back to read again if it was unacceptable.
    
    do
    {       printf("Enter 1 for yes, 0 for no :");
            scanf("%d", &input_value);
    } while (input_value != 1 && input_value != 0)

    The for Loop:

    The for loop works well where the number of iterations of the loop is known before the loop is entered. The head of the loop consists of three parts separated by semicolons.
    • The first is run before the loop is entered. This is usually the initialisation of the loop variable.
    • The second is a test, the loop is exited when this returns false.
    • The third is a statement to be run every time the loop body is completed. This is usually an increment of the loop counter.
    The example is a function which calculates the average of the numbers stored in an array. The function takes the array and the number of elements as arguments.
    
    float average(float array[], int count)
    {       float total = 0.0;
            int i;
    
            for(i = 0; i < count; i++)
                    total += array[i];
    
            return(total / count);
    }
    The for loop ensures that the correct number of array elements are added up before calculating the average.
    The three statements at the head of a for loop usually do just one thing each, however any of them can be left blank. A blank first or last statement will mean no initialisation or running increment. A blank comparison statement will always be treated as true. This will cause the loop to run indefinitely unless interrupted by some other means. This might be a return or a break statement.
    It is also possible to squeeze several statements into the first or third position, separating them with commas. This allows a loop with more than one controlling variable. The example below illustrates the definition of such a loop, with variables hi and lo starting at 100 and 0 respectively and converging.
    
    for (hi = 100, lo = 0; hi >= lo; hi--, lo++)
    The for loop is extremely flexible and allows many types of program behaviour to be specified simply and quickly.

0 comments:

Post a Comment

Powered by Blogger.