Skip to main content
GameDev.net gamedev.net
🔒 Locked

testing == in a loop

Started by Matty_alan May 30, 2010 at 8:42 AM 25 replies 4.1k views
Original Post
Matty_alan
Matty_alan
Iv'e come accross this problem a few times now, why can't you test if x is equal to y in say a for loop but you can test < and >,
It seems strange that it'll let you test < and > but restirct you from using ==
Gorax
Gorax
Loops will continue as long as the given code evaluates to true, regardless of what you're testing.
MaulingMonkey
MaulingMonkey
You can use == just fine in the programming languages I'm aware of. Either you don't quite grok how loops are supposed to behave (as Gorax suggests), or you're doing something like using floating point numbers, where == requires an exact match, and 99% of the time an operation returns an approximation rather than the exact value. (Example: 1.0f == 1.0f/3*3 is not necessarily true -- but this doesn't have anything to do with loops specifically per se.)
DevFred
DevFred
Quote:
Original post by Matty_alan
why can't you test if x is equal to y in say a for loop

Sure you can, but it's probably not what you want.
for (int i = 0; i == 10; ++i){    std::cout << i << std::endl;}

Here, the loop body will be executed 0 times, because 0 == 10 is false.
Hodgman
Hodgman
Quote:
Original post by Matty_alan
Iv'e come accross this problem a few times now, why can't you test if x is equal to y in say a for loop but you can test < and >,
It seems strange that it'll let you test < and > but restirct you from using ==
Can you give an example of when you're not allowed to use ==?
int escapedMentalPatients = 0;int allowableEscapes = 0;while( escapedMentalPatients == allowableEscapes ){  if( rand()%2 )    escapedMentalPatients++;}SoundAlarm();
Matty_alan
Matty_alan
Quote:
Original post by DevFred
Quote:
Original post by Matty_alan
why can't you test if x is equal to y in say a for loop

Sure you can, but it's probably not what you want.
for (int i = 0; i == 10; ++i){    std::cout << i << std::endl;}

Here, the loop body will be executed 0 times, because 0 == 10 is false.


I cut and pasted that loop directly into a program and it didn't give any output

here is an exact copy paste of it...


#include

int main()
{


for (int i = 0; i == 10; i++)
{

std::cout << i << std::endl;

}


system("pause");
}



if i change this to
for (int i = 0; i <= 10; i++) it works perfect
Matty_alan
Matty_alan
Quote:
Original post by Hodgman
Quote:
Original post by Matty_alan
Iv'e come accross this problem a few times now, why can't you test if x is equal to y in say a for loop but you can test < and >,
It seems strange that it'll let you test < and > but restirct you from using ==
Can you give an example of when you're not allowed to use ==?*** Source Snippet Removed ***


I dont know if not allowed is correct words but if i test it it just returns faulse everytime, and therefor never executing the loop. (see above)

Im using Dev C++ if it's a compiler spasific issue
Captain P
Captain P
As the other posters have already explained, it's perfectly normal behavior. Think about what you're really saying: as long as i is equal to 10, run the loop. But i starts as 0, which immediately fails the check, so if you're not seeing any results, it's because the computer does what you told it to do. It's simple logic, really.

There are times where using == in a loop is useful, but this isn't one of them.
boogyman19946
boogyman19946
Quote:
Original post by Matty_alan
Quote:
Original post by DevFred
Quote:
Original post by Matty_alan
why can't you test if x is equal to y in say a for loop

Sure you can, but it's probably not what you want.
for (int i = 0; i == 10; ++i){    std::cout << i << std::endl;}

Here, the loop body will be executed 0 times, because 0 == 10 is false.


I cut and pasted that loop directly into a program and it didn't give any output

here is an exact copy paste of it...


#include

int main()
{


for (int i = 0; i == 10; i++)
{

std::cout << i << std::endl;

}


system("pause");
}



if i change this to
for (int i = 0; i <= 10; i++) it works perfect


