Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!sppy00!dwb From: dwb@sppy00.UUCP (David Burhans) Newsgroups: comp.lang.c Subject: Re: More gotos... Keywords: Well, why not? Message-ID: <539@sppy00.UUCP> Date: 11 Sep 89 16:42:27 GMT References: <10353@phoenix.Princeton.EDU> Reply-To: dwb@sppy00.UUCP (David Burhans) Organization: Online Computer Library Center, Dublin, Ohio. Lines: 53 In article <10353@phoenix.Princeton.EDU> tbrakitz@phoenix.Princeton.EDU (Byron Rakitzis) writes: >I came across what I thought was a good application for a goto: >say you're scanning an input stream for tokens, and that you >skip over whitespace. Furthermore, suppose that there is a pair >of comment-delimiter characters. You also want to skip over comments. >So: > >token_grabbing_function() > { > > get_a_token: > > while (is_white_space(c = getchar()); /* skip it */ > > /* So, c contains a non-whitespace character */ > > if (c == comment_delimiter) > { > while (c = getchar() != other_comment_delimiter); /* skip it */ > goto get_a_token; > } > else > { > deal_with_the_token_properly_and_return; > } > } > >Can anyone see a goto-less version which is as concise? >(I'm ignoring EOF errors and the like for the moment) How's this: token_grabbing_function() { while(1) { while(is_white_space(c = getchar()); /* skip it */ if (c == comment_delimiter) while( (c = getchar()) != other_comment_delimiter); else { /* deal with token and return */ } } } -DwB -- dwb@sppy00 {att|killer|pyramid}!osu-cis!sppy00!dwb David Burhans/6565 Frantz Rd./Columbus, Oh 43017 **** Views expressed above were randomly generated with a six sided die and as such are not the views of my employer. *****