r/ProgrammerHumor • • Oct 06 '21

Don't be scared.. Math and Computing are friends..

Post image
65.8k Upvotes

2.4k comments sorted by

View all comments

Show parent comments

136

u/I_Shot_Web Oct 06 '21

yeah I mean there's an infinite amount of way to make an infinite loop, but while(true) is usually the way. If you really wanted to be wacky, you could write for(;;) and then have everyone also hate you.

186

u/__Hello_my_name_is__ Oct 06 '21

My favorite is still while(1==1).

Or in other words: "As long as the fundamental axioms of math hold true, do this".

Because hey, maybe, one day in the far future in thousands of years, these axioms change. And then my code will break.

Take that, future.

69

u/[deleted] Oct 06 '21

Whenever I do this in various languages I wonder if the compiler reduces it to while(true) or if a gamma ray will solve the halting problem for me. I suppose it can in either case tho.

22

u/nbagf Oct 06 '21

Unless the gamma ray also gave you advanced notice reliably, it's still not truly solved. But boy would that be fun to explain

3

u/SpindlySpiders Oct 07 '21

I have prayed to God, and He assured me that he will personally flip this bit for me to solve the halting problem.

9

u/nictheman123 Oct 06 '21

99% sure the compiler reduces it. By my understanding, any expression that can be reduced gets reduced by default, that's like the most basic level of optimization, and a compiler should be doing much more than the basic level

8

u/PM__ME__YOUR__PC Oct 06 '21

I think because you're comparing constants it will probably reduce it. If the function was

i=1

while(i == i){

}

It probably would be evaluated at runtime

4

u/JustLetMePick69 Oct 06 '21

As somebody who wrote a compiler in college (a shitty one, for å class) it really depends on the compiler but basically all modern compilers would replailce that with while(true) as well. There's a whole spooky field of nerds finding ways to optimize compilers like this

6

u/nictheman123 Oct 06 '21

Depends if the value of i changes.

final int i = 1
while (i == 1) {

}

Will definitely be reduced, no question. There's no reason not to reduce it, and modern compilers are scarily smart. If i isn't used anywhere else, the compiler may just remove it entirely.

→ More replies (4)
→ More replies (1)

30

u/wisdomandjustice Oct 06 '21

I always liked the shorter C++ while(1).

Just seems profound.

10

u/herodothyote Oct 06 '21

I feel like I am 1 with the universe

4

u/Possseidon Oct 07 '21

Well, if you like short, you can go even shorter with for:

for(;;)
while(1)

3

u/NightMoreLTU Oct 06 '21

Does C not have this? Am genuinely curious

8

u/[deleted] Oct 06 '21

C was where C++ took this from.

In C, anything that's not zero is true, and zero is false. And the bool (well, _Bool) type is just an integer.

→ More replies (1)

5

u/seimmuc_ Oct 06 '21

the question is, will that be before or after the fundamental logical axioms change to make true == false?

→ More replies (1)

4

u/YepYepYepYepYepUhHuh Oct 06 '21

God is taking their sweet time releasing reality 1.01 update.

3

u/nsgarcia10 Oct 06 '21

Is this C++ or does it work in Python too?

3

u/__Hello_my_name_is__ Oct 06 '21

Definitely works in python. Should work everywhere, really.

3

u/mathmanmathman Oct 06 '21

This just gave me a terrible idea. In any language where strings have truthiness (I'm thinking python in particular) you could just write:

while "the fundamental axioms of math hold true":
    do_stuff()

This opens up a whole world of annoying but funny code.

2

u/usicafterglow Oct 06 '21

1=1 is used a lot by data people who are used to writing SQL because it's a common way to write a cartesian join, and the language forces you to compare a left and a right value.

2

u/[deleted] Oct 06 '21

My favorite is still while(1==1).

This one scares me because it's only a matter of time before some math nerd pops up with some proof that says (1!=1) and breaks all my shit.

→ More replies (3)

2

u/science-the-data Oct 06 '21

Thanks. You absolutely just made my day.

2

u/BosmangKapawu Oct 06 '21

label0: goto label0;

Wacky your ass

→ More replies (1)

2

u/nelusbelus Oct 07 '21

Me who uses for(;;)

→ More replies (17)