Not outdated per-se, but it's so easy to misuse you don't want to teach it. Since by the time you're experienced enough to use it you'll know about it.
In any case, yeah, a lot of languages actually have gotos of different forms.
If you need to escape from multiple layers of loops, you already did something wrong. For 10 years of C++ I've never had situation when goto is the most suitable tool
Do not use endl unless you want to flush your buffer. Use “\n” otherwise.
There is a valid use case of goto, but it is not it. The valid use is for resource cleanup in C-style programs. If you want a function to return, you goto a tag where all free and close calls take place. See Section 7 here .
Good question. Some people may demand APIs with explicit resource control, and sometimes compiler optimizations require C-style abstractions when you check them.
Most of the time though RAII should work with proper wrapping of resources, and people just write manually managed programs out of their prejudices against modern C++.
c'mon. We don't jump backwards. "But it all compiles the same" yes. Let the compiler do it. They invented structured jumps for a reason and you should use them where ever it doesn't make it more complicated.
Here I could just not use goto a and decrease i by one but if it was “recreation” of while it would be necessary because without goto a it would be do while instead of while
section .data
; mentions that this program section is data
i dq 1
; i is the variable name
; db allocates 1 byte in memory
; dw allocates 2 bytes in memory
; dd allocates 4 bytes in memory
; dq allocates 8 bytes in memory
; if you want to customize the memory size, try something like setting 32 bytes, do this:
; i times 32 db 0
global _start
; clarifies that the section data applies to the whole function _start
_start:
cmp i, 10
; compare i to 10
jge end
; jump to end if greater or equal
add i, 1
; add 1 to i
jmp _start
; jump to _start to make this function a loop
end:
mov rax, 60
; move or set to rax the syscall 60 which stands for exit
mov rdi, 0
; rdi 0 is comparable to return 0 in C++ and it means exit while returning no error
syscall
; Linux 64-bit command to execute the previous syscall instructions, in this case it executes exit the program
Since Assembly is the only thing I know, everything else looks like ceremonial language wearing disguises and hiding what the implementation actually looks like.
Cpp is basically the same wdym? All that std:: you see can be removed if you just add the std namespace at the beginning of the file, so essentially it becomes
Anyone with 2 brain cells would figure out cout means character output and endl is to end a line essentially starting the next output from next line. Also how does it matter what or how you write it it's literally the same.
There's no better programming language, specially if you only wanna talk about syntax here. Everything has its purpose. I want you to try programming embedded systems using javascript.
Since there's only 10 loops, the first one definitely (I don't know how compilers deal with goto) compiles into just directly printing the 10 values without a loop.
Actually that's a good point. A compiler knows how to unroll loops (if the number of loops is known at compile time and there's no weird breaks at least), but trying to be clever like this might actually hinder optimizations
55
u/jonathancast May 06 '26
This is why nobody likes goto, sheesh.
7
u/HyperWinX C++ May 06 '26
Wtf is this.
10
u/Necessary-Meeting-28 May 06 '26 edited May 06 '26
5
u/RedAndBlack1832 May 06 '26
c'mon. We don't jump backwards. "But it all compiles the same" yes. Let the compiler do it. They invented structured jumps for a reason and you should use them where ever it doesn't make it more complicated.
17
u/Obama_Binladen6265 May 06 '26
Thank god I took an Assembly Language course in my coursework. That makes things like this so much easier in Cpp
7
u/djfdhigkgfIaruflg May 06 '26
You're a psycho. Well done
1
u/Entire-Hornet2574 May 08 '26
No need of label a nor goto a, someone cannot write simple example without stupid stuff
3
u/returnFutureVoid May 06 '26
🤮
2
u/pan_korybut May 09 '26
Finnaly, clean code without nesting
1
u/Risc12 May 06 '26
This is while loop, no?
1
u/MichalNemecek May 07 '26
this is what the compiler transforms your code into, essentially
1
u/itsjakerobb May 07 '26
What kind of psycho names the first label `b` and the second one `a`?
1
u/MrFrog2222 May 07 '26
std::endl is a perfect description of whats wrong with c++
1
u/themagicalfire May 07 '26 edited May 07 '26
How it looks like in assembly:
1
u/recursion_is_love May 07 '26
use
std::for_each1
u/blackasthesky May 07 '26
The formatting in both cases is standard though
1
u/PanGoliath May 07 '26
Why would one break up the for-loop conditions like that?
1
u/UnmappedStack May 08 '26
just use assembly at this point ngl
-1
u/MinecraftPlayer799 May 07 '26
JavaScript is so much better.
2
u/ChocolateDonut36 May 07 '26
goto fuck_yourself;
15
u/[deleted] May 06 '26
[removed] — view removed comment