2

I have this loop here that alternates a boolean.

Here is what I mean:

bool switch = false;

while(true) {
    switch = !switch;
    std::cout << switch << std::endl;
}

Every time this code loops, the boolean named switch will alternate between true and false.

Something like:

bool switch = false;

while(true) {
    std::cout << !switch << std::endl;
}

There is really nothing wrong with this but I would like a one-line solution.

Of course, that doesn't work but something similar to that.

BendedWills
  • 99
  • 2
  • 10
  • 4
    What is wrong with this? – balki Oct 14 '20 at 01:48
  • 2
    Take out the newlines. Problem solved :-) – paxdiablo Oct 14 '20 at 01:50
  • 2
    C++ is not that obsessed with one-liners like python is for example. Your first variant is the most clear one. Prefer that. – bolov Oct 14 '20 at 01:59
  • 1
    `while ((switch = !switch) || true) std::cout << switch << std::endl;` will do it. However, not necessarily friendly to programmers (including yourself) who get to maintain your code in future. – Peter Oct 14 '20 at 02:18

5 Answers5

3
std::cout << (switch = !switch) << std::endl;

Or

std::cout << !switch << std::endl << switch << std::endl;
273K
  • 29,503
  • 10
  • 41
  • 64
  • 4
    If you're new then now is a bad time to be learning bad habits. Future you will need to read this code someday when it isn't fresh in your mind. – Retired Ninja Oct 14 '20 at 01:57
2

If all you want to do is change switch and output it in one line of code, you can do:

std::cout << (switch = !switch) << std::endl;

But that's almost certainly not going to do anything other than reduce your line count by one, a dubious achievement at best. It'll most likely end up as the same underlying machine code.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • I'm a little confused. The dupe seems to cover everything in the question, as well as the answers. May I ask what the issue with it is? – cigien Oct 14 '20 at 14:03
  • 1
    @cigien: it appeared to me that the proposed dupe was about replacing `if a then a = false else a = true` with a `a = not a`. Clearly this OP knew about that since that's what their code did. What OP was after was a way to do several things in one line of source which I thought a different question. – paxdiablo Oct 14 '20 at 21:12
  • Ok, that's reasonable. I added my own solution as well :) – cigien Oct 14 '20 at 21:44
1

I think the way you have it is fine, but another way to do it would be like this:

unsigned int count = 0;

while(true) {
   std::cout << ((++count%2) != 0) << std::endl;
}
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • We should really point out that this alternative could lead to code that is about 7 times slower if the optimiser fails to fix it for us. A `/` divide operation is very slow (typically 5 clock cycles or so), and the `%` modular operation *contains* a divide. Also we're using more memory than necessary. Also, it's less readable. – Elliott Oct 14 '20 at 21:50
  • @Elliott a modern optimizer will recognize that `((x%2)!=0)` can be more efficiently implemented as `((x&0x01)!=0)` so there shouldn't be any performance penalty. As for readability, I think you're right about that. – Jeremy Friesner Oct 14 '20 at 21:54
0

From c++20, you can write:

for (auto b : std::views::iota(1)) 
{
    std::cout << b % 2 << std::endl;
}

Here's a demo.

cigien
  • 57,834
  • 11
  • 73
  • 112
0

If you are in the mood for overcomplicating things, here is the "enterprise edition":

#include <iostream>

class SwitchingBool
{
    bool state_{};
public:
    operator bool()   const noexcept { return state_; }
    bool operator()()       noexcept { return (state_ = !state_); }
    // Or use a named accessor or a named mutator or invert the bodies or ...
};

int main()
{
    SwitchingBool blinker;
    
    std::cout << std::boolalpha;
    for (int i{}; i < 10; ++i)
    {
        std::cout << "From " << blinker << " to " << blinker() << '\n';
    }
}
Bob__
  • 12,361
  • 3
  • 28
  • 42