Is it common practice to "abuse" loops as goto [closed] - c++

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.

Related

When is it better to use switch/case over if statements? [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 5 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I have a tendency to use switch statements if I am creating a menu driven program, and I tend to use if statements when I only have a few items. I believe this has to do with the way I was taught in school, but I don't know if that is necessarily the way to go.
Are there vast differences between the two? When should you pick one over the other?
Edit: I should specify, I am mainly concerned with optimization (even if one or the other is only marginally more efficient).
If statements look like if statements. Switch statements look like switch statements. Some compilers may be mildly better at optimizing certain types of switch statements than the equivalent set of if statements, though that won’t be a significant factor in your situations. In cases where the two are both applicable, there are few practical concerns in choosing one over the other.
Use whichever fits your intended coding style better.

Why else condition is needed in c++ for good coding practice even if we have nothing to write in there? [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 have seen else condition even if there is nothing inside it in many standard code documents. Why is that? Why should we use else even there is nothing to write in there as a good practice?
int a=1,b=2;
if(a)
{
a=a+b;
}
else
{
// Why we need this even there is nothing to write
}
Code standards are not global, these vary with the group, organization or community. Some people think it is more readable and maintainable this way, some think otherwise.
In my opinion, it is one of those rules made up by people who have too much free time on their hands.
I mean just look at it
if(condition) { do something }
else { }
How is that even more readable and maintainable. It is just adding some extra trash code.
I think one should avoid such documents for the sake of code.
However, if you work with people who follow the same standards either convince them to drop this out of standards or just walk away slowly :)

Is it considered better or worse to use custom macros for simplifying things like cin and cout statements? [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've been doing this with my code for quite a while and I wanted the opinions of the experts on if this is a good idea or not.
#include <iostream>
#define OutPut std::cout <<
int main()
{
OutPut "This sentence should appear in console.";
return 0;
};
Is this macro safe to use in terms of shorting things like cout, cin, and the like? Could it potentially cause more harm than good down the line?
No it is not a good idea. A C++ programmer expects to read C++ and not some kind of made up language that someone decided to concoct.
Using the Preprocessor with C++ is almost always a poor idea as there are almost always better alternatives.
See Why are preprocessor macros evil and what are the alternatives?
See https://softwareengineering.stackexchange.com/questions/249767/is-it-a-good-idea-to-define-me-this

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.

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.