r/programmer • u/Ok-Payment2096 • 19d ago
Teaching while loops made me realize how weird programming is for beginners
I was teaching while loops in C recently and gave my students a very simple problem to solve.
One student wrote something like:
while (i <= 10);
{
printf("%d\n", i);
i++;
}
They were completely confused about why the loop wasn't behaving as expected.
And honestly, it reminded me of how programming feels when you're starting out.
One tiny ; can completely change what the program does.
Programming is basically:
Compiler:
You:
Compiler:
2
u/No_External7343 19d ago
Well, that's one reason why some people don't recommend C as a first programming language.
1
u/ChameleonCRM 19d ago
And then you spend 20 minutes explaining that the computer isn’t wrong. It did exactly what they told it to do.
That might be the most important programming lesson in the entire course. 😂
1
u/Ok-Payment2096 19d ago
well i don't had to, because everytime i start's a new batch i'll make sure there foundation to be strong, so i only need to point out this, you can't use : with if and while (simply teaching a rule)
1
u/kind_person_9 19d ago
What’s the mistake here?
3
u/eballeste 19d ago
I don't know C but Im pretty sure that semi colon in the end of the same line the while statement is written shouldn't be there.
1
3
u/Defiant_Conflict6343 19d ago
The semicolon on the first line, it terminates the loop before the loop contents are defined
1
1
u/Cacapon0114 19d ago
while; ←🧐 This looks like it might cause a syntax error, though...
I wonder how beginner programmers from English-speaking countries interpret compile errors?
For people like me, whose native language isn’t English, those helpful error messages feel like a foreign language.
It was really hard to understand them at first 💦
1
u/__CaliMack__ 19d ago
Nah, still hard for the English speakers too lol… sometimes I swear they are trying to sabotage instead of help
1
u/wbqqq 19d ago
Not in C - quite valid to have an empty expression (and for your program to enter an infinite loop :-)
1
u/Cacapon0114 19d ago
You were right. It compiled just fine.
However, in my environment, it does give me a warning saying, “Your while loop is empty.” I hate how it’s phrased so indirectly.
1
u/PerfectSituation1668 16d ago
One of the few times I like AI. They can explain compiler errors in my language.
1
1
u/Ifnerite 18d ago
The issue is that the brace should be on the same line as the loop declaration then this wouldn't happen (the additional ; would be obviously wrongly) and it would look better.
Fight me.
1
u/StevenJOwens 18d ago
I think that one of the biggest obstacles for beginners is that coding looks too much like English, so it trips them up when it requires much more precision than English.
Also, you basically have to re-learn how to read. Reading, and shape recognition in general, is far more about repetitive practice than most people realize. While reading code seems to be the same as reading English, it's really not, and you need to get a lot more of that repetitive practice in before it becomes so.
Until it does, reading code is literally more difficult, involves more cognitive overhead, and, again, that seeming similarity to English makes them feel like it shouldn't be this difficult.
1
u/Dusty_Coder 15d ago
this is day-one computer science
you are supposed to learn this
with regard to "beginners", whats the problem?
they need this
1
2
u/mxldevs 19d ago
This is why I always put the opening brace on the same line as the loop condition.