How to handle multiple OR condition in IF [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
bool A=false;
bool B=true;
if(!A || !B)
{
.....
}
In this condition when A is true, it didn't checks the B but I want to check B instead of A .
In this condition I want to execute if any one is true(A or B), But If A is true I want to check B also. is there any other logic to resolve this apart from using different if conditions?

You may turn off short-circut evaluation in your compiler. If you work with your own types, you may overload || and &&. Overloaded logical operators are not short-circuted.
Both things are really bad. Programmers expect logical operators to behave in certain way and are very likely to get super confused with this unexpected behavior. You should stick to short-circuting.

Related

Whats the expressive way of or'ing a range of bools? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I recently came across a situation where i needed to 'or' a range of bools together.
An example could be std::vector<bool> bools;
I have come up with some different ways of or'ing all of them together:
std::any_of(bools.begin(), bools.end(), [](bool x) { return x; } )
or
std::find(bools.begin(), bools.end(), true) != bools.end()
or
std::accumulate(bools.begin(), bools.end(), false, std::logical_or);
or
std::accumulate(bools.begin(), bools.end(), 0) != 0;
If the length is fixed, I could use a std::bitset which has the any function - but this is only an option in that case.
Of all these, neither is very intuitive or is really expressing the intent clearly here (in a small way they all exhibit some kind of "WTF? code")
The irony with this situation is that for variadic-templates we do have the fold-expressions which, in comparison, would be simple and expressive:
(bools || ...)
Note: I do have boost which have the range library, but AFAIK it doesn't alleviate this issue really.

Why not always use assert? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
If static_assert is restricted to compile-time evaluations, why not always stick with assert if it can handle both compile-time and run-time evaluated expressions?
assert(...) is ALWAYS evaluated at runtime, of course you can call it with a compile-time evaluated expression, but you will only first see the assertion at runtime.
Sometimes you wanna make sure something only compiles when a certain expression is true, thats when you use
static_assert(expression) which gives a compiler error if not fulfilled.
This is in direct spirit with "fail as early as possible" (and probably hard too ;-)

Places where assignment operator can be used wrongly instead of comparison operator , = Vs == [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm doing a C++ compiler project where I need to flag warnings at places where assignment operator can be used wrongly instead of the comparison operator. .e.g
while doing comparison in if statement , to check variable a as 10 sometimes we wrongly type if(a= 10), which will result in if statement always true whereas I wanted to be true only if a is 10. Some of the cases I can think of are :
if(var = a), logically it should be if( var==a )
while(var = a )
for(;var=a;)
do{}while(var=a)
var=a? "some XYZ": "some ABC"
Can you please help me with the more cases where this logical error can occur, where the user was supposed to use == and by mistake = was used?
A nasty one I ran across recently was assert (a=b). The reason that's so particularly nasty is because the assumption stated in the assert is that the two are already equal, so the statement is most likely harmless. But if they aren't, this sets you up for a nasty debugging session as the debug builds functionally differs from the release build.

alternative of if else-if else statement c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I'm trying to expand my knowledge of conditional statements in C++
if (condition){
return 0;
}
if else (condition A && condition B) {
//
}
else {
//
}
in this multiple conditonal state, what would be a good alternative?
Obviously because of condition A && condition B on if else statement, I can't use switch statement?
what would be a good alternative?
Shouldn't matter if no good alternative is better than what you already have.
(Ignoring the apparent if else error) Your shown control flow appears to be quite minimal (and therefore good) representation. There is no code duplication, and no repetitive structure that could be further exploited.

Implement sizeof operator using bitwise operation [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
During an interview for the position of C/C++ , I faced one question Write a c program to implement sizeof operator ,I have wrote the code ,Then he asked me to implement using bitwise operations .I tried for int.... But I coudlnt ,,,, anyone can post the code ,, how to implement ,,,
You cannot do this, since the set of data types that support bitwise operations is not a superset of the set of types that support sizeof.
Proof: int*.