18
u/taji34 May 03 '17
I shit you not, there is a professor at my University that I TA for who advocates using while loops instead of for loops in all cases. We're working in Java for that class, and it's frustrating because it just leads to more students forgetting to increment their counter or something and getting infinite loops.
20
u/jarfil May 03 '17 edited Dec 02 '23
CENSORED
4
u/taji34 May 03 '17
In theory, yes, but in practice it doesn't work. At least not the way he does it. Furthermore, he encouraged while loop use over for loops in every case, and all of his examples use while loops exclusively, which has the result that students don't learn how to use for loops properly and never use them themselves.
While I agree, forcing them to use while loops for a homework assignment or on a test would be beneficial in teaching them about their counters it should be limited to that. A single assignment or test, not the whole course.
2
u/zoells May 04 '17
UMN?
2
2
u/taji35 May 04 '17
Yep
1
u/here-to-jerk-off May 05 '17
what's going on here? these accounts are 3 and 4 years old, but off by 1?
2
1
u/jesse_dev May 24 '17
In other words, you mean:
while (iAgree()) { if (endOfCourse()) { break; } else { force('use of while loops'); } }
13
6
u/faubiguy May 03 '17
Needs more recursion.
public int fun3(int n)
{
if (n < 2)
{
return 1;
}
else
{
return n * fun3(n - 1);
}
}10
u/prof_hobart May 03 '17
Needs more ternary
public int fun4(int n) { return n<2 ? 1 : n * fun4(n - 1); }
5
5
u/Nikolai-Agnon May 03 '17
While you're at it...
product *= k++;e: Or, if you're like me and are a fan of inverted iterations,
int k = n;
while (k) {
product *= k--;
}3
u/RocketLeague May 03 '17
I don't get it...
4
u/RadioactivMango May 03 '17
He likes while loops, not for loops. (possible shittyness because for loops are useful)
3
3
1
u/sintos-compa May 04 '17
i've been coding since 1980s and i am slightly ashamed about how often i forget to iterate the loop counter in a while loop (while never forgetting it in a for loop)
1
1
u/jamesh1999 Jun 07 '17 edited Jun 07 '17
public int fun(int n)
{
for(int k = n; k; n *= --k) ;
return n;
}Pretty golfed now
:)
1
-16
u/hunyeti May 03 '17 edited May 03 '17
and what's shitty about this?
the for loop above is in fact, a horrible concept.
EDIT: Geez guys, forgot which subreddit is this? you supposed to upvote this.
13
May 03 '17
no, why?
1
u/MyPhallicObject May 25 '17
I must agree, the C style for loop is a horrible concept. It requires three statements and a throwaway variable just to iterate a counter.
Swift has done it correctly:
for _in 1...10. Simple, and no additional variables.10
u/ElectricalAlchemist May 03 '17
They parsed your comment as it was. If you wanted to guarantee correct translation you should have used the sarcasm modifier.
4
u/hunyeti May 03 '17
It's not sarcasm, it's /r/shittyprogramming
2
u/three18ti May 04 '17
And now the story of one Reddit fully of shitty programmers and the one Redditor who had to keep them all together. Iiiittt'sss....
2
May 04 '17
Don'tevenneedalexer / parsergenerator,justhand - rollyourownsarcasmparserafterstrippingoutallwhitespace
Error line 1: unmatched single quote
0
66
u/Laat May 03 '17
A case could be made that k is "leaking" into the block-scope after the for/while loop is finished. In that case (if this works in C), this would be better: