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;
edit: formatting
38 Replies
if you use the $code formatting it will be easier to read
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/$codegif
ty lemme do that rq
then you can use spaces instead of tabs to indent too (as you should :when: )
im really new thank you for telling me!
alright it's updated
what's the first part that's confusing?
I mean from what he taught us, or at least what I remember, the bot of code saying
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
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"
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
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
Jimmacle
REPL Result: Success
Console Output
Compile: 394.454ms | Execution: 38.493ms | React with ❌ to remove this embed.
but the answer isn't 3 either way :KEKW:
😭
how tf did he get three??
Conclusion: Your teacher is wrong 😄
thank you, I thought I was an idiot for a second
Unless you missed something somwhere of course... But the code execution doesn't lie.
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
ignore the top two it's just the bottom one
well we can go through it step by step
that would be nice
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)
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?
sure
(ints score and points are the same value as before)
you could try unrolling the loop to think about it differently
that code is the same as
so you'd get 50?
what operation are you doing to score with
=
?ohh
so just 10?
yeah
got it
it's just setting it to 10 5 times in a row
I think I finally get this now
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
I'll keep that in mind for the future, thank you all so much!
Unknown User•10mo ago
Message Not Public
Sign In & Join Server To View