By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,903 Members | 2,036 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,903 IT Pros & Developers. It's quick & easy.

What is the scope of this while loop?

P: n/a
JS
#include <stdio.h>

main(){

int c, i, nwhite, nother;
int ndigit[10];

nwhite = nother = 0;

for (i = 0; i < 10; ++i)
ndigit[i] = 0;

while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else
if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;

printf("digits =");
for (i = 0; i < 10; ++i)
printf(" %d", ndigit[i]);

printf(", white space = %d, other = %d\n",
nwhite, nother);

}

Where does the above while loop end?? For some reason all the examples I
have read with C code there is no brackets to confine the while loop (like
in Java).

JS
Nov 14 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
here's the bracketed version. i'm pretty sure i got it right, but you
better double check the output of both versions to make sure they
match.

#include <stdio.h>

main()
{
int c, i, nwhite, nother;
int ndigit[10];

nwhite = nother = 0;

for (i = 0; i < 10; ++i)
{
ndigit[i] = 0;
}

while ((c = getchar()) != EOF)
{
if (c >= '0' && c <= '9')
{
++ndigit[c-'0'];
}
else
{
if (c == ' ' || c == '\n' || c == '\t')
{
++nwhite;
}
else
{
++nother;
}
}
}

printf("digits =");

for (i = 0; i < 10; ++i)
{
printf(" %d", ndigit[i]);
}

printf(", white space = %d, other = %d\n",
nwhite, nother);

}

Nov 14 '05 #2

P: n/a
Vig


--
--
Vig
"JS" <sf****@asdas.com> wrote in message
news:d1**********@news.net.uni-c.dk...
#include <stdio.h>

main(){

int c, i, nwhite, nother;
int ndigit[10];

nwhite = nother = 0;

for (i = 0; i < 10; ++i)
ndigit[i] = 0;

while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else
if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;

printf("digits =");
for (i = 0; i < 10; ++i)
printf(" %d", ndigit[i]);

printf(", white space = %d, other = %d\n",
nwhite, nother);

}

Where does the above while loop end?? For some reason all the examples I
have read with C code there is no brackets to confine the while loop (like
in Java).


I believe the while loop extends till the line just prior to the first
printf statement. This is because only the line immediately after a while
loop is in its scope. However, the if statemnt's scope extends back to the
while loop.

--
Vig
Nov 14 '05 #3

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

JS wrote:
#include <stdio.h>

main(){

int c, i, nwhite, nother;
int ndigit[10];

nwhite = nother = 0;

for (i = 0; i < 10; ++i)
ndigit[i] = 0;

while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else
if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;

printf("digits =");
for (i = 0; i < 10; ++i)
printf(" %d", ndigit[i]);

printf(", white space = %d, other = %d\n",
nwhite, nother);

}

Where does the above while loop end??
In C, a while() statement consists of three parts:
1) the 'while' keyword,
2) a condition, enclosed in parenthesis, and
3) a single statement to be executed if the condition tests true

In C, a series of statements, enclosed in brace brackets, is considered to be a
single statement.

Thus:
while (1) printf("Continious loop\n");
and
while (1) { printf("Continious loop\n");
are equivalent.

So, to answer your question, the while() statement in your example code extends
from the while() to the ++nother;

Or in other words, the following is a whole while() statement.

while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else
if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;
For some reason all the examples I
have read with C code there is no brackets to confine the while loop (like
in Java).


The implication is that Java requires that the body of a while loop be enclosed
in brace brackets. So?? What's Java got to do with it? Different languages have
different rules.

- --
Lew Pitcher
IT Specialist, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFCNen5agVFX4UWr64RAtYqAJ9n1twwQeQ6KCGK39wIEO lIUV9I1wCfbmeI
Nkltw9EMstpO1koUwr6u778=
=O1jF
-----END PGP SIGNATURE-----
Nov 14 '05 #4

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Lew Pitcher wrote:
[snip]
In C, a while() statement consists of three parts:
1) the 'while' keyword,
2) a condition, enclosed in parenthesis, and
3) a single statement to be executed if the condition tests true

In C, a series of statements, enclosed in brace brackets, is considered to be a
single statement.

