conditional-statements

Why is my c != 'o' || c != 'x' condition always true? [duplicate]

坚强是说给别人听的谎言 提交于 2019-11-26 02:15:41
This question already has an answer here: Why non-equality check of one variable against many values always returns true? 3 answers I have this loop statement, which I'll express using C-like syntax (C, C++, Java, JavaScript, PHP, etc. all use similar syntax): while (c != 'o' || c != 'x') { c = getANewValue(); } I want it to run until I get a 'o' or 'x' , but it never exits, even when c is 'o' or 'x' . Why not? I've also tried using if : if (c != 'o' || c != 'x') { // Show an error saying it must be either 'o' or 'x' } but that also always shows the error message, even when c is 'o' or 'x' .

Why would you use an assignment in a condition?

天涯浪子 提交于 2019-11-26 02:09:59
问题 In many languages assignments are legal in conditions. I never understood the reason behind this. Why would you write: if (var1 = var2) { ... } instead of: var1 = var2; if (var1) { ... } 回答1: It's more useful for loops than if statements. while( var = GetNext() ) { ...do something with var } Which would otherwise have to be written var = GetNext(); while( var ) { ...do something var = GetNext(); } 回答2: I find it most useful in chains of actions which often involve error detection, etc. if (

Why is my c != 'o' || c != 'x' condition always true? [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 01:50:39
问题 This question already has answers here : Why non-equality check of one variable against many values always returns true? (3 answers) Closed last year . I have this loop statement, which I\'ll express using C-like syntax (C, C++, Java, JavaScript, PHP, etc. all use similar syntax): while (c != \'o\' || c != \'x\') { c = getANewValue(); } I want it to run until I get a \'o\' or \'x\' , but it never exits, even when c is \'o\' or \'x\' . Why not? I\'ve also tried using if : if (c != \'o\' || c !

How to filter by conditions for associated models?

早过忘川 提交于 2019-11-26 01:01:51
问题 I have a belongsToMany association on Users and Contacts. I would like to find the Contacts of the given User. I would need something like $this->Contacts->find()->contain([\'Users\' => [\'Users.id\' => 1]]); The cookbook speaks about giving conditions to contain, custom finder methods and sing through association key, but I did not find out how to put these together. 回答1: Use Query::matching() or Query::innerJoinWith() When querying from the Contacts table, then what you are looking for is

How to filter by conditions for associated models?

若如初见. 提交于 2019-11-25 22:48:37
I have a belongsToMany association on Users and Contacts. I would like to find the Contacts of the given User. I would need something like $this->Contacts->find()->contain(['Users' => ['Users.id' => 1]]); The cookbook speaks about giving conditions to contain, custom finder methods and sing through association key, but I did not find out how to put these together. Use Query::matching() or Query::innerJoinWith() When querying from the Contacts table, then what you are looking for is Query::matching() or Query::innerJoinWith() , not (only) Query::contain() . See Cookbook > Database Access & ORM