18
lmute
6y

Can anyone tell me exactly why this is equal to zero?

Comments
  • 16
    read it out loud, it helps, while bigger than 0 do minus 3, so it does:

    12
    9
    6
    3
    0

    then it exits because 0 isn't bigger than 0
  • 6
    What output did you expect and why?
  • 10
    While the number is greater than zero, subtract 3 from it

    So it will run 5 times (15/3=5)

    At the end (after the while loop) the number has a value of 0, because you subtracted 3 from it, 5 times (15-3-3-3-3-3=0)
  • 4
    @JoshBent omygosh duh Thank you so much !!=p
  • 1
    @Scade I was thinking 12
  • 6
    @lmute

    that hair or scratch on your monitor is really disturbing though, so maybe remove that if its not physical damage 😅
  • 1
  • 4
    @Bitwise me too 😅

    especially I hate languages like python where indentation is a part of the semantics of the language
  • 2
    @JoshBent lmfao I'm guessing it was my dogs hair I'm not sure I didn't even notice it hahah.
  • 3
    You can get better help from StackOverflow, Just Saying..
  • 4
    @jaydevs SO will give you -8 for posting this. And the first comment will read "we do not do homework" or something like that.
  • 2
    Well since non of the options is infinite loop and there are no negative numbers in the answer options, answer is 0, got my two grades and pass the exam 😀

    Just how I think in exam no need to be nerdy and ask why happens
  • 1
    It would be 13 if it weren't a loop.

    Not knowing how "imaginary" brackets work as a beginner can be a bit frustrating and confusing.

    So your code really looks like this

    int num = 15;

    while ( num > 0 )

    {

    num = num - 3;

    }

    System.out.println(num);

    In most modern languages (assuming this is probably C, C# or Java), its kind of allowed to just write an if(), a while or for without brackets if its just ONE line of code that gets repeated. You can see the syntax checker questioning your code sometimes if theres a line of code below "imaginary brackets" .

    But the big giveaway here is the spacing. Do you see that num = num - 3 is tabbed to the right?

    Most IDEs do that for you when you write a while, if or for above it. (also else if, else, you get the idea)

    Its an indicator that its a loop for just ONE line.

    If it would be an if(num > 0) num = num - 3; It would be 12, since if's do not loop.
  • 1
    That's what you get when you don't use curly braces in while-loops. At least for me non-curled variants do not register as proper loops in my brain, instead I see it first as a statement that is run only once. I have to do some gazing before my brain registers that there is a loop.

    TL;DR: use curly braces for readability. And yes, I'm this zealous on this matter. :)
  • 2
    Indentation matters ;)
  • 1
    Because 15 is divisible by 3
  • 1
    It would be "13"´,

    15 - 3 = 13

    Yes I can math.
  • 0
    3 more ++ and I can get a sticker =)
  • 0
    15 is multiple of 3. 15=5*3
Add Comment