Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!seismo!lll-crg!lll-lcc!styx!twg-ap!amdahl!hplabs!pesnta!amd!amdcad!amdimage!prls!philabs!pwa-b!mmintl!franka From: franka@mmintl.UUCP (Frank Adams) Newsgroups: net.lang.c++ Subject: Re: for != while Message-ID: <1793@mmintl.UUCP> Date: Mon, 8-Sep-86 18:38:37 EDT Article-I.D.: mmintl.1793 Posted: Mon Sep 8 18:38:37 1986 Date-Received: Sun, 14-Sep-86 03:31:37 EDT References: <749@tekla.UUCP> Reply-To: franka@mmintl.UUCP (Frank Adams) Organization: Multimate International, E. Hartford, CT Lines: 46 In article <749@tekla.UUCP> in net.lang.c dant@tekla.UUCP writes: >>>In article <86900030@haddock> karl@haddock writes: >>>>It's well known that the equivalence between for and while breaks down if >>>>there's a "continue" statement. Here's another case I just discovered: >>>> >>>>main() { >>>> char *foo = "outer"; >>>> for (;; printf(foo),exit(0)) { >>>> char *foo = "inner"; >>>> } >>>>} >>>> >>>>This prints "outer" (vax SVR2 compiler), though the for-while equivalence >>>>might lead one to expect "inner". >the ... above is correct. The >equivalence of the ... example is this: > >main() { > char *foo = "outer"; > while () { > { > char *foo = "inner"; > } > printf(foo),exit(0)); > } >} > >The *foo = "inner" applies only within the inner braces (which >delineate the statement). The printf(foo) goes outside those braces. This is fine as far as C goes. In C++, one may instead write main() { char *foo = "outer"; for (;; printf(foo),exit(0)) char *foo = "inner"; } which should, in fact, print "inner"[1]. I don't currently have accesss to the C++ processor; does anyone know whether these two examples in fact work correctly for it? Frank Adams ihnp4!philabs!pwa-b!mmintl!franka Multimate International 52 Oakland Ave North E. Hartford, CT 06108 [1] Actually, my example may be an illegal multiple definition of foo. In this case, try it with the "outer" declaration removed.