C
C#14mo ago
Ewan

✅ How come i and j doesnt exist in the current context?

No description
34 Replies
Ewan
EwanOP14mo ago
how can i make it so the error at the bottom doesnt happen?
cap5lut
cap5lut14mo ago
they only exist within their for loops, u would have to declare them outside and before. so something like
int i;
int j;
for (i = 0; ...
int i;
int j;
for (i = 0; ...
but what do u try to achieve here? i will be the same value as width and j same as height because that if/else is after all the loop iterations are done
Ewan
EwanOP14mo ago
No description
Ewan
EwanOP14mo ago
i get this then ;-;
Ewan
EwanOP14mo ago
No description
No description
Ewan
EwanOP14mo ago
this is the assignement i was given
cap5lut
cap5lut14mo ago
yeah, because now u try to declare it twice (once at the top and once in the loop), so in the loop u do not have to declare them any more declaration in loop:
for (int i = 0; ....)
for (int i = 0; ....)
declaration outside of the loop
int i;
for (i = 0; ....)
int i;
for (i = 0; ....)
Ewan
EwanOP14mo ago
how would i change the for loop to not declare it inside? OHHH wait a sec ahh
Ewan
EwanOP14mo ago
No description
Ewan
EwanOP14mo ago
i then get this?
cap5lut
cap5lut14mo ago
aah, yes, thats because the compiler isnt smart enough to know that the outer for loop will run at least once, so it doesnt see that for (j = 0; would have happened
Ewan
EwanOP14mo ago
ah because its a nested loop?
cap5lut
cap5lut14mo ago
yep
Ewan
EwanOP14mo ago
how do i make it so it works then? ;-;
cap5lut
cap5lut14mo ago
instead of just int j; u could initialize it with int j = 0;
Ewan
EwanOP14mo ago
oooo ok that runs without any errors which is good, however it doesnt make the red and yellow pattern oh here at the end
cap5lut
cap5lut14mo ago
hm, hard to explain without spoonfeeding the answer so basically ur code is executed sequentially from top to bottem right? (the for loops repeat a couple steps ofc)
Ewan
EwanOP14mo ago
yeah
cap5lut
cap5lut14mo ago
and with the for loops u print the rectangle, right?
Ewan
EwanOP14mo ago
yeah? do i put it in the for loop?
cap5lut
cap5lut14mo ago
yep
Ewan
EwanOP14mo ago
ooooooooooooo i see! ok perfect it works! thanks a ton!!!! yay2
cap5lut
cap5lut14mo ago
thats also the only reason why u had the CS0103 errors
Ewan
EwanOP14mo ago
i see
cap5lut
cap5lut14mo ago
so basically u can reverse ur code to declaring the variables in the loops again because u only need them in there
Ewan
EwanOP14mo ago
ok that makes perfect sense you're a lifesaver, thanks again <a:BE_Yayy:1086338828333228032>
cap5lut
cap5lut14mo ago
glad i could help o7 and one last tip, regarding the code when u declared i and j outside of the loops: its good practice to declare them close to where u use them, so in that case that would have been directly above for loops
Ewan
EwanOP14mo ago
ooo that makes sense, just to make it easier to read and stuff
cap5lut
cap5lut14mo ago
yeah, because its one semantic block
Ewan
EwanOP14mo ago
yeah gotcha! JJKGojoNice
cap5lut
cap5lut14mo ago
if u dont have any more questions regarding this topic, then please $close the thread
MODiX
MODiX14mo ago
Use the /close command to mark a forum thread as answered
Ewan
EwanOP14mo ago
😄 !close
Accord
Accord14mo ago
Closed!
Want results from more Discord servers?
Add your server