Thus:
while (1) printf("Continious loop\n");
and
while (1) { printf("Continious loop\n");
Oops. Correction
while (1) { printf("Continious loop\n"); }

are equivalent.

[snip]

- --
Lew Pitcher
IT Specialist, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFCNepGagVFX4UWr64RAiXPAKCMfxxydswF1zsaBaOf+0 bPXLFezgCfYUNg
+b2aWTcHplyJvfaB9cu37sg=
=v6mw
-----END PGP SIGNATURE-----
Nov 14 '05 #5

P: n/a
"JS" <sf****@asdas.com> writes:
#include <stdio.h>

main(){

int c, i, nwhite, nother;
int ndigit[10];

nwhite = nother = 0;

for (i = 0; i < 10; ++i)
ndigit[i] = 0;

while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else
if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;

printf("digits =");
for (i = 0; i < 10; ++i)
printf(" %d", ndigit[i]);

printf(", white space = %d, other = %d\n",
nwhite, nother);

}

Where does the above while loop end?? For some reason all the examples I
have read with C code there is no brackets to confine the while loop (like
in Java).


A while() always controls a single statement. Typically that single
statement is a compound statement, which is delimited by '{' and '}',
but it can be any statement.

In this case, the single statement is the if-else statement consisting
of the 7 lines following the while().

Note that the indentation on the last two of those lines is incorrect;
the "else" should be directly under the "if", and the "++nother;"
should be directly under the "++nwhite;". (This isn't incorrect in
the sense that a compiler is going to complain about it, but it's
misleading.)

Personally, I always use braces around sub-statements like this (a
habit I picked up from Perl, which requires them). Mike Deskevich's
fully braced version of your program appears to be correct.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #6

P: n/a
JS wrote:

#include <stdio.h>

main(){

int c, i, nwhite, nother;
int ndigit[10];

nwhite = nother = 0;

for (i = 0; i < 10; ++i)
ndigit[i] = 0;

while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else
if (c == ' ' || c == '\n' || c == '\t')
++nwhite;
else
++nother;

printf("digits =");
for (i = 0; i < 10; ++i)
printf(" %d", ndigit[i]);

printf(", white space = %d, other = %d\n",
nwhite, nother);

}

Where does the above while loop end?? For some reason all the
examples I have read with C code there is no brackets to confine
the while loop (like in Java).


It ends after one statement, which may be a compound statement,
effected via {}. Better indentation and formatting will show
things up clearly, as in:

while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9') ++ndigit[c-'0'];
else if (c == ' ' || c == '\n' || c == '\t') ++nwhite;
else ++nother;

I don't believe in extra braces when the complete condition/action
can be specified on one line of reasonable length.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 14 '05 #7

P: n/a
CBFalconer wrote:
... Better indentation and formatting will show
things up clearly, as in:
.
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9') ++ndigit[c-'0'];
else if (c == ' ' || c == '\n' || c == '\t') ++nwhite;
else ++nother;
.
I don't believe in extra braces when the complete condition/action
can be specified on one line of reasonable length.


Does that mean you're writing...

while (blah) continue;

....instead of...

while (blah)
{
continue;
}

....these days! ;)

--
Peter

Nov 14 '05 #8

P: n/a
Peter Nilsson wrote:

CBFalconer wrote:
... Better indentation and formatting will show
things up clearly, as in:
.
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9') ++ndigit[c-'0'];
else if (c == ' ' || c == '\n' || c == '\t') ++nwhite;
else ++nother;
.
I don't believe in extra braces when the complete condition/action
can be specified on one line of reasonable length.


Does that mean you're writing...

while (blah) continue;

...instead of...

while (blah)
{
continue;
}

...these days! ;)


I prefer compound statements with all ifs and loops.
It makes reading simpler.

--
pete
Nov 14 '05 #9

P: n/a
Peter Nilsson wrote:
CBFalconer wrote:
... Better indentation and formatting will show
things up clearly, as in:
.
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9') ++ndigit[c-'0'];
else if (c == ' ' || c == '\n' || c == '\t') ++nwhite;
else ++nother;
.
I don't believe in extra braces when the complete condition/action
can be specified on one line of reasonable length.


Does that mean you're writing...

while (blah) continue;

...instead of...

while (blah)
{
continue;
}

...these days! ;)


Definitely. Wouldn't have it any other way :-)

int flushln(FILE *f)
{
int ch;

while (('\n' != (ch = getc(f))) && (EOF != ch)) continue;
return ch;
}

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.