alternative of if else-if else statement c++ [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
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.

Related

Is a single return statement cleaner? [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
Today, me and my colleague have a disagreement on a subject :
Which of the following is "cleaner" and why ? :
bool check(){
if(!test to do){
return false;
}
if(!other test to do){
return false;
}
return true;
}
Or:
bool check(){
bool result = true;
if(!test to do){
result = false;
}
if(!other test to do){
result = false;
}
return result;
}
In my opinion, it doesn't matter because of compilation process.
It almost doesn't matter functionally, because of the compilation process.
Some older texts recommend a single return, i.e. your second example, as they say the logic of the function is easier to trace.
Personally I dislike clutter and find the first example easier to read. You still have to think about and reason about the function in either case, just the first doesn't involve an additional variable and potentially more if/else flow clutter.
The first one doesn't always evaluate the second condition, though, which may be of interest to you if it has performance penalties, or side effects.
Furthermore, individual return statements are easier for the compiler to elide (not that it matters here).
But otherwise it's completely subjective.

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.

what the best way to make a loop infinite in c++ [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 4 years ago.
Improve this question
in c++ what is the bets way to make an infinite loop using the for loop method?
These are some ways through which you can make infinite loops -
while(1) {
..... statements ....
}
while(true) {
.... statements ......
}
for(;;) {
.... statements ......
}
do {
.......statements.....
} while(true);
It's surprisingly difficult, although the request is also surprisingly illogical. Empty infinite loops are undefined behaviour in C++.
See Optimizing away a "while(1);" in C++0x
Really though such a thing is unnecessary - simply terminate your program instead. In a sense that is equivalent to having a program that consumes no input and outputs nothing, running forever.

How to handle multiple OR condition in IF [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
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.

Is it common practice to "abuse" loops as goto [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 6 years ago.
Improve this question
If in code there are many if-clauses and execution of code is not sensible on a previous error, is it OK to use a one-cycles loop for this? Simply for being able to exit the block with a break?
Like this:
do {
//..code
if (error1) break;
//..code
if (errorN) break;
//do finally something when no errors before
} while (false);
Yes, this is idiomatic, even if, perhaps, it was not the intended use for a do while loop. The source code for the linux kernel exploits this.
There's nothing unclear about it: while(false) does exactly what is says on the tin.
Yes it's a common technique to avoid deep nesting, and actually preferable to goto;.
From point of readability its way better than goto statements. The scope and code flow of the loop is well defined, and you don't need to lookup the corresponding labels of the goto statements, which not necessarily appear below.