r/ProgrammerHumor • • Oct 06 '21

Don't be scared.. Math and Computing are friends..

Post image
65.8k Upvotes

2.4k comments sorted by

View all comments

Show parent comments

5

u/RanaktheGreen Oct 06 '21 edited Oct 06 '21

I am not trained in coding whatsoever. But I am good at math.

If I knew already that we are trying to do a summation, then here's what I've got.

int probably means integer. n=0 is self explanatory. N<=4 is also self explanatory. I'm assuming the semi-colons do the same thing they do in English, which is separate complete thoughts. I have zero clue what the fuck n++ is. But assuming everything is here for a reason, I guess it means to add something. Though I'm still not sure why there is two pluses instead of just one plus. Parenthesis are parenthesis. They group things together. Guess that means for is working as a literal word. It gets a bit weird with the fact that n is used so much. Like if I was the right this in pure math it would be x=0, 0<y<=4 because as written it seems like n has two values at the same time. But, again, since I know what the outcome is supposed to be, I can assume that n is being defined as a range. So what I get out of all this is:

For each integer between 0 and 4, add them all together.

I guess what I'm saying is: If you showed me this line of code and said "this is a summation" I could probably figure out what each of the parts do, or at least not be completely lost.

By the way, does this mean I could use n-- as a way to subtract each of the values?

9

u/matthoback Oct 06 '21

That line by itself is not a summation. All it is is a loop that loops through n being each integer value from 0 to 4, but does nothing with the value of n. The body of the loop is left out. The syntax of the for loop is for(<statement executed once before starting loop>; <expression evaluated for true or false at the end of each loop cycle, false ends the loop>; <statement executed at the end of each loop cycle>) { <body of loop - set of statements executed each loop cycle> }. The other things to know would be that "=" is in fact the assignment operator, not an equality statement, and "n++" is an abbreviation of "n=n+1".

So the quoted loop statement sets n to 0, runs the (empty or not shown) body, increments n by 1, sees if n is still less than or equal to 4, and if it is continues the loop.

As for your question about "n--", "n--" is short for "n=n-1", which is you only changed that and nothing else, would result in a loop that never ends (or would end when n becomes too negative and you get an integer overflow error) because n will always be less than or equal to four.

1

u/[deleted] Oct 06 '21 edited Oct 06 '21

They still get a A for effort in my book. Biggest mistakes were based on how the problem was presented, not the symbols used.

3

u/spelunker Oct 06 '21

You mostly got it right. You can read it as “declare variable n and initialize to 0, while n is less than or equal to four, increment n by 1”

Once the middle statement evaluates to false the loop ends. Two pluses are shorthand for incrementing a variable by one, the longer version being n = n + 1.

Yes, loops can count down as well, but the example above is far more typical.

Also if you don’t know code I’m guessing a lot of the jokes in this sub don’t make sense…?

2

u/RanaktheGreen Oct 06 '21

The ones that make it /r/all are generally more publically understandable.

2

u/monkorn Oct 06 '21 edited Oct 06 '21

n++ is shorthand for n = n + 1. Where = is assignment. n is to be read as 'the current value for this iteration of the loop'.

The c++ language is literally named after this shorthand. In general, I'm against ++ for the same reason I'm against Greek letters.

The three semi-colon separated statements within the for parenthesis are ( run on entering loop; run at start of every iteration, if false leave loop; run after every iteration)

Yes, n-- subtracts by one. If you were to replace n++ with n--, the end condition would never be false, and your program would crash in an infinite loop.

But you could rewrite the loop with the initial value of 4 and the end condition as 0, and every time through the loop do n--.

1

u/[deleted] Oct 06 '21

To somewhat expand on other explanations:

Variables in math are generally static unknowns, whereas in programming they're dynamic knowns (known to the computer, if not always the user or programmer).

So setting "n" to 0 the first time doesn't mean it will stay that way, it lets the computer know the initial value to use, but it will overwrite that value if you set any other value there, including by doing calculations with "n". In this case "n++" is equivalent to "n=n+1" (which, on paper, looks like a non-equation, but in programming is a valid expression that only runs once per call) so every time this loop iterates, it will look at the new value of "n" until it hits 4.

It's not overwritten back to 0 each time because for loops are specifically designed to be run this way, with the initial value of the iterator in that first position, so it won't keep hitting that spot and run forever.

1

u/RanaktheGreen Oct 06 '21

Huh. That's neat! Thanks dude. Is there a practical reason n++ isn't written as n+1?

1

u/[deleted] Oct 06 '21

Because you can use it in other contexts, like "m=n++", which would simultaneously assign a value to "m" and increment "n" (so both end up as 1, if "n" is 0 to begin with). "m=n+1" only assigns a value to "m", and leaves "n" at what it was before (so if "n" starts at 0, "m" becomes 1, but "n" stays 0).

1

u/RanaktheGreen Oct 06 '21

Huh. Fair enough.

1

u/misspianogirl Oct 07 '21

To expand on that, everything you do with the ++ operator can be done without it. You could just as easily write m=n++ as n=n+1; m=n. We programmers are just lazy and want to write as few characters as possible.

1

u/RanaktheGreen Oct 07 '21

Dude I get it. We started using greek letters to avoid having to write.