Home > Articles > Programming > General Programming/Other Languages

  • Print
  • + Share This
This chapter is from the book

Q&A

  • Q. Is it better to use postfix or prefix?

    A. This depends on the needs of your calculations. There is no performance gain using one or the other; it purely depends on how you need to use them. For instance, ++a and a++ both increment a, but the former increments before returning its value, and the latter returns its value and then increments. Be careful of this distinction in your code.

  • Q. Why are there two range operators? Wouldn’t it be simpler to just use one?

    A. Having two range operators allows you to express intent a lot more clearly in code. In the case of a zero-based list, you may have 10 items but the last item’s index is 9, so a half-closed range operator (0..<count, for instance) would be a better fit. If you know you have a finite number of times something should loop, a closed-range operator (1...10, for instance) clearly indicates a range of 1 to 10.

  • Q. What is a practical example of using remainder?

    A. Imagine an app that shows a progress bar when loading. In this case, the progress is only checked occasionally (every nth time) throughout the loading loop, or when the count of the loop % n is equal to 0. This becomes clearer in later hours when we cover conditionals and control flow in Hour 5 and loops in Hour 7.

  • + Share This
  • 🔖 Save To Your Account