what is code mean?
void fo(){
count += 1 + !(count <5);
cout << count <<endl;
}
Thank you .
i think this typed code is wrong
what the user want to do is to run the function fo until count;s value becomes 5 and it prints count everytime
It should be like this
int f0()
(
int count;//the user did not declare the variable count in the function
for(count=0;count<5;count++)
{
cout<<count;
}
return 0;
)
Regardless of what the author of this code intended, what it actually does is this:
This function fo() prints out the value of count, and the value of count will be increased by 1 if it is less than 5 or else it will be increased by 2 if it is greater than 5. In other words, this is what this code is doing:
If(count < 5)
count = count + 1; // Increases the value of count by 1.
Else
count = count + 2 // Increases the value of count by 2.
cout << count; // Prints the value of count to the screen.
So count is either increased by 1 or 2 depending on whether it is less than 5 or not.
Regardless of what the author of this code intended, what it actually does is this:
This function fo() prints out the value of count, and the value of count will be increased by 1 if it is less than 5 or else it will be increased by 2 if it is greater than 5. In other words, this is what this code is doing:If(count < 5) count = count + 1; // Increases the value of count by 1. Else count = count + 2 // Increases the value of count by 2. cout << count; // Prints the value of count to the screen.
So count is either increased by 1 or 2 depending on whether it is less than 5 or not.
Or more succinctly:
count = count + 1; // Increases the value of count by 1.
If(count >= 5)
count = count + 1; // Increases the value of count by 1.
cout << count; // Prints the value of count to the screen.
:icon_wink:
Also, I forgot to mention:
void fo(){
count += 1 + !(count <5); // (count < 5) will equate to either 1 if true and 0 if false. It's a bool value,
// and is inverted by the "!" (logical not operator).
cout << count <<endl;
}
Can you absolutely guarantee that for all compilers on all systems !(count <5)
will be 1 or 0? Is 1 a definition of TRUE or is non-zero the definition in C/C++? Tricks like this can be dangerous...
Can you absolutely guarantee that for all compilers on all systems
!(count <5)
will be 1 or 0? Is 1 a definition of TRUE or is non-zero the definition in C/C++? Tricks like this can be dangerous...
Well this looks like it could be another case of "assumption killed the cat." Since non-zero defines TRUE, and since somebody has stolen my crystal ball, there is no way I could guarantee that for all systems both now and in the future !(count < 5) would be 1 for TRUE. However, I could guarantee it would be zero for FALSE (as per definition). I see now that 1 + !(count < 5) is just a lousy replacement for a proper if statement: if(count > 5){++count;} and is unsafe.
I guess we'll just have to leave count += 1 + !(count < 5) for the reckless hot-shot programmers.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.