Results 1 to 6 of 6

Thread: Loop's Tutorial ( Mini One )

  1. #1
    Join Date
    Mar 2002

    Post Loop's Tutorial ( Mini One )

    There are basically 3 types of loops. For awhile, Try each of them for they have their uses. They are all outlined below.

    FOR - FOR loops are the most useful type, I believe. The layout is for(variable initialization, conditional, incrementing variable) It is very versatile, and the layout can be changed somewhat. Basically, the variable initialization allows you to either declare a variable and give it a value, or give a value to another variable. Second, the conditional statement. What it does is it says that while the conditional is true then it would do what in is in the body. Third, the incrementing variable section. It does not have to increment a variable. It can decrement, which is subtracting one, or it can perform various other manipulations on the variable.

    Ex. #include <iostream.h> //We only need one header file

    void main() //We always need this

    //The loop goes while x<100, and x ha // s one

    for(int x=0;x<100;x++)/*THE LOOP*/ //added to it every time the loops


    cout<<x<<endl; //Outputting x



    This program is a very simple example of a for loop. x is set to zero, while x is less than 100 do cout<<x<<endl; add 1 to x until the loop ends. Pretty simple to understand, but it is a very powerful loop, and much better than WHILE and DO WHILE loops.

    WHILE - WHILE loops are very simple, but not as useful as FOR loops. The basic structure is...WHILE(true) then do whatever is in the body. The truth could be x==1 or while(x!= 7) (x does not equal 7)

    Ex. #include <iostream.h> //We only need this header file

    void main() //Of course...

    { int x=0; //Don't forget to declare variables

    while(x<100) //While x is less than 100 do


    cout<<x<<endl; //Same output as the above loop

    x++; //Adds 1 to x every time it repeats



    This was another pretty simple example, but it is longer than the above FOR loop, showing why I like for better than while, though while is a very easy loop to use, so if you are having trouble then you can use it, but try to use for.

    DO WHILE - DO WHILE loops are useful for only things that want to loop at least once. Basically it goes DO { THIS } WHILE(TRUE) Now, it is your turn to try and do a loop! make a DO WHILE loop that does what the above programs do...output 0 to 99!

  2. #2
    Webius Designerous Indiginous
    Join Date
    Mar 2002
    South Florida
    Cool post. Gunna have to look into some more of this.

  3. #3
    The Iceman Cometh
    Join Date
    Aug 2001
    WHILE loops are very simple, but not as useful as FOR loops.
    This isn't quite true. Both FOR loops and WHILE loops are useful within their own realms. FOR loops, for instance, are terrific if you have a finite (predetermined) number of executions. This wouldn't work, for instance, in a menu driven program, which would exit when a certain menu command was entered. In this instance, a WHILE loop would be much more useful because it allows for the user to dynamically choose how many times the contents of the loop will be executed. Many of the programs I have written, either for school or within the industry, require that the user be able to choose the number of executions, therefore containing WHILE or DO-WHILE loops. Just my added input. Good (short) tutorial, though.


  4. #4
    Join Date
    Aug 2001
    Just make sure to quote the source....

  5. #5
    Wow Neg, how do you even find these things?

    Edit: Never mind... I found it too... google strikes again...

  6. #6
    2 recommendations for future versions.

    in your for loop, you should explain the difference between x++ and ++x.

    in your while loop, you should probably add in an explanation of the compound statements (Not, And, Or; !, &&, ||, respectively).

    Just a recommendation.

    ---Aku. Soku. Zan.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts