Ask coding questions

← Back to all posts
Why is there a While loop in the code?(not my code)

I found a task on CodeAbbey

I decided to look at other people's solutions and I found this code and I don't understand why there is a loop here?

Answered by SixBeeps [earned 5 cycles]
View Answer

This is actually an interesting solution...

So in C++, ++ and -- not only change the variable, but also returns either the old or new value of the variable depending on where the operator is placed. So, these two code samples are the same.

In here, the while loops look at the values of number or counter, which will break once their values go below 0. That means these two are the same as well.

It's neat, but I don't think I'd ever see it in production-level code.