scope of a for loop
what is the scope of i in
for (int i = 0; i < Length; i++) { }
?
does it only exist within the for loop? if i want i to be used outside, i need this right?
this would break?
88 Replies
$tias
ok fair enough
i want my code to look neat tho and ChatGPT did this
It can do that??? How does the for loop know what to use in front of the ;
lmao why are you doing that
i should show the full code hang on
dont do that just use the normal for loop syntax
right now, moveList is a bit longer than numberOfCaptures
so i wanna count numberOfCaptures
so like that?
i dont get it why do you need to do this , you should use normal for loop syntax, if you need to keep track of a value e.g count then use an extra variable
i need numberOfCaptures to splice the array im working with
so my plan is to count it first, then make another loop going up to numberOfCaptures only
it doesn't "know" anything, all 3 expressions inside a for loop's parentheses are optional
for (;;)
is valid codeooo
Unknown User•7d ago
Message Not Public
Sign In & Join Server To View
lmfaoooo true true the LLM sucks
Unknown User•7d ago
Message Not Public
Sign In & Join Server To View
oh
Unknown User•7d ago
Message Not Public
Sign In & Join Server To View
no
Unknown User•7d ago
Message Not Public
Sign In & Join Server To View
no
Unknown User•7d ago
Message Not Public
Sign In & Join Server To View
not really
Unknown User•7d ago
Message Not Public
Sign In & Join Server To View
lmfao
Unknown User•7d ago
Message Not Public
Sign In & Join Server To View
ok
Unknown User•7d ago
Message Not Public
Sign In & Join Server To View
i try my best to understand the code im doing
Unknown User•7d ago
Message Not Public
Sign In & Join Server To View
but sometimes it's hard to find good sources
Unknown User•7d ago
Message Not Public
Sign In & Join Server To View
ChatGPT is surprisingly organised
even though the code it spits out is broken half the time
Unknown User•7d ago
Message Not Public
Sign In & Join Server To View
yea
that's what i use it for mostly
Unknown User•7d ago
Message Not Public
Sign In & Join Server To View
yea so i didnt copy paste the result i came here to ask what the result is doing
Unknown User•7d ago
Message Not Public
Sign In & Join Server To View
ummmm ok
so... what's the scope of a for loop
that was the question i came here with
Unknown User•7d ago
Message Not Public
Sign In & Join Server To View
but i is declared within for()
Unknown User•7d ago
Message Not Public
Sign In & Join Server To View
so this wont work
but then where is the i defined
jimm said
for(;;)
is legalUnknown User•7d ago
Message Not Public
Sign In & Join Server To View
and this was GPT's code
Unknown User•7d ago
Message Not Public
Sign In & Join Server To View
ooo i havent
Unknown User•7d ago
Message Not Public
Sign In & Join Server To View
mmmm ok
and stop using gipity
Unknown User•7d ago
Message Not Public
Sign In & Join Server To View
ngl i kinda overlooked the for loop's docs bcs i use it so often in the
for (int i = 0; i < Length; i++)
so like i kinda assumed that was it
never really thought about what goes on behind the scenes thoUnknown User•7d ago
Message Not Public
Sign In & Join Server To View
i only use it as a glorified search engine when google wont tell me what i want
aight
Unknown User•7d ago
Message Not Public
Sign In & Join Server To View
aha so it's a glorified while loop

ooooooooo ok
Unknown User•7d ago
Message Not Public
Sign In & Join Server To View
wowzers ok
Unknown User•7d ago
Message Not Public
Sign In & Join Server To View
ooo ok
also after reading just a small part of the docs i think i understand what ChatGPT did
Unknown User•7d ago
Message Not Public
Sign In & Join Server To View
It did not include the initializer, so C# defaulted to using whatever value the variable had beforehand
Unknown User•7d ago
Message Not Public
Sign In & Join Server To View
is that correct
Unknown User•7d ago
Message Not Public
Sign In & Join Server To View
makes sense makes sense
just had a stupid idea: can you use a for loop as a while loop
tl;dr
This is fine, a for loop is not required to define a variable to loop, but it does require the semicolons to be defined. As jimmacle mentioned this makes
for(;;)
valid but it would be the same as an infinite while loop. Depends on use case
This is wrong, no such thing as a "normal" loop and what they wrote is very valid
Finally, scope is anything in {}
brackets. You should understand that scope is not a strict thing and {}
is valid anywhere in the code, not just if-statements or things like that. You can nest {}
infinitely and it would technically make a new scope each time. It's just most commonly used in for-loops/if-statements/while-statements because they are tied to execute a single line otherwise and this way you execute a whole scope.
I personally use scopes if I have common names for variables and I define them multiple times, but they vary per context. It's nice being able to wrap them in {}
so the scope gets rid of the name so I can just redefine them later.oh so i can literally just place
{}
anywhere and it would define a scope?Yes
I guess "disposed" is not the right word here but you get what I mean
Ignore the other errors, but notice it has no idea what bar is

This is valid code

Again ignore the errors, this is just the analyzer complaining the method and variable are not actually used
ooo ok
also whyare your "not actually used errors" red instead of green
My project explicitly marks warnings and/or suggestions as errors to improve the code written
mine does that too but green instead of red
If you want you can change it to instead be errors if you want.
Unknown User•6d ago
Message Not Public
Sign In & Join Server To View
green is a suggestion, technically
Unknown User•6d ago
Message Not Public
Sign In & Join Server To View
warnings are yellow, though
suggestions are green and don't really mean anything, they're just options for something you can do
Unknown User•6d ago
Message Not Public
Sign In & Join Server To View
what did you do to your IDE
:kekw:
Unknown User•6d ago
Message Not Public
Sign In & Join Server To View
weird
Unknown User•6d ago
Message Not Public
Sign In & Join Server To View
what underline color are suggestions then
Unknown User•6d ago
Message Not Public
Sign In & Join Server To View
That's pretty nutty
Ah yea this is for code that isn't actually used right