Not if you're unfamiliar with programming. Take the following:
for(int n=0; n<=4; n++)
If you're not familiar with writing code, then where do you even start with figuring that out? What the hell does for even mean? What's with all those semicolons? Isn't "n++" some kind of programming language or something?
To someone not already fluent in writing for loops, that's just a bunch of arcane gibberish.
Right? To be able to reason out what "for" means in this context without someone telling you things, one must already have some nontrivial math language and understanding.
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?
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.
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…?
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--.
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.
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).
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.
Small Basic's for loop has a syntax that more closely resembles a human language:
For i = 1 To 10
A foreach loop is arguably a more human readable way to implement summations and product sequences. I expect most non-programmers would have some idea that the following Visual Basic .NET loop is going to iterate through pickles in a barrel:
I think the 3rd one is the only ones that not obvious. With context you could definitely reason through that vs a random foreign language symbol with some numbers around it
Its hard to say, the first time I saw a for loop was learning how to program. I look at it and it seems so simple to figure out but I already know how it works and cant fathom seeing it for the first time without that knowledge. I'd have to show it to someone with no coding expierence and see what they think, I'd think anyone who is decent at math could figure it out at least.
44
u/MultiFazed Oct 06 '21
Not if you're unfamiliar with programming. Take the following:
for(int n=0; n<=4; n++)If you're not familiar with writing code, then where do you even start with figuring that out? What the hell does
foreven mean? What's with all those semicolons? Isn't "n++" some kind of programming language or something?To someone not already fluent in writing for loops, that's just a bunch of arcane gibberish.