JRJC - How To Nest Loops

Very simple: put your second loop inside your first. Example:

    while ( conditionOne )
    {

        while ( conditionTwo )
        {

        }

    }

The first loop might be "open all files one at a time" and the second loop might be "copy each line of the file to the screen".

This can work with for loops too, although you must be careful with your variables! Since most for loops iterate with the variable i, many inner loops will use the variables j, k, etc. Example:


    for ( int i = 0 ; i < 100 ; i++ )
    {
        for ( int j = 0 ; j < 20 ; j++ )
        {

        }
    }


That's it!






© 2000 Paul Wheaton