I need help with for loop homework :(

My coding teacher wants me to tell how he got 3 from the following code but I don't get it at all since this is like my third class and he doesn't exactly teach in a way I understand things easily. Could someone just tell me what's going on here? Help would be greatly appreciated since I don't want him getting mad at me! int score = 0; int points = 10;
int score = 0;
int points = 10;

for(int i = 0; i<3; i++)
{
for (int j = 0; j<10; j++)
{
score += points;
}
score /= points;
}
int score = 0;
int points = 10;

for(int i = 0; i<3; i++)
{
for (int j = 0; j<10; j++)
{
score += points;
}
score /= points;
}
edit: formatting
38 Replies
Jimmacle
Jimmacle10mo ago
if you use the $code formatting it will be easier to read
MODiX
MODiX10mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
BlangleglorbGiggledoo
$codegif
BlangleglorbGiggledoo
ty lemme do that rq
Jimmacle
Jimmacle10mo ago
then you can use spaces instead of tabs to indent too (as you should :when: )
BlangleglorbGiggledoo
im really new thank you for telling me! alright it's updated
Jimmacle
Jimmacle10mo ago
what's the first part that's confusing?
BlangleglorbGiggledoo
I mean from what he taught us, or at least what I remember, the bot of code saying
for(int i = 0; i<3; i++)
for(int i = 0; i<3; i++)
means that everything below loops 3 times, then the bit inside will add 10, 10 times so what I'm getting is 3 loops of 100 (10 loops of adding ten) then that's divided by 10 and I got 30
Jimmacle
Jimmacle10mo ago
you have it mostly right, but when everything loops everything loops it doesn't do the 3 loops of 100 first and then divide, it's 3 loops of "add 100 then divide"
BlangleglorbGiggledoo
Alright, thanks for that, but wouldn't that still be 30? (3 loops of add 100 then divide by 10) I believe I put up there that he got 3
Jimmacle
Jimmacle10mo ago
you aren't dividing by 10 once, you're dividing by 10 after every time you add 100 every line inside the loop gets repeated
MODiX
MODiX10mo ago
Jimmacle
REPL Result: Success
int score = 0;
int points = 10;

for(int i = 0; i<3; i++)
{
for (int j = 0; j<10; j++)
{
score += points;
}
score /= points;
}

Console.WriteLine(score)
int score = 0;
int points = 10;

for(int i = 0; i<3; i++)
{
for (int j = 0; j<10; j++)
{
score += points;
}
score /= points;
}

Console.WriteLine(score)
Console Output
11
11
Compile: 394.454ms | Execution: 38.493ms | React with ❌ to remove this embed.
Jimmacle
Jimmacle10mo ago
but the answer isn't 3 either way :KEKW:
BlangleglorbGiggledoo
😭 how tf did he get three??
z0mb
z0mb10mo ago
Conclusion: Your teacher is wrong 😄
BlangleglorbGiggledoo
thank you, I thought I was an idiot for a second
z0mb
z0mb10mo ago
Unless you missed something somwhere of course... But the code execution doesn't lie.
BlangleglorbGiggledoo
yeah I typed everything on my paper he handed us a paper so I typed down what was on it I could upload a pic in case I got sm wrong give me a sec
BlangleglorbGiggledoo
ignore the top two it's just the bottom one
Jimmacle
Jimmacle10mo ago
well we can go through it step by step
BlangleglorbGiggledoo
that would be nice
Jimmacle
Jimmacle10mo ago
1. 0 + 100 = 100, divided by 10 is 10 2. 10 + 100 = 110, divided by 10 is 11 3. 11 + 100 = 111, divided by 10 is 11 (because we're using integers the 0.1 part gets lost)
BlangleglorbGiggledoo
OHHH I think I was told the wrong thing then WAIT I think I know what he was telling me now I think what he was thinking was add 100 then divide by ten (middle loop) then divide by 10 again (last bit) 3 times and he got 3 so we at least know how he did it but I get what you said and it makes a lot more sense I have one more thing that looks really simple but he never explained and I have like four braincells dinking around my head, could you explain one last problem?
Jimmacle
Jimmacle10mo ago
sure
BlangleglorbGiggledoo
(ints score and points are the same value as before)
for(int i = 0; i<5; i++)
{
score = 10
}
for(int i = 0; i<5; i++)
{
score = 10
}
Jimmacle
Jimmacle10mo ago
you could try unrolling the loop to think about it differently that code is the same as
score = 10;
score = 10;
score = 10;
score = 10;
score = 10;
score = 10;
score = 10;
score = 10;
score = 10;
score = 10;
BlangleglorbGiggledoo
so you'd get 50?
Jimmacle
Jimmacle10mo ago
what operation are you doing to score with =?
BlangleglorbGiggledoo
ohh so just 10?
Jimmacle
Jimmacle10mo ago
yeah
BlangleglorbGiggledoo
got it
Jimmacle
Jimmacle10mo ago
it's just setting it to 10 5 times in a row
BlangleglorbGiggledoo
I think I finally get this now
z0mb
z0mb10mo ago
As a side note, on the topic of loops, especially for loops, make sure to read up on "Off By One" errors. It's a common enough mistake, especially when learning
BlangleglorbGiggledoo
I'll keep that in mind for the future, thank you all so much!
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server