-5

I'm coding a program and I thought while() function could help me but it's not as I thought, and a normal GOTO function of the old Commodore 64 made me think that is the solution. I just need to do this:

// float condition 
10 printf("type a number: ");
20 scanf("%f", &condition);
30 if (condition == 1) {  printf("ok! \n"); }
// goto 10 here

discard that in this case I could use while() function anyway but as I said in my case while() function will not work. How can I do the Basic goto function in C?

nkrivenko
  • 1,231
  • 3
  • 14
  • 23
  • Why would a while not work? Perhaps you mean that you'd rather want a `do...while` loop? – fredrik Nov 01 '20 at 11:43
  • 2
    You do not want to do that. Despite being a rather low level language, C allows structured programming, and `goto` should be seldom if ever used. – Serge Ballesta Nov 01 '20 at 11:48
  • Read [this C reference](https://en.cppreference.com/w/c), the [n1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) C standard, the documentation of your C compiler (e.g. [GCC](https://gcc.gnu.org/)...) and of your debugger (e.g. [GDB](https://www.gnu.org/software/gdb/)...). Read also [*Modern C*](https://modernc.gforge.inria.fr/) and take inspiration from existing open source C code on http://github.com/ – Basile Starynkevitch Nov 01 '20 at 12:04
  • 2
    [GOTO still considered harmful?](https://stackoverflow.com/q/46586/995714), [Why is goto considered evil in Java and other high-level programming languages?](https://stackoverflow.com/q/13785068/995714), [Is it ever advantageous to use 'goto' in a language that supports loops and functions? If so, why?](https://stackoverflow.com/q/24451/995714) – phuclv Nov 01 '20 at 12:05

1 Answers1

2

You can use labels :

something like (very simplified)

voif f()
{
  goto a;

a:
  dosomething();
}

But you should learn to use control flow logic to not have to use goto.

Max
  • 3,128
  • 1
  • 24
  • 24
  • That is correct C language from a conformance point of view and directly answers the question. But as best practices recommend to use clean structured loops instead of plain `goto`, you should at least warn future readers against it. – Serge Ballesta Nov 01 '20 at 11:46
  • Thanks, goto was the only solution and I made it safe and good working. I don't wanted to use it since I heard is dangerous but it works. Thanks ;D – Ishimura_Projects Nov 01 '20 at 11:46
  • 2
    @Ishimura_Projects `goto` was not the only solution. The way to do it is to enclose the lines with `while(1) { }` – Weather Vane Nov 01 '20 at 11:55
  • 1
    @Ishimura_Projects `goto` is never the only solution. And in by far the most cases it is not even the best solution. – Gerhardh Nov 01 '20 at 12:12