Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.c
Re: Exercise 1-8 of the C programming language
Seebs
8/13/2011 6:05:00 AM
On 2011-08-13, Joseph Santoyo <josephsantoyo@gmail.com> wrote:
> Is there any way to write this code without an else if?
Sure!
> If so how..and
> can you explain it line by line please :/ I'm really new to C or
> programming.
Welcome!
> /* count blanks, tabs and newlines in input */
> main()
You should probably spell this
int
main(void)
in modern C.
> if (c == ' ')
> ++bl;
> else if (c == '\t')
> ++t;
> else if (c == '\n')
> nl;
(You missed the "++" on the last one; it should be "++nl".)
You can write this without else if as follows:
> if (c == ' ')
> ++bl;
> if (c == '\t')
> ++t;
> if (c == '\n')
> ++nl;
It's not necessarily any better or more efficient, but it'll work.
The way I'd probably do it would be:
switch (c) {
case ' ': ++bl; break;
case '\t': ++t; break;
case '\n': ++nl; break;
default: break;
}
'switch' jumps to a case matching the controlling expression, then executes
code until it hits a 'break'. It's often relatively efficient for cases
where there are many options; compilers may generate smarter code than for
the corresponding sequence of else if ()s.
> printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl);
.... and
return 0;
here.
> }
-s
--
Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nospam@seebs.net
http://www.seeb...
<-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/...
(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
1 Answer
Joseph Sanoyo
8/13/2011 6:40:00 AM
0
On Aug 13, 2:04 am, Seebs <usenet-nos...@seebs.net> wrote:
> On 2011-08-13, Joseph Santoyo <josephsant...@gmail.com> wrote:
>
> > Is there any way to write this code without an else if?
>
> Sure!
>
> > If so how..and
> > can you explain it line by line please :/ I'm really new to C or
> > programming.
>
> Welcome!
>
> > /* count blanks, tabs and newlines in input */
> > main()
>
> You should probably spell this
> int
> main(void)
> in modern C.
>
> > if (c == ' ')
> > ++bl;
> > else if (c == '\t')
> > ++t;
> > else if (c == '\n')
> > nl;
>
> (You missed the "++" on the last one; it should be "++nl".)
>
> You can write this without else if as follows:
>
> > if (c == ' ')
> > ++bl;
> > if (c == '\t')
> > ++t;
> > if (c == '\n')
> > ++nl;
>
> It's not necessarily any better or more efficient, but it'll work.
>
> The way I'd probably do it would be:
> switch (c) {
> case ' ': ++bl; break;
> case '\t': ++t; break;
> case '\n': ++nl; break;
> default: break;
> }
>
> 'switch' jumps to a case matching the controlling expression, then executes
> code until it hits a 'break'. It's often relatively efficient for cases
> where there are many options; compilers may generate smarter code than for
> the corresponding sequence of else if ()s.
>
> > printf("There are %d blanks, %d tabs and %d newlines", bl, t, nl);
>
> ... and
> return 0;
> here.
>
> > }
>
> -s
> --
> Copyright 2011, all wrongs reversed. Peter Seebach / usenet-nos...@seebs.net
http://www.seebs.net/...
lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/...
(Scientology) <-- get educated!
> I am not speaking for my employer, although they do rent some of my opinions.
Hey this is the second time you help me! Thanks a lot man :D!!
Your explanations are very clear.
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Re: Exercise 1-8 of the C programming language
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password