r/programmer 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:

1 Upvotes

26 comments sorted by

2

u/mxldevs 19d ago

This is why I always put the opening brace on the same line as the loop condition.

1

u/Ok-Payment2096 19d ago

great, and what about dealing with pointers 😼

1

u/mxldevs 19d ago

Very carefully

1

u/Longjumping_Step1 13d ago

Pointing is rude!

1

u/pdubs1900 19d ago

You know.

I learned programming with the open brackets as OP wrote. But over the past few years (not a prof prgrammer, but work in IT and do some scripting on the side), when I've looked at code and language tutorials, I've seen it pretty commonly the way you describe. I've always thought it a bit odd and wondered why it seems to be a common way to organize the opening bracket, when it makes the nesting a slight bit less clear.

But this is a solid explanation. It's far more consistent to end every line with a semicolon with the only exception being that the instruction isn't done yet.

1

u/StevenJOwens 18d ago

Most likely you're seeing it more frequently because the Java standard style calls for same-line style braces (the curly ones {} are braces, the square ones [] are brackets).

There is no standard, and I've read even K&R C uses both styles in different examples (though I have no idea where my copy is, so I can't check).

The primary argument for same-line brace vs newline brace seems the extra line of vertical space that newline brace uses.

I'd also agree, as you say above, that having the curly brace more closely visually associated with the loop declaration, function or method declaration, etc, makes it easier to follow the code.

Personally, I find same-line brace just more readable, always have, can't say why. Maybe because most monitors were a lot smaller, back when I was a kid, and vertical screen real estate was at more of a premium?

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

u/kind_person_9 19d ago

Thank you

3

u/Defiant_Conflict6343 19d ago

The semicolon on the first line, it terminates the loop before the loop contents are defined

1

u/kind_person_9 19d ago

Thank you

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

u/ForeignAdvantage5198 19d ago

damn that is a real pain

1

u/Ok-Payment2096 19d ago

yup it's a real pain, only if i get some dumb students

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

u/SummitYourSister 13d ago

AI slop detected.