Help with loops

This is a discussion on Help with loops within the C++ Programming forums, part of the General Programming Boards category; Ok, im new to c++ (started like 2 days ago on the tutorials on this website, so i can code ...

  1. #1
    ------------
    Join Date
    Jun 2005
    Posts
    79

    Help with loops

    Ok, im new to c++ (started like 2 days ago on the tutorials on this website, so i can code my own custom MUD codebase) and skipped it earlier because i didnt get it. now i tried to return and still dont get it. I was woundering if anyone could possably give a diffrent explaination on how to use it and/or what its used for? Any help would be greatly appreciated.
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  2. #2
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    Well, that's a very wide question. May be you can narrow it down a bit? What exactly don't you understand about loops?

    Or perhaps you should check out another tutorial? There are a plenty of those. Just try to google the subject.
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  3. #3
    ------------
    Join Date
    Jun 2005
    Posts
    79
    Ok, i'll try google-ing it. I get what it does (repeat a command until a certain thing is reached) but i dont know what it can be used for or what to do with it... i tried the practice programs on the tutorial and it reached whatever value i set in like 1/2 a second so it seems kind of pointless... I want to learn everything on the tutorials since if it is there, it has to have some kind of use. im just not finding the use for this one...
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  4. #4
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    There are a number of things you can use loops for. No program can survive without them. Some important areas of use that I can come up with right now:

    - change all numbers in an array (if you have like 2000 values, you just can't sit and do it by hand) according to some rule

    - print all the numbers in an array

    - main loop: process user input while he/she hasn't typed 'exit'

    there are of course thousands of other...
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  5. #5
    ------------
    Join Date
    Jun 2005
    Posts
    79
    Ok, i found a tutorial on google.com that so far is explaining it pretty well...
    http://richardbowles.tripod.com/cpp/cpp8.htm
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  6. #6
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    Great
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  7. #7
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    You'll be using loops (while, for and if) a lot in your MUD. You know the multiple choice questions, like "1. go exploring, 2. go to the bank, 3. go home" and "1. attack, 2. run away, 3. view inventory".. those are done with a 'while' loop, using case situations (explained somewhere in the tutorial). Then you would use 'if' to get situations, like if(level < 10), which is just a situation. So heres an incrementing example for a 'for' loop.. calculating experience: you would need something like

    Code:
    for( int x = 0; x <= AddedExperience; x++) { TotalExperience + x; }
    .. and the reason for adding the experience (you say got for killing a rat) 1 by 1 is so that when you hit the experienced needed to get your next level you get it automatically instead of skipping right by it and losing any extra experience. I always found the experience calculating system to be complex to explain, so I hope that wasnt confusing. Theres no doubt you'll need them because if you ever want an efficient random map generator you'll be using loops (as well as a lot of other things you prob havent gotten to). In games you might notice there are multiple loops going on, even at the same time (patch installations, any install process, loading screens, fps calculators, x and y rotation display, clock, and various background things).
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  8. #8
    ------------
    Join Date
    Jun 2005
    Posts
    79
    1 more question... why is the "goto" command not good to use anymore? I used it in BASIC (while trying to code a game for my calculator) and a website said it was bad to use... seems like using
    Code:
    if ( x <= 4 ) {
    goto label;
    }
    is a lot easier than using a loop...
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  9. #9
    ------------
    Join Date
    Jun 2005
    Posts
    79
    ok, your explaination wasnt confusing (suprising enough for me... im confused by a lot...) and now it makes sense i guess... im just anxious to learn and get comfortable with c++ so i can start on the mud .
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  10. #10
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    Using goto can cause several problems, for example you can by accident jump over variable declaration, can get problems with memory allocation etc.


    if ( x <= 4 ) {
    goto label;
    }

    Why not just do:

    Code:
    if(x<=4)
    {
    //the code you wanted to have after the label here
    }
    Don't need to use loops at all.
    Last edited by MathFan; 06-21-2005 at 02:27 PM.
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  11. #11
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    I never used goto, but I've read 3 tutorials on why not to use it. It basicly just says its sloppy to use because if you have too many it gets disorganized and hard to edit, and considering goto isnt necessary to use thats not a good thing. Also with C++ its not necessary to use it AT ALL, but there might be unique cases where it would be better to use it.

    http://cprogramming.com/tutorial/goto.html

    The question should be not "do you have to use it" but "is it the best choice" to use it.
    I still dont know the answer. (found no use for goto yet) :P
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  12. #12
    ------------
    Join Date
    Jun 2005
    Posts
    79
    well... i guess that was a bad example of what i was asking. guess ill just practice with loops until i understand them and they get easier... thanks for the help everyone
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  13. #13
    ------------
    Join Date
    Jun 2005
    Posts
    79
    I GOT IT!!! yay!!! lol...

    Code:
    #include <iostream>
    
    using namespace std;
    int main()
    {
    int number; // declare an integer named number
    
    do // start  a command
    {
    cout<<"Pick a number 1-5: ";
    cin>>number // get number
    cin.ignore(); // ignore decimals and spaces
    }
    while ( number == 0 or number >= 6 ); // if number isn't zero or is over 5
    }
    is a simple code i wrote to test out the do-while loops! that one i can tell how it is helpful, now to write one for the other ones... and i just reolized i comment a lot... at least i wount forget what each thing does!
    Last edited by Goosie; 06-21-2005 at 07:20 PM. Reason: fixing code tag...
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

  14. #14
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Its great to keep it thoroughly commented.. it only helps, and it doesnt hurt

    Nice implimentation, I might go like this if you put error handling:

    Code:
      int number; // declare an integer named number
    
      while( number == 0 || number > 5 ) { // the symbols || is "or"
        cout << "Pick a number from 1-5: "; 
        cin >> number;
        cin.ignore();
    
        if( number == 0 || number > 5 ) {
          cout << "Error, incorrect choice: " << number;
        } else {
          cout << "Great! you chose: " << number << endl; 
        } // put an endl in there because youre wrapping back up there.
      }
    
      cin.get(); // used to hold the console window open
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  15. #15
    ------------
    Join Date
    Jun 2005
    Posts
    79
    Yea, i wasnt really thinking of errors... just trying to figure this out... but, what does endl do? wrapping back up? whats that mean?
    CAUTION: Newbie at programming!
    -------------------------------------------------
    Thanks everyone who helped me with all the questions, and utter lost-ness i happen to have... it is VERY VERY much appreciated

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 12:18 AM
  2. loops with incrementing strings .... ?
    By twomers in forum C++ Programming
    Replies: 1
    Last Post: 12-12-2005, 10:29 AM
  3. Evaluation of nested loops
    By Mister C in forum C Programming
    Replies: 2
    Last Post: 08-13-2004, 01:47 PM
  4. recoursion or loops?
    By Mecnels in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2002, 11:09 AM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21