Array Functions Not Clarified?

I am really not trying to beat a dead horse here lol but this is another example, at least to me, as to changing things up with out properly addressing why it was done. While I do understand the the issue of white space. It would be good to understand why it was done this way rather then the other.

Example shown here:

Again shows the “double” word in the array when we used “int” the code in the previous discussion. I understand the whitespace so I was a bit surprised it was listed like that in the example. I would assume that is the correct way to make your code more readable going forward. Which I for one really want to incorporate best practices so I don’t confuse myself lol.

There is also the wording in the loop, where it says double dmg = damage, why is the word damage shortened??? Just so it all fits on the screen??

Again I am just looking for some clarification on this so I have a good solid basic understanding of c++. Thanks and I am totally new to this so any clarification goes a very long way.

While I’m not Stephen, I think I can give a reasoning and method to the madness. I ultimately think you’re reading into the example a little too much but allow me to do my best to explain.

Let’s start with the use of double, double is just a data type. It happens to allow us to hold numbers with decimal values much like a float can, but it can hold much higher values. For this example it wouldn’t really matter if we used int, float, double, long, or any other data type as long as the data itself matched.

As for double dmg we’re declaring a new variable within the scope of the of the for loop. Since the variable damage was declared outside of that for-loop we can use it inside of the for-loop as well. But that also means we can’t declare another variable with the same name, so we pick a new one instead.

Hopefully this helps explain the slide a bit better. A quick note on “best practice” and whitespace, there’s no hard-fast rule on whitespace in C++. Some programmers hate how the damage array would be declared and others will stand by it until they’re in their graves. Best practice in a more general sense is to use the “style” laid out by the documentation of the platform you’re working with.

1 Like

Thanks I appreciate the feed back. I know from one of his previous examples he did show that some values can hold more decimal values which is why I was confused. I am pretty logical I think most times so when something new gets thrown in without an example or why, that can confuse me. So that does tend to lend me to think to much into something. As for the whitespace, that is a great idea actually.

I think it’s just an example. the dmg variable would probably be used somewhere else outside the for loop. I think the future videos will get in to more detail on using a for loop.

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms