Tag: Reddit

Discussing the use of break in C

Discussing the use of break in C

C
Image by pramit marattha from Pixabay

On the C programming subreddit there’s a discussion where a student has been old he should never use break in C. As one comment says, “The teacher is a nitwit” and I agree with that comment.

I use break (a) to exit loops early and (b) to exit every branch of a switch statement unless I want it to drop through or else that case does a return. I use switch a few times in the asteroids source code and there’s examples of all three instances of using breaks, using returns and dropping through cases (no break).

Here’s an example of the latter. It controls the logic of what to display when you start or restart a level after losing lose a life. If you have one life left it prints “Last life” otherwise it prints how many you’ve got. Actually I think it’s a bit of an overkill and an if else would have done just as well

 

	switch (Player.lives) {
		case 1:sprintf_s(buffer, sizeof(buffer), "Last Life!");
			break;
		case 2:
		case 3:sprintf_s(buffer, sizeof(buffer), "Lives left: %d", Player.lives);
			break;
		}

So probably not the best example of using a switch! But it illustrates a point.

Where do I get a lot of my information from?

Where do I get a lot of my information from?

SubredditsI was asked this a few months ago. There are various sources including Hackers News and Reddit.com. But reddit is just a collection of specialised sub communities (known as Subreddits) and even I wasn’t aware of quite how many there are.

I came across a link which lists 43 programming related subreddits. On the right hand bar there’ a list of a few Subreddits but at the bottom there’s a View More link to show all 43.

I’ve shown them here to give you an idea of what’s available. The member numbers will change and are often out of date anyway.