what the best way to make a loop infinite in c++ [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 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.

Related

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 :)

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.

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.

Indentation of curly brackets in functions/methods in C/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 8 years ago.
Improve this question
Semantically speaking, does it make a difference which one of these styles you use when writing code, or will the processor parse it the same way. If there is a difference, which one is faster?
int function(bool) {
...
return 0
}
or
int function(bool)
{
...
return 0
}
Both are correct, choose one style and stick with it.
But don't use both styles in same project.
Both styles are commonly in use. When I began programming I used the latter, it made it easier to determine that the block was closed. Now I use the former because it uses less vertical space. Both are fine.

What's the Correct Way to Shutdown a Condition Variable [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 8 years ago.
Improve this question
A coworker recently asked for my opinions on a thread safe queue class he is modifying. A shortened version of the waitAndPop method is below:
std::shared_ptr<T> waitAndPop() {
std::unique_lock<std::mutex> lock(mut);
dataCond.wait(lock, [this] { return (!dataQueue.empty() || !queueAlive);});
if (dataQueue.empty() || !queueAlive)
return std::shared_ptr<T>(); // NULL
std::shared_ptr<T> result = dataQueue.front();
dataQueue.pop();
return result;
}
A common dilemma with these sorts of classes is how to handle breaking out of the condition wait on shutdown. In this case, he eventually returns a null pointer. Returning null pointers from modern C++ seems clunky and outdated to me and places a burden on the user. I'd prefer throwing an exception or ignore it altogether, forcing the user to cooperatively shutdown.
My question is what's the best way to handle this situation?