No kidding it didn't return anything, there is nothing to return. i is 0 and it is compared to a constant 10. Does 0 = 10? No, therefore the loop is false and it executes nothing except for system("pause");. If you want to see == in action, set i to 10 so the loop will be executed once. After that the expression will return false and the loop will exit.
Yo dawg, don't even trip.
Matty_alan
Matty_alan
Oh, I see the problem now, I think my brain's failing to boot properly today :|
Mathmo
Mathmo
Perhaps what you are actually looking for is ! (i == 10) or as it is normally written, i != 10, so that you iterate through the loop while i is not 10, ie. until i is equal to 10.
Y_Less
Y_Less
Quote:
Original post by Matty_alan
I cut and pasted that loop directly into a program and it didn't give any output


Quote:
Original post by DevFred
Here, the loop body will be executed 0 times, because 0 == 10 is false.


That's exactly what he said would happen. "==" means "is equal to" and the central part of a for statement is essentially a while statement. That code says "set i to 0, while i equals 10, do this code". Since i is 0, it is not equal to 10, so the loop will not do anything. You can use ==, but only if it's the right thing to use. In this case 0 is less than 10, so < or <= (or !=) are the right things to use (often (in C++) you will see loops using "!=" and "++i" rather than the more common (no evidence for that statement) "<" and "i++", this is to do with handling for overloaded operators on objects, so I would recommend getting in to this habit where possible).

As an additional example of where == would be the correct operator to use in a loop:

for (int i = 0, j = 10; j == 10; ++i)
{
if (i == 7) j = 5;
}

This is clearly a completely contrived example, but it will execute 8 times.
freeworld
freeworld
Quote:
Original post by Matty_alan
Quote:
Original post by DevFred
Quote:
Original post by Matty_alan
why can't you test if x is equal to y in say a for loop

Sure you can, but it's probably not what you want.
for (int i = 0; i == 10; ++i){    std::cout << i << std::endl;}

Here, the loop body will be executed 0 times, because 0 == 10 is false.


I cut and pasted that loop directly into a program and it didn't give any output

here is an exact copy paste of it...


#include

int main()
{


for (int i = 0; i == 10; i++)
{

std::cout << i << std::endl;

}


system("pause");
}



if i change this to
for (int i = 0; i <= 10; i++) it works perfect


it shouldn't give any ouput, for loops continue indeffinately until the second argument returns false. it the given code this happen immediately, because 'i' is set to 0 therefore not equaling 10.

I think what you need to look into is a while() loop instead.

int i = 0;while (i == 0){    // do something    i = 2;}

iMalc
iMalc
Here's a contrived way to code a loop from 1 to 34 that uses two =='s:
for (int i=1; ((i%5)==0) && ((i%7)==0); ++i){    cout << i;}

Point being, there's nothing stopping you from using == in a loop except logic.
SiCrane
SiCrane
Are you sure about that? When i is 1, then i % 5 is 1 and i % 7 is 1.
return0
return0
Quote:
Original post by iMalc
Here's a contrived way to code a loop from 1 to 34 that uses two =='s:
for (int i=1; ((i%5)==0) && ((i%7)==0); ++i){    cout << i;}

Point being, there's nothing stopping you from using == in a loop except logic.



for (int i=1; ((i%5)!=0) && ((i%7)!=0); ++i){    cout << i;}



Maybe?
SiCrane
SiCrane
That will only get you 1 to 4.
szecs
szecs
Quote:
Original post by iMalc
Here's a contrived way to code a loop from 1 to 34 that uses two =='s:
for (int i=1; ((i%5)==0) && ((i%7)==0); ++i){    cout << i;}

Point being, there's nothing stopping you from using == in a loop except logic.

for (int i=0; i==0 || i==1 || i==2 || i==3 || i==4 || i==5 || i==6 || i==7; ++i){    cout << i;}
Muhahaha
DevFred
DevFred
And if you want to be sure that i < 10 is really true:
for (int i = 0; (i < 10) == true; ++i){    std::cout << i << std::endl;}

;-)
Sneftel
Sneftel
Oh, let's just get this over with.
template<typename T>class EverythingElse<T>{public:    EverythingElse(T x) : x(x) { }    template<typename U>    bool operator==(U const& other) { return x != other; }        template<typename U>    bool operator!=(U const& other) { return x == other; }    private:    T x;};template<typename T>EverythingElse<T> AnythingBut(T x) { return EverythingElse<T>(x); }for(int i=0; i == AnythingBut(10); i++){    std::cout << i << std::endl;}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.