   testing == in a loop
  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 ==   
  Loops will continue as long as the given code evaluates to true, regardless of what you're testing.   ;  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.)   ;Quote:Original post by Matty_alanwhy can't you test if x is equal to y in say a for loopSure 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.   ;Quote:Original post by Matty_alanIv'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();;Quote:Original post by DevFredQuote:Original post by Matty_alanwhy can't you test if x is equal to y in say a for loopSure 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 outputhere is an exact copy paste of it...#include <iostream>int main(){for (int i = 0; i == 10; i++){    std::cout << i << std::endl;} system("pause");}if i change this tofor (int i = 0; i <= 10; i++) it works perfect   ;Quote:Original post by HodgmanQuote:Original post by Matty_alanIv'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   ;  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.   ;Quote:Original post by Matty_alanQuote:Original post by DevFredQuote:Original post by Matty_alanwhy can't you test if x is equal to y in say a for loopSure 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 outputhere is an exact copy paste of it...#include <iostream>int main(){for (int i = 0; i == 10; i++){    std::cout << i << std::endl;} system("pause");}if i change this tofor (int i = 0; i <= 10; i++) it works perfectNo 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.   ;  Oh, I see the problem now, I think my brain's failing to boot properly today :|   