Newsgroup: comp.std.c


Article 5155 of comp.std.c:
Newsgroups: comp.std.c
Path: icdoc!dds
From: dds@doc.ic.ac.uk (Diomidis Spinellis)
Subject: Re: Grammar of SWITCH statement
Nntp-Posting-Host: dirty.doc.ic.ac.uk
Message-ID: <1991Jul22.162440.898@doc.ic.ac.uk>
Summary: switch without compund block
Organization: Dept. of Computing, Imperial College, London, England
Keywords: switch block
Date: Mon, 22 Jul 1991 16:24:40 GMT
References: <1678@caslon.cs.arizona.edu> <1991Jul22.004055.23396@zoo.toronto.edu>
Lines: 29
Content-Length: 1178
In article <1991Jul22.004055.23396@zoo.toronto.edu> henry@zoo.toronto.edu (Henry Spencer) writes:
>In article <1678@caslon.cs.arizona.edu> dave@cs.arizona.edu (Dave Schaumann) writes:
>>... why on earth did they make it so general?
[...]
>>	  switch( i ) printf( "%d\n", i ) ;
>
>It is a slightly curious way of doing things.  However, it doesn't cause
>any problems that I know of.

Indeed.  Harbison and Steele in "C: A Reference Manual"  give a code
example where the body of a switch statement is is a non-block
statement.  The intent of the code is to do some processing on a number
depending whether the number is prime or composite.  Common cases are
handled by case labels, the rest by a default case.  Here it is, copied
from memory:

	switch(x)
	default:
		if (isprime(x))
	case 1: case 2: case 3: case 5: case 7: case 11:
			process_prime(x);
		else
	case 4: case 6: case 8: case 9: case 10:
			process_composite(x);

-- 
Diomidis Spinellis                  Internet:                 dds@doc.ic.ac.uk
Department of Computing             UUCP:                    ...!ukc!icdoc!dds
Imperial College, London SW7        #include "/dev/tty"




Newsgroup comp.std.c contents
Newsgroup list
Diomidis Spinellis home page

Creative Commons License Unless otherwise expressly stated, all original material on this page created by Diomidis Spinellis is licensed under a Creative Commons Attribution-Share Alike 3.0 Greece License.