This question already has answers here:
How is if statement evaluated in c++?
(7 answers)
Closed 7 years ago.
What does this condition mean in a while loop?
int x;
cin >> x;
while(x) {
...
}
int has an implicit conversion to bool. Basically 0 converts to false, all nonzero values convert to true
So more verbosely, your condition would read
while (x != 0)
As #CoryKramer says, when you have a condition which only contains a variable, even if is a char, int, float, etc. the value 0 is considered as false, and any other as true. If you are using pointers is the same: the NULL value is considered as false, and any other direction is considered as true.
Related
This question already has answers here:
Why does this if conditional with string.length() evaluate inconsistently?
(2 answers)
Signed/unsigned comparisons
(6 answers)
Weird std::string::size() in a for loop
(2 answers)
Closed 5 months ago.
I have a string called in, a string that has the value of Hello, world!. I also have a integer called i that has the value -1. When I ask C++ to print out if i is less than the length of in (in.length()), it says false but when I try -1 < 15, it says true. Why does it say false? I feel like this is extremely basic math?
string::length() returns an unsigned integer. You can't compare that to a negative signed value, so the -1 gets converted to an unsigned value, which wraps it to a very large number, which is not less than the string's length, hence the result is false.
-1 < 15, on the other hand, is comparing two signed integers, so no conversion is needed, and the result is true, as expected.
This question already has answers here:
bool operator ++ and --
(4 answers)
Closed 2 years ago.
why incase of boolean, overflow doesn't occur in circular fashion. eg say a=126 when you reach 128 and you increment it, a goes to -127 if range is -127 to 128. similarly for boolean it is 0 to 1 so it should move around 0101010101 and so on. please clarify
using namespace std;
int main()
{
bool a;
for (a = 1; a <= 5; a++)
cout << a;
return 0;
}
From cppreference
If the operand of the pre-increment operator is of type bool, it is set to true
If the operand of the post-increment operator is of type bool, it is set to true
Note that this behaviour was removed in C++17 and your code won't compile with newer standards (probably because it was confusing).
This question already has answers here:
How does C++ handle &&? (Short-circuit evaluation) [duplicate]
(7 answers)
Is short-circuiting logical operators mandated? And evaluation order?
(7 answers)
Closed 2 years ago.
In the below example will the comparison between a and c be executed or will it be skipped? I would assume the answer is no because the value of valid has already been determined since if any comparison is false in this expression the resulting value of valid is false, but I don't actually know if that's true.
int i = 10, b = 20, c, 30;
bool valid = (i > b && a < c);
Thanks for taking the time to answer my question!
This question already has answers here:
What is !0 in C?
(5 answers)
Closed 3 years ago.
I was doing some random stuff and I came up with int a = !3; and when I outputted the value it I was expecting an error but it got me a 0.
Why did this happen and what ! means on that example?
! is the BOOLEAN NOT operator, i.e. !true == false and !false == true. In C and C++ every value that is nonzero is treated as true when used with a boolean operator. And false is numerically 0. So 3 is treated as true and !3 = !true = false = 0.
!3 is an expression, it evaluates to a bool type.
In this example it evaluates to false.
bool's can be casted to an int, which happens automatically when you assign it to one.
The int representation of false is '0' whilst true is '1'.
That "negates" the value. Anything non-zero becomes 0, and 0 becomes 1.
This question already has answers here:
How is if statement evaluated in c++?
(7 answers)
Closed 5 years ago.
I'm not sure how the while loop works in the following simple piece of code
short CountBits(unsigned int x){
short num_bits = 0;
while (x){
num_bits += x & 1;
x >>= 1;
}
return num_bits;
}
How does an unsigned integer evaluate to True or False?
In the given context, x must be converted to true or false.
From the C++11 Standard (4.12/1):
A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.
Think of
while (x){ ... }
as
while (x != 0){ ... }
True is any integer which is not equal to 0. Thus if x evaluates to 0 then the loop breaks.
"How does an unsigned integer evaluate to True or False"? The same way any numeric value evaluates to true or false: 0 is false, any other value is true. Some people would write the test as while (x != 0); that's exactly the same thing.
For any integral number in C++, on most machines, 0 will evaluate to false. As such, when X becomes 0, the loop will terminate. The loop will continue while x is a non-zero value.