If statement logic error - c++

if ((pCommandPts>=tempChar.commandValue) && ((pCommandPts - tempChar.commandValue)<=0))
If pCommandPts is an int with the value 6 and tempChar.commandValue is an int with the value 3, why would this statement evaluate to false?

Left part of this expression is true in case of 6 and 3, but 6-3 is not lower or equal to zero
&& ((pCommandPts - tempChar.commandValue)<=0))

That code makes no sense, and it's almost certainly a bug.
If you rearrange the inequalities, you get:
a >= b && a <= b
Which is only true if a == b, which is not true for your case 6 != 3

Related

what does if(!(x%3)) mean?

I am attempting to solve a hw problem in which I need to write down what the program will output. I am stuck however, on the syntax "if ( !(i%3)). What does that really mean? Does it mean that the program is checking for any i that is divisible by three? aka, is the if statement only runs if i is divisible by three?
int main () {
for (int i=0; i<10; (i<3?i++;i+=2)) {
if (!(i%3)) {
continue;
}
else if (i%7 ==0) {
break;
}
cout << i<< endl;
}
Does it mean that the program is checking for any i that is divisible by three? aka, is the if statement only runs if i is divisible by three?
Correct. The longer version of that check would be
if (i % 3 == 0)
continue;
The most common use case for such branching is probably FizzBuzz.
İt means if i is not(!) divisible by 3 continue.
For example if i is 3,6,9 it won't continue otherwise it will continue.
if (x) where x is int implicitly compared with zero. I.e. if (x) equals to if (x != 0). ! is negation. So if (!x) equals to if (x == 0). And the last step is if (!(i%3)) equals to if ((i%3) == 0) what is the same with check, that i deivisible by 3
The if() statement is false only if the result inside the parentheses is 0 (false). Take a look at your program:
i%3 may return 0 (false), 1 (true), or 2 (true)
The negation operator ! changes the result of the operation (i%3). So, if the i is divisible with 3 the statement will return 0 (false). Being negate, ! it will result in True. Otherwise the result of (i%3) will be true and with the operator ! the result of the hole statement will be false. Basically this code is checking if the value of i is divisible with 3.
Other options will be:
if (0==i%3)
{
/*code*/
}
Your code can be simplified as below
int main() {
for (int i=0; i<10;)
{
if (i % 3 == 0) {
continue;
}
else if (i % 7 == 0) {
break;
}
cout << i << endl;
i = i<3 ? i+1 : i+2;
}
}
When you write a integer variable like i as a condition, what happens is that if i==0 then the result of the condition is false, otherwise it would be true.
Let's check it out in your program, if(!(x%3)), let's name condition= !(x%3), when this condition is true? when x%3 == 0, note that the negation operator ! is behind x%3, so in this case the condition would be equal to true, more formally the condition is equal to :
if(x%3==0)
these kinds of conditions are common, check this example out :
int t = 10;
while(t--){
cout<<t<<endl;
}
The above condition i.e if(!(i%3)) will true when " i is not disvisable by 3"
Hope this helps.
In java and other languages there is a special type to represent booleans and evaluate expressions; in c and its variants there is no such thing, instead an expression is considered "true" if -taken as a integer number- is equal to 0; false for every other value. (Fun fact: this is why you usually end the code by return 0)
So if(x%3) in c is equivalent to if(x%3==0) in other languages. That said, if(x%3) execute the if body when the remainder of x/3 is 0, that is when x is a multiple of 3.
In your code you have the ! before, that -as you may know- "inverts" the expression. That means that if(!(x%3)) can be read as "If the remainder of the integer division of x by 3 is not 0", or alternatively: "If x is not a multiple of 3".
So, basically, you saw it right.
Hope I helped!

assigning boolean values into boolean variables [duplicate]

This question already has answers here:
using Boolean to represent integers
(4 answers)
Closed 7 years ago.
int x = 5;
bool t = ((x % 3) && (x % 4));
when I try this code in visual studio, I get the value true for variable t. How this code is working?
First of all, you have the && operator, which compares two values and returns a boolean, so t will b a boolean. Secondly, in C++, every value other than 0 will evaluate to true, so x % 3, which evaluates to 2 will return true, then x % 4, which evaluates to 1 will also return true.
So in the end you have
bool t = (x % 3) && (x % 4);
which equals
bool t = 2 && 1;
which equals
bool t = true && true;
which equals
bool t = true;
All nonzero values are considered as true for logical operations like AND(&&) and OR(||)
x%3 -s a module operation returned value 2,
x%4 returns 1
2 && 1 is equal to true && true , which is equal to true.
All non zero value assignment to a bool typed variable is considered as true or 1 and all zero value assignment considered as false or 0.
Operator % returns the remainder after division (modulo) and true is any value that is not 0.
5 % 3 == 2
5 % 4 == 1
2 && 1 == true && true == true
you can just do:
bool t=(x% 3!=0) && (x% 4!=0);

how to compiler compile if statement [duplicate]

This question already has answers here:
Is Short Circuit Evaluation guaranteed In C++ as it is in Java?
(2 answers)
Closed 9 years ago.
main()
{
int k = 5;
if(++k <5 && k++/5 || ++k<=8); // how to compiler compile this statement
print f("%d",k);
}
// Here answer is 7 but why ?
++k < 5 evaluates to false (6 < 5 = false), so the RHS of the && operator is not evaluated (as the result is already known to be false). ++k <= 8 is then evaluated (7 <= 8 = true), so the result of the complete expression is true, and k has been incremented twice, making its final value 7.
Note that && and || are short circuit boolean operators - if the result of the expression can be determined by the left hand argument then the right hand argument is not evaluated.
Note also that, unlike most operators, short circuit operators define sequence points within an expression, which makes it legitimate in the example above to modify k more than once in the same expression (in general this is not permitted without intervening sequence points and results in Undefined Behaviour).
Unlike many questions like this, it appears to me that your code actually has defined behavior.
Both && and || impose sequence points. More specifically, they first evaluate their left operand. Then there's a sequence point1. Then (if and only if necessary to determine the result) they evaluate their right operand.
It's probably also worth mentioning that due to the relative precedence of && and ||, the expression: if(++k <5 && k++/5 || ++k<=8) is equivalent to: if((++k <5 && k++/5) || ++k<=8).
So, let's take things one step at a time:
int k = 5;
if(++k <5 &&
So, first it evaluates this much. This increments k, so its value becomes 6. Then it compares to see if that's less than 5, which obviously produces false.
k++/5
Since the previous result was false, this operand is not evaluated (because false && <anything> still always produces false as the result).
|| ++k<=8);
So, when execution gets to here, it has false || <something>. Since the result of false | x is x, it needs to evaluate the right operand to get the correct result. Therefore, it evaluates ++k again, so k is incremented to 7. It then compares the result to 8, and finds that (obviously) 7 is less than or equal to 8 -- therefore, the null statement following the if is executed (though, being a null statement, it does nothing).
So, after the if statement, k has been incremented twice, so it's 7.
In C++11, the phrase "sequence point" has been replaced with phrases like "sequenced before", as in: "If the second expression is evaluated, every value computation and side effect associated with the first expression is sequenced before every value computation and side effect associated with the second expression." Ultimately, the effect is the same though.
In following:
for && "something" is evaluated when first condition is True
for || "something" is evaluated when first condition is False
( (++k <5) && (k++/5) ) || (++k<=8)
( 6 < 5 AND something ) OR something
( False AND something ) OR something
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
False OR 7 < 8
False OR True
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
True
So k comes out to be 7
Initially k is assigned 5, in your declaration, then in following if condition:
++k < 5 && k++ / 5 || ++k <= 8
// ^
// evaluates
Increments k to 6 then its and LHS of && operand evaluates false.
6 < 5 && k++ / 5 || ++k <= 8
// ^ ^^^^^^^
// 0 not evaluates
Then because of Short-Circuit behavior of && operator k++ / 5 will not evaluates.
Short-Circuit behavior of && operator is:
0 && any_expression == 0, so any_expression not need to evaluate.
So above step 2 conditional expression becomes:
0 || ++k <= 8
// ^^^^^^^^
// evaluates
Increments k to 7 then its:
0 || 7 <= 8
because you have ; after if, so no matter whether if condition will evaluates True or False printf() will be called with k = 7.

Comparison of int values in an if statement

I had a question in my test paper in which we had to compare the values of int type variables. The first thought that came to my mind was that it was missing the && operator but i am not sure.
int a=2, b=2, c=2;
if(a==b==c)
{
printf("hello");
}
I have a doubt, will the above statement will execute or not in c or c++? Can i have the reason as well.
Thank You
It will execute but with what I believe unexpected results to you.
One of the == will evaluate to a boolean value, which will then be converted to an int and then the second comparison will be performed, comparing an int to either 1 or 0.
The correct statement is a==b && b==c.
For example:
3 == 3 == 3
evaluates to
true == 3
1 == 3
false
a==b==c
is equivalent to
(a == b) == c
The result of a == b is 1 (if true) or 0 (if false), so it will probably not achieve what you expect.
Use a == b && b == c to check if the value of the three objects are equal.
a == b == c is a comparison between c and result of a==b (1 or 0) operation.
use a==b&&b==c.
the condition a==b==c is equivalent to (a==b)==c which will provide the required result iff c==1, else the code will fail.

Do while with a boolean as the condition in c++?

so i want to know how a boolean acts in a condition statement in the following code
bool flag = true;
do {
d += data[i];
if (d > 15 || i == 3) {
flag = false;
}
i = i + 1;
} while (flag);
when will it exit the dowhile loop?
If either d > 15 or i == 3 evaluates to true, i will get incremented and the loop will stop.
In other words, flag is only checked at the end of each iteration, even though it might be set to false in the middle of one.
It will exit when (d > 15 || i == 3) which means (d > 15 or i == 3).
i is incremented at each iteration therefore if i is < 3 at the beginning of the program we are sure that at a certain point it will reach i == 3 and break the loop.
On d we can't tell much since we don't know it initial value nor its behavior inside the loop since we don't know anything about data.
Depends on your values of d and i...
As soon as d is greater than 15 or i is equal to 3, flag becomes false and the loop will end.
This might not happen in the same iteration, though. For example if i is incremented to 3 in a loop, it will be evaluated in the following loop first and flag might be set to false.
It will break the while when SUM of first 3 values in array Data[] be greater than "15", or break if does not be greater than 15 the SUM of first 3 values.
[It's depend on initial value of "i"]