Code: Select all
#include <iostream>
using namespace std;
int main(){
int i=10;
while(i-=2 && cout<<i<<endl);
return 0;
}
why this code counting with odd numbers
it will work if i changed
Code: Select all
while((i-=2) && cout<<i<<endl);
Moderator: Board moderators
Code: Select all
#include <iostream>
using namespace std;
int main(){
int i=10;
while(i-=2 && cout<<i<<endl);
return 0;
}
Code: Select all
while((i-=2) && cout<<i<<endl);
Code: Select all
cout<<i<<endl;
That's not true, only assignments are right-associative.chunyi81 wrote:Expressions in C++ are evaluated from right to left
Only if everything is fine with cout. If you close stdout or an error occurs, this expression will evaluate to false.chunyi81 wrote:2 && cout << i << endl is evaluated first -> this always give 1.