this post was submitted on 17 Mar 2025
1364 points (99.7% liked)

Programmer Humor

34491 readers
80 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
[–] idiomaddict 6 points 3 days ago (5 children)

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

[–] Moredekai 41 points 3 days 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 3 days 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] 14 points 3 days ago

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

[–] jqubed 12 points 3 days 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.

[–] JustAnotherKay 7 points 3 days ago* (last edited 3 days 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] 2 points 3 days ago* (last edited 3 days ago)

It’s a For Loop.

Imagine you want something to repeat a certain number of times, or run while a certain variable is set to a specific number.

It starts by setting i = 0. Then it says “while i is less than 10, run.” Then it says “Every time you run, increment i by 1. So it sets i to 0, runs once, sets i to 1, runs again, sets i to 2, etc… And it does this until i is 10, at which point it will stop because i is no longer less than 10.

The actual code you want to run each time i is less than 10 would go below this function.

It’s a pretty basic way to tell a program “do this thing [x] times and then move on.” It’s like coding 101, which is why it’s funny that the person claimed they knew everything except for that part.