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?
i = x
for (int i = 0; i < Length; i++) { }
i = x
for (int i = 0; i < Length; i++) { }
this would break?
for (int i = 0; i < Length; i++) { }
x = i
for (int i = 0; i < Length; i++) { }
x = i
88 Replies
ElectricTortoise
ElectricTortoiseOP2mo ago
ok fair enough i want my code to look neat tho and ChatGPT did this
int i = 0;
for (; i < Length; i++)
{
//Do stuff
}
int i = 0;
for (; i < Length; i++)
{
//Do stuff
}
It can do that??? How does the for loop know what to use in front of the ;
سَلْمَانْ  حَيدَرْ
lmao why are you doing that
ElectricTortoise
ElectricTortoiseOP2mo ago
i should show the full code hang on
int numberOfCaptures = 0;
for (; numberOfCaptures < moveList.Length; numberOfCaptures++)
{
if ((moveList.Moves[numberOfCaptures] >> 12) != (int)MoveFlag.Capture)
break;
}
int numberOfCaptures = 0;
for (; numberOfCaptures < moveList.Length; numberOfCaptures++)
{
if ((moveList.Moves[numberOfCaptures] >> 12) != (int)MoveFlag.Capture)
break;
}
سَلْمَانْ  حَيدَرْ
dont do that just use the normal for loop syntax
ElectricTortoise
ElectricTortoiseOP2mo ago
right now, moveList is a bit longer than numberOfCaptures so i wanna count numberOfCaptures
int numberOfCaptures;
for (numberOfCaptures = 0; numberOfCaptures < moveList.Length; numberOfCaptures++)
{
if ((moveList.Moves[numberOfCaptures] >> 12) != (int)MoveFlag.Capture) { break; }
}
int numberOfCaptures;
for (numberOfCaptures = 0; numberOfCaptures < moveList.Length; numberOfCaptures++)
{
if ((moveList.Moves[numberOfCaptures] >> 12) != (int)MoveFlag.Capture) { break; }
}
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
ElectricTortoise
ElectricTortoiseOP2mo ago
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
Jimmacle
Jimmacle2mo ago
it doesn't "know" anything, all 3 expressions inside a for loop's parentheses are optional for (;;) is valid code
ElectricTortoise
ElectricTortoiseOP2mo ago
ooo
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP2mo ago
lmfaoooo true true the LLM sucks
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP2mo ago
oh
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP2mo ago
no
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP2mo ago
no
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP2mo ago
not really
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP2mo ago
lmfao
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP2mo ago
ok
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP2mo ago
i try my best to understand the code im doing
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP2mo ago
but sometimes it's hard to find good sources
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP2mo ago
ChatGPT is surprisingly organised even though the code it spits out is broken half the time
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP2mo ago
yea that's what i use it for mostly
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP2mo ago
yea so i didnt copy paste the result i came here to ask what the result is doing
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP2mo ago
ummmm ok so... what's the scope of a for loop that was the question i came here with
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP2mo ago
for (int i = 0; i < Length; i++) { }
x = i
for (int i = 0; i < Length; i++) { }
x = i
but i is declared within for()
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP2mo ago
so this wont work but then where is the i defined jimm said for(;;) is legal
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP2mo ago
int i = 0;
for (; i < Length; i++)
{
//Do stuff
}
int i = 0;
for (; i < Length; i++)
{
//Do stuff
}
and this was GPT's code
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP2mo ago
ooo i havent
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP2mo ago
mmmm ok
SG97
SG972mo ago
and stop using gipity
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP2mo ago
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 tho
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP2mo ago
i only use it as a glorified search engine when google wont tell me what i want
SG97
SG972mo ago
aight
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP2mo ago
aha so it's a glorified while loop
No description
ElectricTortoise
ElectricTortoiseOP2mo ago
ooooooooo ok
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP2mo ago
wowzers ok
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP2mo ago
ooo ok also after reading just a small part of the docs i think i understand what ChatGPT did
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP2mo ago
It did not include the initializer, so C# defaulted to using whatever value the variable had beforehand
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP2mo ago
is that correct
Unknown User
Unknown User2mo ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP2mo ago
makes sense makes sense just had a stupid idea: can you use a for loop as a while loop
FusedQyou
FusedQyou2mo ago
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.
ElectricTortoise
ElectricTortoiseOP2mo ago
oh so i can literally just place {} anywhere and it would define a scope?
FusedQyou
FusedQyou2mo ago
Yes
void Main()
{
int foo = 0;

{
int bar = 0;

// Legal, defined in parent scope
foo = 5;
}

// Not legal, defined in disposed scope
bar = 5;
}
void Main()
{
int foo = 0;

{
int bar = 0;

// Legal, defined in parent scope
foo = 5;
}

// Not legal, defined in disposed scope
bar = 5;
}
I guess "disposed" is not the right word here but you get what I mean
FusedQyou
FusedQyou2mo ago
Ignore the other errors, but notice it has no idea what bar is
No description
FusedQyou
FusedQyou2mo ago
This is valid code
No description
FusedQyou
FusedQyou2mo ago
Again ignore the errors, this is just the analyzer complaining the method and variable are not actually used
ElectricTortoise
ElectricTortoiseOP2mo ago
ooo ok also whyare your "not actually used errors" red instead of green
FusedQyou
FusedQyou2mo ago
My project explicitly marks warnings and/or suggestions as errors to improve the code written
ElectricTortoise
ElectricTortoiseOP2mo ago
mine does that too but green instead of red
FusedQyou
FusedQyou2mo ago
If you want you can change it to instead be errors if you want.
Unknown User
Unknown User5w ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle5w ago
green is a suggestion, technically
Unknown User
Unknown User5w ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle5w ago
warnings are yellow, though suggestions are green and don't really mean anything, they're just options for something you can do
Unknown User
Unknown User5w ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle5w ago
what did you do to your IDE :kekw:
Unknown User
Unknown User5w ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle5w ago
weird
Unknown User
Unknown User5w ago
Message Not Public
Sign In & Join Server To View
Jimmacle
Jimmacle5w ago
what underline color are suggestions then
Unknown User
Unknown User5w ago
Message Not Public
Sign In & Join Server To View
ElectricTortoise
ElectricTortoiseOP5w ago
That's pretty nutty Ah yea this is for code that isn't actually used right

Did you find this page helpful?