Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Game of life from K&R: question about switch statement |
| Date | 2015-11-17 11:46 -0500 |
| Organization | A noiseless patient Spider |
| Message-ID | <n2flgr$f1q$1@dont-email.me> (permalink) |
| References | <16998259-e524-4529-94e3-8e17be7041fa@googlegroups.com> |
On 11/17/2015 11:34 AM, Alla _ wrote:
> Hello!
>
> Please, take a look a the program from K&R tutorial; it has descriptive
> comments, therefore I will skip adding my ones. I would be grateful
> for the help.
>
> I am not sure I understand how the switch statement works within
> BuildNextGeneration () function. I read it as:
>
> if a value at the position array[i][j] is '*', then the program
> has to choose between 3 cases. Given each case in switch statement should
> have a certain value to which the condition is compared, I don't
> understand what 2 or 3 represent:
The three cases are "two neighbors," "three neighbors," and "other."
This suggests a switch statement with the general outline:
switch (neighbors) {
case 2:
// handle "two neighbor" case
break;
case 3:
// handle "three neighbors" case
break;
default:
// handle "other" case
break;
}
It turns out that the handling is the same for the two- and three-
neighbors cases, so we can combine those two in the switch:
switch (neighbors) {
case 2:
case 3:
// handle "two neighbors" or "three neighbors"
break;
default:
// handle "other" case
break;
}
This could have been written in other ways, like
if (neighbors == 2 || neighbors == 3) {
// handle "two neighbors" or "three neighbors"
} else {
// handle "other" case
}
... but I'm guessing the point of the lesson is to teach you about
the switch statement. Too bad you've not been given a more compelling
example.
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Game of life from K&R: question about switch statement Alla _ <modelling.data@gmail.com> - 2015-11-17 08:34 -0800
Re: Game of life from K&R: question about switch statement Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-11-17 11:46 -0500
Re: Game of life from K&R: question about switch statement James Kuyper <jameskuyper@verizon.net> - 2015-11-17 11:56 -0500
Re: Game of life from K&R: question about switch statement glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2015-11-17 20:19 +0000
Re: Game of life from K&R: question about switch statement "Charles Richmond" <numerist@aquaporin4.com> - 2015-11-19 17:48 -0600
Re: Game of life from K&R: question about switch statement Stephen Sprunk <stephen@sprunk.org> - 2015-11-17 12:27 -0600
Re: Game of life from K&R: question about switch statement Keith Thompson <kst-u@mib.org> - 2015-11-17 15:12 -0800
csiph-web