C
C#8mo ago
Ewan

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

No description
34 Replies
Ewan
Ewan8mo ago
how can i make it so the error at the bottom doesnt happen?
cap5lut
cap5lut8mo 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
Ewan8mo ago
No description
Ewan
Ewan8mo ago
i get this then ;-;
Ewan
Ewan8mo ago
No description
No description
Ewan
Ewan8mo ago
this is the assignement i was given
cap5lut
cap5lut8mo 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
Ewan8mo ago
how would i change the for loop to not declare it inside? OHHH wait a sec ahh
Ewan
Ewan8mo ago
No description
Ewan
Ewan8mo ago
i then get this?
cap5lut
cap5lut8mo 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
Ewan8mo ago
ah because its a nested loop?
cap5lut
cap5lut8mo ago
yep
Ewan
Ewan8mo ago
how do i make it so it works then? ;-;
cap5lut
cap5lut8mo ago
instead of just int j; u could initialize it with int j = 0;
Ewan
Ewan8mo 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
cap5lut8mo 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
Ewan8mo ago
yeah
cap5lut
cap5lut8mo ago
and with the for loops u print the rectangle, right?
Ewan
Ewan8mo ago
yeah? do i put it in the for loop?
cap5lut
cap5lut8mo ago
yep
Ewan
Ewan8mo ago
ooooooooooooo i see! ok perfect it works! thanks a ton!!!! yay2
cap5lut
cap5lut8mo ago
thats also the only reason why u had the CS0103 errors
Ewan
Ewan8mo ago
i see
cap5lut
cap5lut8mo ago
so basically u can reverse ur code to declaring the variables in the loops again because u only need them in there
Ewan
Ewan8mo ago
ok that makes perfect sense you're a lifesaver, thanks again <a:BE_Yayy:1086338828333228032>
cap5lut
cap5lut8mo 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
Ewan8mo ago
ooo that makes sense, just to make it easier to read and stuff
cap5lut
cap5lut8mo ago
yeah, because its one semantic block
Ewan
Ewan8mo ago
yeah gotcha! JJKGojoNice
cap5lut
cap5lut8mo ago
if u dont have any more questions regarding this topic, then please $close the thread
MODiX
MODiX8mo ago
Use the /close command to mark a forum thread as answered
Ewan
Ewan8mo ago
😄 !close
Accord
Accord8mo ago
Closed!