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

Programmer Humor

34449 readers
224 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@lemmy.world 6 points 2 days ago (4 children)

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

[–] Moredekai@lemmy.world 41 points 2 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

[–] Fermion@feddit.nl 6 points 2 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.

[–] hperrin@lemmy.ca 15 points 2 days ago

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

[–] jqubed@lemmy.world 12 points 2 days ago

@Moredekai@lemmy.world 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@lemmy.world 7 points 2 days ago* (last edited 2 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