PDA

View Full Version : loop



Unregistered
10-20-2001, 10:38 AM
how do you get the program to go back to a certain part in the program

Engineer
10-20-2001, 04:37 PM
// 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.

Zeeshan
11-06-2001, 12:26 AM
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

Hillbillie
11-06-2001, 04:42 PM
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...

ronin
11-08-2001, 03:41 PM
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
}

ronin
11-08-2001, 03:45 PM
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.

frenchfry164
11-08-2001, 07:06 PM
Are you doing a game loop, or just a normal loop?