Thread: loop

  1. #1
    Unregistered
    Guest

    loop

    how do you get the program to go back to a certain part in the program

  2. #2
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125
    // certain part of the program
    certain_part:
    // some code


    // another part of the program
    goto certain_part;

    Be careful on how you use the goto statement. Use it ONLY if you really need it, but it would be much safer to break your code into small functions to enable to call a function instead of using a goto. Goto's can cause unwanted recursion and, like someone on this board once said "goto's can make your code look like a spaghetti".

    Cheers.
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  3. #3
    Registered User Zeeshan's Avatar
    Join Date
    Oct 2001
    Location
    London, United Kingdom
    Posts
    226
    GOTO can b useful sometimes, although it is not good programming practice 2 use goto...

    Dr. AL-Stevens - "Al-Stevens Teaches C" - BPB Publications
    pg. 91
    Everyone who writes about programming deprecates the goto statement and cautions you not to use it. I've written and published lots of C and C++ code without using goto. It has been conclusively proven that any algorithm can be designed with the three control structures of structures programming and without the goto statement.
    You can use more loops, alongwith the break and continue statements to do what you need. But, my suggestion is DO NOT USE goto

  4. #4
    Disagreeably Disagreeable
    Join Date
    Aug 2001
    Posts
    711
    I agree. You really should stay away from goto as much as possible.

    However, used correctly and wisely goto is a very useful peice of the language...

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Use functions.

    void funct1(void);
    void funct2(void);

    int main()
    {
    int choice;

    cout << "enter your option: 1) blah 2) blah";
    cin >> choice;

    switch(choice)
    {
    case 1: funct1();
    break;
    case 2: funct2();
    break;
    default: break;
    }

    return 0;
    }

    void funct1(void)
    {
    whatever here
    }

    void funct2(void)
    {
    whatever here as well
    }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  6. #6
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Oops... put the entire thing in a do-while loop, a while loop, or a forever loop using a break statment to go back to the portion of the program that you want to go back to. The type of loop depends on what condition you are testing for.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  7. #7
    Are you doing a game loop, or just a normal loop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM