this post was submitted on 17 Mar 2025
1259 points (99.8% liked)

Programmer Humor

34426 readers
917 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 45 points 1 day ago (2 children)

Reminds me of the days before ai assistants where people copy pasted code from forums and then you’d get quesitions like “I found this code and I know what every line does except this ‘for( int i = 0; i < 10; i ++)’ part. Is this someone using an unsupported expression?”

[–] [email protected] 6 points 1 day ago (4 children)

I’m less knowledgeable than the OOP about this. What’s the code you quoted do?

[–] [email protected] 40 points 1 day ago (1 children)

It's a standard formatted for-loop. It's creating the integer variable i, and setting it to zero. The second part is saying "do this while i is less than 10", and the last part is saying what to do after the loop runs once -‐ increment i by 1. Under this would be the actual stuff you want to be doing in that loop. Assuming nothing in the rest of the code is manipulating i, it'll do this 10 times and then move on

[–] [email protected] 6 points 1 day ago

I would also add that usually i will be used inside the code block to index locations within whatever data structures need to be accessed. Keeping track of how many times the loop has run has more utility than just making sure something is repeated 10 times.

[–] [email protected] 15 points 1 day ago

It’s a for loop. Super basic code structure.

[–] [email protected] 12 points 1 day ago

@[email protected] posted a detailed explanation of what it’s doing, but just to chime in that it’s an extremely basic part of programming. Probably a first week of class if not first day of class thing that would be taught. I haven’t done anything that could be considered programming since 2002 and took my first class as an elective in high school in 2000 but still recognize it.

[–] [email protected] 5 points 1 day ago* (last edited 1 day ago)

for( int i = 0; i < 10; i ++)

This reads as "assign an integer to the variable I and put a 0 in that spot. Do the following code, and once completed add 1 to I. Repeat until I reaches 10."

Int I = 0 initiates I, tells the compiler it's an integer (whole number) and assigns 0 to it all at once.

I ++ can be written a few ways, but they all say "add 1 to I"

I < 10 tells it to stop at 10

For tells it to loop, and starts a block which is what will actually be looping

Edits: A couple of clarifications

[–] [email protected] 0 points 1 day ago* (last edited 22 hours ago) (2 children)

~~i <= 9, you heathen. Next thing you'll do is i < INT_MAX + 1 and then the shit's steaming.~~

I'm cooked, see thread.

[–] [email protected] 2 points 22 hours ago

<= makes sense if you start from 1.

[–] [email protected] 1 points 1 day ago (1 children)

If it was correct it wouldn’t have been copied into the forums lmao

[–] [email protected] 1 points 1 day ago* (last edited 1 day ago) (1 children)

I mean i < 10 isn't wrong as such, it's just good practice to always use <= because in the INT_MAX case you have to and everything should be regular because principle of least astonishment: That 10 might become a #define FOO 10, that then might become #define FOO INT_MAX, each of those changes look valid in isolation but if there's only a single i < FOO in your codebase you introduced a bug by spooky action at a distance. (overflow on int is undefined behaviour in C, in case anyone is wondering what the bug is).

...never believe anyone who says "C is a simple language". Their code is shoddy and full of bugs and they should be forced to write Rust for their own good.

[–] [email protected] 4 points 23 hours ago* (last edited 20 hours ago) (1 children)

But your case is wrong anyways because i <= INT_MAX will always be true, by definition. By your argument < is actually better because it is consistent from < 0 to iterate 0 times to < INT_MAX to iterate the maximum number of times. INT_MAX + 1 is the problem, not < which is the standard to write for loops and the standard for a reason.

[–] [email protected] 2 points 22 hours ago* (last edited 22 hours ago) (1 children)

You're right, that's what I get for not having written a line of C in what 15 years. Bonus challenge: write for i in i32::MIN..=i32::MAX in C, that is, iterate over the whole range, start and end inclusive.

(I guess the ..= might be where my confusion came from because Rust's .. is end-exclusive and thus like <, but also not what you want because i32::MAX + 1 panics).

[–] [email protected] 0 points 20 hours ago (1 children)
for (int i = INT_MIN; ; i++) {    ...    if (i == INT_MAX) break;}
[–] [email protected] 1 points 20 hours ago

Would you be bold enough to write if (i++ == INT_MAX) break? The result of the increment is never used, but an increment is being done, at least syntactically, and it overflows, at least theoretically, so maybe (I'm not 100% sure) the compiler could be allowed to break out into song because undefined behaviour allows anything to happen.