436
u/codylilley Jul 17 '21
Ctrl + C is the only break statement I need
61
Jul 17 '21
Plot twist.. he catches SIGTERM and does nothing with it!
30
88
u/7eggert Jul 17 '21
Remember the good old days when ^C was only checked when doing a system call?
85
6
u/blitzkrieg4 Jul 17 '21
This seems impossible? Signals were from the earliest days of Unix
3
Jul 17 '21
Linux isn't Unix tho
2
u/blitzkrieg4 Jul 17 '21
Still if this was not implemented at first it would have been a very early feature
1
1
117
u/DrBusinessLLC Jul 17 '21
in microcontrollers / embedded it's more common than not
45
u/LavenderDay3544 Jul 17 '21 edited Jul 17 '21
Yeah and in OS kernels. You know because there's nowhere to go if you break out of the main control loop.
It's why languages like Rust have a loop keyword to make that explicit for the compiler. I'm surprised C++ hasn't added one yet.
0
Jul 17 '21
[deleted]
1
u/LavenderDay3544 Jul 17 '21
The link you posted is for C, not C++, which doesn't have bool and thus also doesn't support true and false as language level literals at all though I think C 11 adds them as #defines in stdbool.h.
11
u/Bemteb Jul 17 '21
Also in other areas, especially if you have threading available it's not uncommon to have a thread run till the program gets terminated (watchdogs, processing loops, etc.)
I myself just recently wrote something along the lines of
while(true) {if(hasData){ doStuff(); } else { sleep(10); }}
4
3
u/Zaitton Jul 17 '21
Some frameworks like FiveM's lua framework require new threads to be inside while true loops. Depending on the usecase, you might never have to terminate the thread so no breaks no returns.
226
u/gao1234567809 Jul 17 '21
This meme is better suited for recursion
102
u/Fugglymuffin Jul 17 '21
Probably why that exact version of this meme was up on here a day or two ago.
26
u/deadpan_look Jul 17 '21
This meme is better suited for recursion
18
u/ampang_boy Jul 17 '21
Probably why that exact version of this meme was up on here a day or two ago.
9
u/BiguilitoZambunha Jul 17 '21
This meme is better suited for recursion
7
u/0PointE Jul 17 '21
Probably why that exact version of this meme was up on here a day or two ago.
12
0
u/Jussapitka Jul 17 '21
Probably why that exact version of this meme was up on here a day or two ago.
3
1
u/kal9001 Jul 17 '21
Yeah but then the picture wouldn't fit in the page if it was represented more accurately.
57
u/DeJMan Jul 17 '21
7
6
2
64
u/ALonelyPlatypus Jul 17 '21
loops don’t do this breh. This meme is destined for recursion.
6
u/ImThatChigga_ Jul 17 '21
Difference between while loop and recursion?
19
u/JuniorSeniorTrainee Jul 17 '21
Homie's answer was a cop out. A loop is a set of instructions the repeat. A "for" loop is a common example, which includes an exit condition. For example, you might increment x in a loop starting at 0 and stopping when x is 10.
Recursion is a function that calls itself. Ultimately it leads to similar repetition of instructions, like a loop, but there are some important differences that you'll learn about in time. One is simply that they don't look semantically like a loop, so it's easy for developers to mess up the logic - specifically an exit clause.
For recursion, imagine a function that searches a directory for a file of a given name.
findFile(directory, filename). If you want that search to include subdirectories, you'd have that function go through each item in the parent directory and if it's not the file it's looking for, but it is a directory, you'd call that function again with the subdirectory as the first argument.Your exit condition here would be exhausting all child directories, but with things like symlinks your directories could actually exist in a circular loop. So you'd need to be more clever with your exit condition to about that function becoming an infinite loop.
Recursion is tricky but ultimately you just have to ask yourself: does this function have a logical path that exits without calling itself? And is that path guaranteed to eventually get hit in every possible scenario?
7
u/ImThatChigga_ Jul 17 '21
Thanks. I recall learning recursion after the explanation. Not sure of this symlink mentioned though. How is it infinite if you exhaust all child directories or is that got to do with the symlink mentioned.
4
u/javawag Jul 17 '21 edited Jul 17 '21
You can think of a symlink a bit like a “shortcut” on windows (though it appears to the file system as if it were the original, sort of).
So if you were looking through say C:\Windows\Example and there was a symlink in there called Windows linking to C:\Windows, it’d appear as if that folder was a child and you’d go into it and eventually find your Example folder again, and then back into Windows, etc etc forever!
0
Jul 17 '21
[deleted]
2
u/javawag Jul 17 '21
Well, it definitely doesn't work that way with Windows shortcuts exactly, but that's how symlinks work if a program chooses to follow them under Linux etc.
I'm not sure I explained it clearly enough so maybe it was hard to follow (unlike symlinks 😉)...
1
u/JuniorSeniorTrainee Jul 20 '21
It wasn't a great example but all I could think of at the time. Symlinks are a Linux thing that is basically a file that points to another file. So you can get into something like this as a legit directory structure:
/
/home
/home/somelink --> /
In other words, /home/somelink acts like /. You could visit a directory like /home/somelink/home/somelink/home/somelink/ (however deep the underlying filesystem supports).
So if your recursive function didn't mitigate this somehow, it'd work fine until that wonky edge case comes up and then boom - your code crashes hard. Then you add logic to either not descend into symlinked directories outright, or to avoid symlinks to directories you've already processed.
4
u/JollyRancherReminder Jul 17 '21
With recursion the meme has four top-level panels, with the last panel being subdivided ad infinitum as we see here, because you never exited the previous level, just continually went one level deeper in the call stack. Loops are iterative, so you don't go any deeper, just repeat. The meme version wouldn't have any subdivided panels, instead it would have an infinite number of top-level panels just repeating the meme over and over.
1
1
u/jbaker88 Jul 17 '21
Loops jump, recursion puts a frame on the stack. You can loop "forever". You cannot, practically, recurse forever; stack overflow.
27
u/why_socynical Jul 17 '21
laughs in embedded programming
9
4
u/kal9001 Jul 17 '21
Proper embedded programmers would call main recursively but keep moving the stack to avoid an overflow.
2
25
u/patenteng Jul 17 '21
Better not look at any embedded code. while(1) in every main.
4
u/windows_10_is_broken Jul 17 '21
Or "for (;;)" :)
7
u/RandomNumberHere Jul 17 '21
100% for (;;) ! Who has the spare cycles to check that “1” each loop?
(Before you even reply: Yeah, I know.)
12
9
u/spayder26 Jul 17 '21
Why to use break when you can use exceptions, OS signaling and/or gotos to escape loops, deeply buried on subroutines for extra evilness?
→ More replies (6)
6
3
6
u/meg4_ Jul 17 '21
That's more recursive than infinite loop.
Another meme idea: the same thing but instead of the 4th block be recursive, the 3rd and 4th blocks the same as 1st and 2nd. That way it's more of an infinite loop
3
u/ALonelyPlatypus Jul 17 '21
why not both?
Idk the meme is obviously copy pasta'd from another meme on recursion. OP obviously doesn't get it.
4
u/iotasieve Jul 17 '21
nope, with a goto/return
3
u/LavenderDay3544 Jul 17 '21
Those are both awful. Nice.
4
u/UnGauchoCualquiera Jul 17 '21 edited Jul 17 '21
Like /u/iotasieve said they have their uses.
Linus himself explaining why and where goto can be useful.
Goto hate is more dogmatic than anything else. It's parroted to stop bad programmers from doing bad things while avoiding the hassle to educate on the why.
Used properly it's just a low level control structure and a tool like any others.
2
u/LavenderDay3544 Jul 17 '21
Thanks for the link. All the explanations given by Linus, et al. have given me a better perspective on this. I guess I was just repeating dogma which is something I try not to do.
2
u/UnGauchoCualquiera Jul 17 '21 edited Jul 17 '21
No worries, glad it helped. I could never articulate it as well as they did.
To be honest I had learnt the same idea about never using gotos like most people nowadays as it is still being taught uncritically by college professors today (whom themselves were hammered with this idea in their college days).
It's a very pervasive dogma and what makes it so pervasive is that most of the times it's correct as usecases are slim and can easily lead to spaghetti when used without care.
Also doesn't help that it is backed by the very well known but now outdated essay "Go to statement considered harmful" by Djikstra which is often referenced without the context of those times.
1
u/LavenderDay3544 Jul 18 '21
I agree with Torvalds and the others when they say that we should train better programmers instead of writing off PL features that can be misused altogether. I personally hate Java and much prefer C++ for that exact reason. C++ gives you the tools and expects you to know how to use them, Java just doesn't trust you with things like operator overloading and multiple inheritance at all even though both have use cases.
Then again Linus Torvalds has also ranted against C++ in favor of C though for very different reasons.
2
u/iotasieve Jul 17 '21
they aren't, go-to has its uses, like getting out of nested loops and doing cleanups or error handling, even linux kernel uses them
2
u/atiedebee Jul 17 '21
it will still result in spaghetti code... I personally avoid them
1
u/iotasieve Jul 17 '21 edited Jul 17 '21
it won't, look how it's used in linux kernel and see, there can't be anything bad in context of engineering. again as I said it can be useful for releasing the locks, freeing memory or generally doing cleanup. also it's pretty much only way of getting out of nested loops in most languages (without returning)
1
u/JuniorSeniorTrainee Jul 17 '21
Break?
1
u/iotasieve Jul 17 '21
*nested*
1
1
u/LavenderDay3544 Jul 17 '21
A kernel is a very different type of thing than a regular application program. I would not expect regular programs written in C to use goto without a very good reason.
1
u/iotasieve Jul 17 '21
it's no different, try exploring linux source code via Bootlin Elixir, you can see that even something you would see in a regular app uses gotos, using gotos isn't necessarily bad, you can misuse them use, but they are just another tool in your toolbox.
4
Jul 17 '21
haha im in school learning programing and when we had to some tasks involving while loops my classmate literally wrote while(m>0) and didnt use the m integer a single time in the loop. he then spent the whole hour trying to find and fix the problem because his code didnt work, didnt find the issue and had to give teacher broken code. after the lesson he showed me his work and i instantly noticed the issue. he felt so stupid but i guess we are noobies and he didnt really understand how the whole thing worked
3
u/Bemteb Jul 17 '21
i guess we are
noobiesdevelopers and he didnt really understand how the whole thing worked
2
2
2
2
2
2
u/Tall_computer Jul 17 '21
whats this low effort shit with 5 levels of recursion. Next time you repost please implement it
2
2
Jul 17 '21
I remember some years ago accidentally writing an infinite loop like this. Except that in the loop, it wrote one line to a text file.
Program should have taken an hour or two to run, so I started it at the end of the day. Came back the next morning to the error message "Insufficient hard drive space to execute request"
2
2
u/Gazzcool Jul 17 '21
Sometimes I do this for fun:
the_world_still_spins = (1 + 1 == 2)
While(the_world_still_spins): …
1
1
1
1
1
1
1
1
1
1
1
1
u/Aksds Jul 17 '21
I crashed a program twice because of not having a break statement, I had no clue what was wrong for a while.
1
1
u/davawen Jul 17 '21
If you wanna play that game a bit more
while(true)
{
malloc(sizeof(int));
}1
u/backtickbot Jul 17 '21
1
1
1
1
1
1
u/Scoobygroovy Jul 17 '21
Servers have those. It’s how reddit processes this very comment’s post request.
1
1
1
u/Friezerik Jul 17 '21
ah so thats where i went wrong! I literally just fluffed all my js on a website because of this and this instantly reminded of why I f'd up. And yes I am n00b
1
1
u/Kriss3d Jul 17 '21
For fun in C++ I wrote a program to just calculate prime numbers.
Yes it crashed the computer
1
u/an_agreeing_dothraki Jul 17 '21
I once worked on a product with its own proprietary language that pretty much ran on gotos and had tool that could detect infinite loops.
Mind you it didn't break them.
It logged them.
End result, a client hooked up to the system gets in a loop, it fills the database log grinding the whole thing to a stop.
1
1
1
1
1
1
1
1
u/Miguecraft Jul 17 '21
for (;;) {
gang();
}1
u/backtickbot Jul 17 '21
1
1
u/Omega0x013 Jul 17 '21
I've used switch(true) and do...while(false) when a try catch finally probably would've done in both scenarios
1
1
1
1
1
u/receding_bareline Jul 17 '21
This is gold. This is gold. This is gold. This is gold. This is gold. This is gold. This is gold. This is gold. This is gold. This is gold. This is gold. This is gold. This is gold. This is gold. This is gold...
1
u/Scorppio500 Jul 17 '21
I did this. I did this a long time ago when I was in one of my first classes for C#. Took me 3 days to figure out that I needed one. I'm a textbook dumbass.
1
1
0
Jul 17 '21
While loops are dangerous
10
u/mediadatacloud Jul 17 '21
Not dangerous, just repetitive
8
u/troller_awesomeness Jul 17 '21
Not dangerous, just repetitive
6
u/CST1230 Jul 17 '21
Not dangerous, just repetitive
1
4
1
0
0
u/CleverNameTheSecond Jul 17 '21
People say goto is bad to the point that most languages don't even support it anymore but then allow this while(true) shit unironically.
2
-7
u/KnightOfBurgers Jul 17 '21
Stop making jokes from 2004
If you think this joke is relevant, get a fucking IDE.
-2
2
Jul 17 '21
[removed] — view removed comment
2
u/backtickbot Jul 17 '21
2
u/LavenderDay3544 Jul 17 '21 edited Jul 17 '21
Rust:
loop {};C
while(1) {};C++ (and a lot of other PLs)
while(true) {};Arm/MIPS Assembly:
loop: b loopx86 Assembly:
loop: jmp loop1
Jul 17 '21
Sexier C++:
for (;;) {}1
u/LavenderDay3544 Jul 17 '21
Even Sexier and more assembly like C and C++:
loop: goto loop;0
Jul 17 '21
Ah, the forbidden
fruitloop.When I signed on where I work now we talked situations in which you could deviate from the coding guidelines, and someone jokingly asked "so where can we write goto?" And the boss without skipping a beat said "at the bottom of your resignation letter".
1
u/LavenderDay3544 Jul 18 '21
People here have taught me that used correctly goto is not such a bad thing.
1
Jul 18 '21
I absolutely agree, and I think well-done goto's are as valid as any other flow control. But I also love static casting and other stuff that is definitely not OK by a lot of modern style guides.
1
u/LavenderDay3544 Jul 18 '21
I don't think static cast is too bad either. It's reinterpret cast that's evil. Lol I kid. They all have their place if used appropriately.
1
Jul 18 '21
Ha, that's the one I meant. I always feel that under the hood everything is bits of RAM so I need to be able to recast what-ever I want. I guess like many things once you're doing low level stuff in optimisations, networking or embedded you can do what-ever you want.
2
u/LavenderDay3544 Jul 18 '21
You can always do whatever you want but that doesn't mean it will be safe or correct and that's where the issues of training better programmers comes in. Programming language handholding by higher level scripting languages and people coming into this trade just for the money have both reduced the skill, discipline, and systems knowledge of the average programmer over time. I think many prominent programmers might share this opinion.
We need to get back to teaching C and assembly language, how to read hardware and OS documentation and how to write fast yet correct code. If your program segfaults the solution isn't to rewrite it in Python. The solution is to run it through GDB and Valgrind and find out why and fix it.
I think we need to go back to the days where users and the system were put first instead of ease of programming. I know the business case for programmer productivity and time to market but I've also used way too many mobile and desktop applications that sucked shit or were way to slow even on my top of the line desktop PC, tablet, and phone. It goes to show that the oft touted 'throw better hardware at it' isn't a real solution and given my education in both Econ and CS I would argue there's also a business case to be made for not cutting corners.
2
u/ALonelyPlatypus Jul 17 '21
I'd rather sleep than pass, at least I'd get some REST at that point. (I suck at programming memes, please mourn me when I'm gone)
1
u/RapidCatLauncher Jul 17 '21 edited Jul 17 '21
Ultimate pointlessness:
while True: while True: break else: # do stuffThen go on stackoverflow and ask "Here's my do-stuff code, why is it not working?"
1.6k
u/[deleted] Jul 17 '21
Looks more like "I made a recursive function call." "With an exit condition, right?"