473,465 Members | 1,395 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

question about "while"

Hi Group,
When I run this:

# include <stdio.h>
int main(){
int c=0;
while ( (c=getchar()) != EOF && c != ' ' && c != '\t' )
printf("foo");
if (c == '8')
putchar(c);
else if (c=='\n')
printf( " It's a new line");
return(0);
}

and the stdin is: 'b" i get "foofoo" as the output.

8 or a new line feed gives "8" and "It's a new line" as expected.

My question is this. How exactly is the while statement evaluated if c
is neither a space or a tab.

Thank you.

Apr 11 '06 #1
9 1632
When c is neither a space or a tab, this program will always be in the
while loop. when c is a space or a tab, no output will display.

1. c = getchar()
2. compare to EOF ,' ' , and '\t'
3. compare return false --> quit while loop
compare return true --> back to 1

Apr 11 '06 #2

LZXIA wrote:
When c is neither a space or a tab, this program will always be in the
while loop. when c is a space or a tab, no output will display.

1. c = getchar()
2. compare to EOF ,' ' , and '\t'
3. compare return false --> quit while loop
compare return true --> back to 1

Hi,
If I understand you correctly, why does it only seem to print "foofoo"
if is not "EOF ,' ' , and '\t' " as opposed to getting stuck in the
loop with endless foos?

Thanks

Apr 11 '06 #3
I am not good at english, and I can't understand your words, but I can
give you a example

Now most keyboard is designed to be like this:
1. It has a buffer to restore input, and the buffer size is limit such
as 80 characters.
2. When the buffer is full, or it get a '\n', it will send the
characters to where them should go.

So when you type "bacd" and did not hit the [CR], nothing will
happen.But if CR is pressed, the funtion getchar() will work.
It get the characters from the buffer one by one. after it has got the
'd', it will get a '\n' . so you will get 5 foo. Then getchar()
waiting for you input again.

I hope I have explained clearly.

Apr 11 '06 #4

LZXIA wrote:
I am not good at english,
No...it was I not understanding well.
So when you type "bacd" ....... get a '\n' . so you will get 5 foo.


And that is exactly what happened.

I hope I have explained clearly.

Yes...thank you.

Apr 11 '06 #5
becos the function getchar() waits for input (its a function which
blocks) so soon after while condition is evaluated (and is false i.e no
\n or ' ' or '\t' ) it waits for next input ().

Apr 11 '06 #6
becos the function getchar() waits for input (its a function which
blocks) so soon after while condition is evaluated (and is false i.e no
\n or ' ' or '\t' ) it waits for next input ().

Apr 11 '06 #7
white.crow wrote:
becos the function getchar() waits for input (its a function which
blocks) so soon after while condition is evaluated (and is false i.e
no \n or ' ' or '\t' ) it waits for next input ().


Please read in the information in my .sig below.

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Apr 11 '06 #8
LZXIA wrote:
I am not good at english, and I can't understand your words, but I can
give you a example


Please read in the information in my .sig below.

Brian
--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Apr 11 '06 #9
morpheus wrote:
Hi Group,
When I run this:

# include <stdio.h>
int main(){
int c=0;
while ( (c=getchar()) != EOF && c != ' ' && c != '\t' )
printf("foo");
if (c == '8')
putchar(c);
else if (c=='\n')
printf( " It's a new line");
return(0);
}

and the stdin is: 'b" i get "foofoo" as the output.

8 or a new line feed gives "8" and "It's a new line" as expected.

My question is this. How exactly is the while statement evaluated if c
is neither a space or a tab.

Thank you.


From MSDN Library:

while ( expression ) statement

The expression must have arithmetic or pointer type. Execution proceeds
as follows:

1. The expression is evaluated.

2. If expression is initially false, the body of the while statement is
never executed, and control passes from the while statement to the
next statement in the program.

If expression is true (nonzero), the body of the statement is executed
and the process is repeated beginning at step 1.

Apr 12 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Sybren Stuvel | last post by:
Hi there, Is it possible to use an assignment in a while-loop? I'd like to do something like "loop while there is still something to be read, and if there is, put it in this variable". I've been...
6
by: John Pass | last post by:
What is the difference between a While and Do While/Loop repetition structure. If they is no difference (as it seems) why do both exist?
1
by: DougFAI | last post by:
Hello there. Today, I was developing a college work software and then I saw thoe 2 types of while. My teacher asked me how does the "while(ifs)" (considering my ifstream was ifs) (that was the one...
3
by: archeryguru2000 | last post by:
Hello, I've been stumped for several weeks on this particlar program. At work here, we have 30 machines that we record scrap data on. I take this data and create spreadsheets (boring) that can be...
7
by: lukertin | last post by:
I have a 2-D array stored as an object, when i go to call up the array using my $testvar = @ {$self->Peptides}; it gives me the error Can't use string ("0") as an ARRAY ref while "strict...
2
by: zxo102 | last post by:
Hi, I would like to combine two python applications into a single one with two threadings. Both of them have a "while 1:" loop respectively. For example, one application is to monitoring serial...
2
by: recordlovelife | last post by:
So I am trying to display a title, date, and content of a wordpress blog. Word press provides nice drop in functions to get the job done with simple names like "the_title", and the "the_content" But...
1
by: robotlizz | last post by:
Hello - I am a brand new at Java and I am having a hard time with a program I have to turn in tomorrow. I can not get the 'Q' option to work and the loop goes on forever. I've tried to go over the...
3
by: toofuerte | last post by:
I have placed an input JOptionPane and get input and then qualify the input but then when I try to use the input to qualify when it is entered again it says the variable was not initialized. How do I...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.