Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

strange(?) while-loop conditions

Status
Not open for further replies.

801119

Programmer
Apr 10, 2000
311
SE
Greetings all...
X-)
As I am a half-newbi I still do know how conditions work in a normal loop, or do I? (I have the feeling something is wrong, either with me today, or my compiler; which by the way is Borland C++ Builder 5)
This is the thing that confuses me, asuming we have initialized x and y
Code:
do
{
 scanf("%d",&x);
 scanf("%d",&y);
} while( x!=0 && y!=0 );

Doesn't this "code-snip" say that: As long as BOTH x AND y DO NOT have the value 0(zero), it will enter the loop and run the sequenze?(cause the statement is false?) Well, when I execute the program and enter x=1 and y=0, then the program will no longer enter the loop.

I could of course be wrong cause the program I wrote wanted me to use this:
Code:
do
{
 scanf("%d",&x);
 scanf("%d",&y);
} while( x!=0 || y!=0 );

I know for certain that as long as the conditions within a loop are true, it will not be entered(or have I got it mixed up).


do{
x=4
}while(x!=1);
// this program will run forever
but
do{
x=4
i=1
}while(x!=1 && i!=1);
// this will only run once
whilst
do{
x=4
i=1
}while(x!=1 || i!=1);
// this will run forever

Can someone explain to a confused boy, how this can be? :)

NEED answers, desperate(my head is spinning s-))!!

Thanks for anything you can come up with My codes look like something a kid wrote
I have absolutely no idea what I am talking about
Somehow I still manage to make it work
 
Try using ( (x!=0) && (y!=0) ) to ensure that your logic is grouped properly. (If in doubt, bracket it out etc)
Also, a do loop will always execute at least once, as the expression is evaluated after the loop body. If you want it to not enter the loop unless your condition is met, try this:

scanf("%d",&x);
scanf("%d",&y);
while( x!=0 && y!=0 )
{
// do stuff
}

As for the example your book gave you, it asks the loop to continue until the longest string is read in. So it carries on if only one value is zero (ie, null-terminated), btu as soon as both are, it stops.

Any other opinions welcome..:)
 
Thanks for your reply, jaybe38, but that didn't work either :(
the problem is that I MUST enter the loop least one time. Therefore I chosed the do-while loop.

But, since that didn't work I did a work-around thing

for( ; ; )
{
scanf("%d",&x);
scanf("%d",&x);
if(x==0 && y==0)break;
}

this worked without any problems...
but I still want to know why this won't
do{
scanf("%d",&x);
scanf("%d",&x);

}while(x!=0 && x!=0);

I've tried all kinds of sollutions, in vain!
(x&&y)!=0
(x!=0)&&x(y!=0)

but the only thing that worked was,
do{
scanf("%d",&x);
scanf("%d",&x);

}while(x!=0 || x!=0); I JUST DON'T UNDERSTAND My codes look like something a kid wrote
I have absolutely no idea what I am talking about
Somehow I still manage to make it work
 
Take a look at the thread thread250-209940.
The same type of problem is addressed there.

Maniraja S
 
Hi,

The while condition needs to evaluate as a full statement to TRUE for the while() loop to continue. It says:

while (true)
{
keep doing this loop
}

or

do {
do it once and then keep doing it
} while(true);


Evaluate your while (x!=0 && y!=0)

So when x=1,y=0

(x != 0 (true, x=1) and y != 0 (false, y=0))

...but as a full expression it evaluates to false because both are not true.

so the while looks like this

while (false)

...and you break out of the loop.

Keep an eye on your sub-expressions. They will get you into trouble. That is why...

( (x!=0) && (y!=0) )

...still doesn't work.

Hope that helped.

-Tyler
 
Hi,
While loop will execute the statements only if the condition you want to check is true.
do
{
statement
...
} while(condition);
this loop will execute the statement once and then checks for the condition. if the condition is true then it executes the statements in the braces again otherwise it will come out.
In Your example
do
{
scanf("%d",&x);
scanf("%d",&y);
} while( x!=0 && y!=0 );

The condition of while loop will be true if both sides of the && are true and both sides will be true if x and y both are not equal to zero. if any of the x and y is equal to zero it fails.
Same is the case with ||. it requires any one of the sides to be true.


 
hi,
try using this
do
{
}while(!(x == 0 && y == 0)); Radha
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top