Useful alternative control structures?

后端 未结 28 932
礼貌的吻别
礼貌的吻别 2021-01-30 02:20

Sometimes when I am programming, I find that some particular control structure would be very useful to me, but is not directly available in my programming language. I think my

28条回答
  •  清歌不尽
    2021-01-30 02:49

    This is a bit of a joke, but you can get the behavior you want like this:

    #include 
    #include 
    
    int main (int argc, char *argv[])
    {
      int N = std::strtol(argv[1], 0, 10); // Danger!
      int state = 0;
      switch (state%2) // Similar to Duff's device.
      {
        do {
          case 1: std::cout << (2*state) << " B" << std::endl;
          case 0: std::cout << (2*state+1) << " A" << std::endl; ++state;
        } while (state <= N);
          default: break;
      }
    
      return 0;
    }
    

    p.s. formatting this was a bit difficult and I'm definitely not happy with it; however, emacs does even worse. Anyone care to try vim?

提交回复
热议问题