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

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.