commentsCP

Comments for Intro to Computer Programming (all chapters.)

Misspellings, wrong code, missing parts, general things I messed-up; suggestions.

18 thoughts on “commentsCP”

  1. Hello, I posted on the Unity Forums as I think UnityFan18 or something like that. I have a question about Chapter 2 Section 5.4. I am loving this resource, and just wanted to point something out that I noticed.

    In the section, you mention about the assignment order rule concerning when you are assigning integer values to float variables.

    You use the following example,

    f = 12/5; // 2.0f
    f = 2.5f + 8/3; // 4.5 — 2.5+2

    Afterwards, you have the following statement

    The computer doesn’t care that 6/5 will be going into a float. Is that supposed to be a 6? Or is it supposed to say 12/5?

    1. Thanks for picking up that inconsistency. The 6/5 in the text below is wrong (updating it now.)

      At first, I had 6/5’s in both places. But then I though 6/5=1 looked more like a trick, so changed the top line to 12/5 – made it look more “dividy.” Forgot to look around. And I should know better than to make one change and not test it. After all, programming is the same way.

  2. I also had another question for the same section.
    In the following part,

    f = 2.5f + 8/3; // 4.5 — 2.5+2

    I understand that the 8/3 part goes first. In addition, because those two values are integers the result will be a integer. My question is that when 8/3 the result is 2.67. Thus, why would the computer round down to 2 as opposed rounding up to a value of 3?

    1. That happens to everyone. It’s what all those example are for – something it took me teaching the same class a few times to learn.

      First you see 8/3 truncating to 2, in the integer section all by itself, and it’s fine; then maybe you remember the rule when you see 1.2f + 8/3 in the mixed-types section. But when you get to variables AND assignment AND mixed-types AND integer division — all those rules together seem to just slide around. You have to see the same thing in a few different contexts before you really know it.

  3. I have another question about Chapter 2 section 8
    with the chapter heading of “Semi-useful variable and assign examples”
    The following line is posted

    line2 = “”Average damage is ” + dps + “/second”;

    Should it read as line2 = ” Average damage is ” + dps + “/second”;

    I just wanted to double check because Im so used to seeing the double quotes going around the string

    1. Thanks – yes, that’s a pure typo – I added an extra double-quote in [ line2=”” ] by mistake. I really did run these in C# first, then paste into the text. Well, most of them.

      Maybe subconsciously I’m ambivalent about that example. I used it back when all games listed base damage and attack speed, and you learned about computing DPS. Now, I think games only list DPS.

  4. I have another question. In the section titled Chapter 2 section 8
    with the chapter heading of “Semi-useful variable and assign examples”. In the part where you mention the example of increasing something by 10% the following print statements are posted

    print(“Month ” + months + “: + cash);
    print(“Month ” + months + “: + cash);
    print(“Month ” + months + “: + cash);

    There should a double quote around the colon to indicate that is a string correct?

    1. Gah. Thanks. I was just looking at that section, too. It’s like when you write “the the.” You can read it over and over and never notice there are two of them. And, for that matter, why didn’t I use the more interesting “: $”?

      While I was looking, I went over all of chapter 2 and rewrote parts of it.

  5. In Chapter 3 section 3 where you are talking about the differences betwen the Update and Start programs in Unity, I found a type in the line below with the allow, I think should be allowed in this case. Just wanted to update you. Enjoying this great resource :).

    You’re allow to use either one by
    itself, or both.

  6. Hello, I had another question in Chapter 3 in the section that is entitled More Cute Cube Tricks there is a section where you are refering to xSpeed.

    public float x=-7.0f;
    public float xSpeed=0.1f;
    public float xAccel=-0.0002f; // new. x acceleration variable

    however, in the lines below you have

    A fun trick is to start xSpd at 0

    Did you want us to name the float xSpeed or xSpd for that example?

    1. Opps. Thanks. xSpd and xSpeed are supposed to be the same variable – I just messed up using the same name everywhere.

      The thing I was trying to show was, with xSpeed as negative number, the code feels like “gradually reverse my direction.” But if you start the xSpeed at just 0, the code feels totally different, like “slowly accelerate from being stationary.” It’s kind of cool that just tweaking numbers can feel like those two different things.

  7. Hello, In the same chapter, the following line was posted

    public float r = 0;
    public float rSpd = 0.005; // how fast it changes

    I tried to run that script but it gave me an error. I changed the lines to this, and it resolved the error. I just wanted to double check if I had done the correct the issue because the error was saying “Literal of type double cannot be implicitly converted to type `float’. Add suffix `f’ to create a literal of this type”

    public float r = 0f;
    public float rSpd = 0.005f;

    1. Opps. Thanks. Forgot I the f on the 2nd one. The first one doesn’t need the f – just “float r=0;” is legal. People use that shortcut all the time. It’s technically a wrong type – an int to a float – but the computer is OK with that. And again, arg! – I’m inconsistently using Spd here when I used Speed before. Old habits.

  8. In the chapter 4 section regarding if statements I just wanted to make sure that Im understanding lines of code that you had provided that functions. So in this case we wouldnt need the curly braces in the body of the If statement because we are just using one line with different variations? In addition, would we need to declare num as a integer above as well?

    string numWord = “unknown num”;
    if(num==1) numWord=”one”;
    if(num==2) numWord=”two”;
    if(num==3) numWord=”three”;
    if(num==4) numWord=”four”;
    if(num>=5) numWord=”many”;

    1. Right about num — the example is nicer if we say “somewhere above, n was declared and given a value.” It’s the same idea as leaving out Start/Update. We can focus on “turn some number into a word, using ifs.”

      The missing parens rule is simpler than that. It’s in the rules above, sort of hidden. I just used it here for the first time since all those extra {}s make things hard to read (and everyone one else would leave them out.)

  9. Hi, I just wanted to say I think I found a typo in the section talking about if statements.

    The following line is missing the word “you”
    If you really want to, can use an empty body {} for either

    1. Thanks. I was trying to use an english idiom where the subject is left-out: “Could try it. Might work. Going there now.” I was hoping that being informal would emphasize you shouldn’t use {} after an IF on purpose and this was just a fun example. But the section looks a little awkward to me. I should just rewrite it.

  10. Hello, I think I found a piece of incorrect code but I wanted to make sure. So in the following statement
    if(n==2 || n==4 || n==6 || n=8)

    the last one should read (n==8) instead correct because we want to make sure that it is equal as opposed to assigning it a new value.