Goto is good for you

goto is good for you.

Other urls found in this thread:

docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
drdobbs.com/the-new-c-inline-functions/184401540
twitter.com/AnonBabble

not really

Who even writes assembly anymore?

why would you use that, when you can do the same exact thing with simple if else statements, and produce actually good and readable code?

if you are writing c++ and using goto you should kill yourself at that point

Java did the right thing by making goto a reserved word that is not a keyword.

real men use jmp

implying assembly
how could a goto possibly be unclear, it's probably the most unambiguous instruction in programming
consider the following
for(;;x++) {
for(;;y--) {
if (x>y) goto xyconverged;
}
}
xyconverged:

write me the non-goto version

Hideous nested if else trees are unreadable as fuck.

I never got the goto is unreadable meme.

I guess if the labeling is vague but that's still not a good enough excuse to demonize it so much.

There are some legit reasons to use goto in c++, like exiting a double for loop.

How do you pronounce goto?
For me it's goh-toe

This code doesn't even do what you think it does, retard. The outer loop will never repeat because y will be decremented until it's less than x and then you break out of both loops. The only exception is if x is -2^32 then it will be an infinite loop because decrementing y when y is -2^32 will change y to 2^32-1.

y = x - 1

well fuck me silly and call me george, you get the idea, exiting from nested loops is easier with gotos

Like this, for example

It's an illustration dumbass.

Throw an exception, it’s type safe and not a goto. :^)

how about just copy-paste everything after popFilled into the loop

>write me the non-goto version
t. pajeet

>Ignoring the least instruction-intensive, most computationally efficient way of doing something just because someone somewhere told you to

Every time :^)

a[i++] = i++;

But I wanna break out of the loop when that condition is met

breaking out of nested loops is probably the only case where I've heard that goto is even vaguely appropriate

What about large, complex switch statements?

some languages (e.g. basic) dont have functions so goto is the only thing you can use to jump to different parts of the program

It’s bad! So bad! You can’t use it for critical code. :^)

void PopFill() {
for(..) for(..) {
if(..) return;
}
}

That also sounds appropriate to me, but I don't believe that I've heard it from anyone else.

Well those languages are obviously exceptions, and not too many people are programming in BASIC nowadays.

So, basically, pass more objects around to avoid using a statement that only uses like one clock cycle?

dont get me started, i have been using it daily for the past couple of months because just maintaining the current code is less effort than rewriting in c

>He does not have a FizzBuzz in assembly on his github
>He does not have a prime numbers under 2 million in assembly on his github
Why you even responding to programming threads then?

well.. the compiler will just use jmp anyway so it's not like it's any more expensive. I'm the one who likes goto, I'm just stirring shit basically

Damn, that sounds rough.
What's the program do?

The approach I was taught for that involved checking for the loop completion condition where you do your bounds checking in the for loops.

It's ugly to do that check for each loop you're in but it's really handy to have that condition spelled out explicitly right up in the loop declaration.

Yeah, that works too, but this is still more efficient on paper.

The compiler is probably working its magic though regardless of what's written.

firmware for a PIC18, it's not so bad, the compiler we're using has some pretty crazy undocumented macro support so I have somewhat wrangled everything into a presentable looking syntax
still.. using pointers is basically a hack in basic.. would be so much easier in c, can you imagine every single variable you declare is global, there is no overflow protection, there is no debugging aside form writing messages to a serial port.. and this code is mission critical
still fun tho

>this is still more efficient on paper.
If you're checking for your GOTO with every iteration of the loop, why would it matter if you do so in the body of the loop or the declaration?

DONT CALL HIM THAT

If irc, Java has a named break, which is just a more restrictive version of a goto.
Syntactically at least, it technically isn't a goto. Although semantically, they're equivalent.

A break statement won't break out of both loops, so they're not semantically equivalent.

Also, C/C++ has these too.

Found what youre talking about
docs.oracle.com/javase/tutorial/java/nutsandbolts/branch.html
> search:
> for (i = 0; i < arrayOfInts.length; i++) {
> for (j = 0; j < arrayOfInts[i].length;
> j++) {
> if (arrayOfInts[i][j] == searchfor) {
> foundIt = true;
> break search;
> }
> }
> }
did not know java had that desu

Missed the "named" before the break. Yeah, those are dope

but this is java we're talking about, like.. java
named breaks dont make up for all the other disgusting shit you have to deal with

That's pretty nice.

no one is saying that they make up for anything

[] { for (...) for (...) if (...) return; }();

I would fire you on the spot and fuck your wife

A lambda? Once again, an inefficient work around for a simple goto statement.

Well, you shouldn't use exceptions other than for exceptional cases. Exceptions are slow af

for (;;x++) {
for (;;y--) {
if (x > y) xyconverged();
}
}

/* ... */

void xyconverged() {}

how's that inefficient when the whole thing is inlined? I expect the compiler will generate the same code in both cases.

>what is scope

What happens when you GOTO out of a loop re the variables?

is the increment var still valid? ie:
for (int i = 0; i < 255; i++)
goto Jews;
....
Jews:
std::cout

this doesn't stop loop from iterating

Goto is useful for function-return cleanup in languages like C. Not sure about other useful instances.

Do you realize that every language is converted in machine instructions and it has only goto (JUMP) and compare instruction for dealing with cycles. Moron

the semantics of exceptions are more harmful than the speed of exceptions

Well, I guess you're right. The compiler probably won't generate identical code, but it should at least be similar in execution time.

Still, using a goto here is much more intuitive. I guarantee that using an inline lambda in a case like this will confuse at least a couple of people on your team

>not falling for the "evil goto" meme in the university
>rarely but use it to escape nested functions. don't need to use dozens of breaks
>tfw

Exceptions being slow isn't the only reason why I said not to use them here.

Since i is a loop-local variable (it's declared in the for loop header), this will be a compile-time error. It can't be found at the cout line. goto doesn't affect the scope of variables.

no shit u fucking faggot

And what's the reason to use goto over just extending your already cryptic conditions? I don't get it.

The problem with goto is precisely readability problems it presents. In your case, your conditions of breaking out of the loop are located inside the loop, so assuming someone decides to debug this code, they'll have to read through the whole loop code just to understand its basic behavior. Which is as stupid as expecting someone to first read implementation of all and every method used in a program before reading the program itself.

Even the "legitness" of such an example heavily relies on the fact the given code is dumbly simple. But using the same approach in more complex conditions will result in an unreadable mess while providing nothing of value as a result.

Even here, if I were to debug this code and assume its writer isn't a degenerate, I would first skip the loop entirely only to immediately see a fucking goto label and realize I now have to search for a fucking goto that uses that fucking label understand the jump conditions as the author didn't bother either to make them obvious nor to encapsulate them.

if goto is evil, why won't programming language developers include syntax feature to break from all loops?

because there is one already. it's called goto

How about destructors with side-effects in C++?

>Your conditions of breaking out of the loop are located inside the loop, so assuming someone decides to debug this code, they'll have to read through the whole loop code just to understand its basic behavior

Except that I actually described what this whole code snippet does earlier in the code. I just didn't take a screenshot of it cause IDGAF and I was just trying to explain that this is literally the last legitimate use case for a goto. I'm not trying to describe my algorithm.

Also, the name of the label is pretty obvious. One look at this code and you know exactly what is happening. Replace this with a lambda and you'll confuse someone.

but if they add it we can fully avoid goto

Java as a language is pretty great. I love writing Java.

Everything surrounding it is garbage. The packages, the multiple standards for 1 thing because muh backwards compatibility(which breaks anyway LOL), and all the garbage teachers/programmers using it.

i don't think there will be any because an ultimate break/return from all functions would most probably terminate the program as well because it doesn't need to know how deep into functions it is right now

retry:
try { thing(); }
catch (specific_exception)
{
fix_it();
goto retry;
}

Video codecs,
drivers,
Operating systems etc.

non-brainlet reporting in
what're you using your genetic alg for m8, i have been meaning to look into interesting applications of it but the most useful stuff i have found is finding the function for a given waveform or optimising a route or set of steps
just doesnt seem as useful as a neural net

while (!thing())
{ }

beautiful. now try that with multiple fault conditions.

firmware is one of the bigger ones, your microwave, washing machine, electric toothbrush, cars, cameras, industrial computers and an endless list of other hardware use firmware written in asm
it is definitely not going away any time soon

>If transfer of control exits the scope of any automatic variables (e.g. by jumping backwards to a point before the declarations of such variables or by jumping forward out of a compound statement where the variables are scoped), the destructors are called for all variables whose scope was exited, in the order opposite to the order of their construction.

C++ goto is pretty safe.

Python, as always, does it better
from itertools import product

a = (first thing you want to iterate over)
b = (second thing you want to iterate over)

for it in product(a, b):
....
break

You can also use chainmap from collections if you want to strip dupes.

>imbly thing returns bool

while (enum thing_error = thing())
{ /* handle */ }

goto is perfect for obfuscation

obfuscating well designed algorithms from brainlets

do {
bool retry=false;
try{ thing(); }
catch (specific_exception) {
fix_it();
retry=true;
}
} while(retry);

The problem with Goto's is simply the fact that the compiler in most cases is smarter than you, and using goto's usually forces the compiler to compile to less efficient machine-code.
And getting out of nested for loops is easy as pie, you just add the variable to the condition of the looping, ya doof.

There's basically no reason to prematurely exit a for loop unless you're searching for something, in which case the loop should have probably been inside a function anyway that you can just return out of.
There's literally no use for goto if you have good programming practice.

Here you go
f=1;
for(; f; ++x){
for(; f; --y){
if(x>y) f=0;
}
}

I fucked up the scoping of the bool retry. It compiles to the exact same binary as the goto version on gcc -O2.

bool retry;
do {
retry=false;
try{ thing(); }
catch (specific_exception) {
fix_it();
retry=true;
}
} while(retry);


I'm not arguing wether it is more readable than the goto version though. I wouldn't use goto though.

>Except
that you're a shit-eating idiot, user.

Anyway, if everything was solvable with proper commentary, naming conventions would not have been reinforced so actively.

Your name of the label is shit and the only one who thinks it's "obvious" is you. For an outside viewer, it's worded like a branching operation and nothing indicates the opposite, including the nearby commentary as you didn't bother to leave comments on the fucking label and the statement themselves - probably the only lines in your whole program that could've actually confused someone.

Again, in this particular case, yeah it works and is unlikely to cause any problem, but that doesn't change the fact avoiding usage of goto is a much better idea. Like, for example, putting the loop-breaking conditions somewhere they belong (for example, condition fucking field of the for statement) instead of hiding them inside the loop and writing a book about that in the comments "earlier in the code". This way anyone who's going to debug your code (including yourself when you'll grow up a bit) won't have to guess how stupid the code author was - people are likely to assume that, yeah, "it's likely used to break out of the loop" but you've already lost some points in the "let's think author isn't a degenerate" category, so they can't really be sure about that.
Who knows, maybe your unstoppable genius had decided to "optimize" your code with some spicy goto branching?

It can be acceptable to break out of nested loops with a goto statement but your particular snippet is a shitty example of that. And being an all-out faggot against the completely healthy idea of avoiding goto in your particular code snippet is plain stupid.

Hey user, glad you asked. Please excuse any typos or bad grammar, I'm phoneposting (kms). I'm currently using this GA to design quantum cascade lasers. GAs are invaluable, considering the search space for a device like this is infinitely large and there are, theoretically, many optimal solutions.

First of all, Genetic Algorithms and Neural Networks solve different kinds of problems.

Genetic algorithms are extremely useful for obtaining (non-deterministic) solutions to optimization problems via a guided search. Basically, it mimics Darwin's theory of evolution in that good solutions to problems have a higher fitness and their traits are propegated throughout future populations. This causes the entire population to head to a localized optimal solution, much like how humans have evolved to be "good enough" to survive in our environment.

I'm not as well versed in neural nets, but I know they solve different types of problems. Neural networks exploits patterns discovered in data, mainly through mimicking synapses in a primitive model of a brain.

You can actually use both in tandem; use the neural net to generate individuals and use the genetic algorithm to selectively breed the elite individuals together to develop even better solutions.

interdasting, never thought of using a neural net to improve individual generation
i have used both but only used GAs for reverse engineering waveforms. i guess i just havent used them enough to develop an intuition on what problems can be solved using one

No it's not. Main problem with goto is that label can be anywhere. With blocks of code (encapsulated in parentheses and/or whitespaces) it's obvious from the first glance where code starts and ends.

>he thinks this is impressive
>5 minute programs in less popular languages is probably all he ever does

So, what am I supposed to do? Send my massive array of objects to another function? Spend another 20 minutes declaring more shit? What's wrong with being practical and putting a goto statement here?

By the way, "popFilled" means the population was filled, you ass. The branch is literally less than two lines under the for loop. What, do you want me to call the label "populationwasfilledlongasslabelbecauseanongotbutttouchediusedagotostatement"? Sure, the code needs some more comments, but fuck, give me a break, you autistic faggot. Go Google this problem and show me how many other decent programmers wouldn't have done the same thing in my shoes.

By the way, this is the kind of shit that happens when you have to port someone's MATLAB grad code to C++.

Yeah, GAs are extremely versatile. If you can come up with a way of representing your problem using a gene string and develop some way to determine the "fitness" of a solution, you can apply a genetic algorithm to it.

>Main problem with goto is that label can be anywhere.
So can stuff like semicolons or braces in C, but sane people do not put them anywhere but where they make it obvious at a glance where the blocks are and lines end

Actually I thought about it. Inline functions aren't always optimized by the compiler; the compiler always has the final say.

drdobbs.com/the-new-c-inline-functions/184401540
>Likeregister, theinlinekeyword is only a suggestion that an optimization be performed. Some compilers might ignore it completely and never inline. Others might ignore it and inline based on criteria that usually result in best performance. Still other compilers might only honor the keyword if additional requirements are met by the program.

Have you heard of functions?

Yeah, I've also heard of function parameters, and I don't want to pass like 10 of them

have you heard of scope

had you designed your program properly in the first place you wouldn't need 10 of them

Have you heard of pointers?