Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. The While If

The While If

Scheduled Pinned Locked Moved The Weird and The Wonderful
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bulg
    wrote on last edited by
    #1

    Here's one you've probably seen before..

    while ( GlobalBufferPos > BytesToRead )
    {
    if (//enough space)
    {
    //copy data to GlobalBuffer
    }
    else
    {
    Temp = //space left;
    // copy data into GlobalBuffer
    // copy data into GlobalBuffer2
    }
    GlobalReadPos += BytesToRead;
    if(GlobalReadPos > MAX_SIZE) GlobalReadPos -= MAX_SIZE;

    GlobalBufferPos -= BytesToRead;
    WriteData(...);
        Temp = BytesToRead
    break;
    

    }
    // return ... ;

    I suppose it made sense at the time.

    P S 2 Replies Last reply
    0
    • B bulg

      Here's one you've probably seen before..

      while ( GlobalBufferPos > BytesToRead )
      {
      if (//enough space)
      {
      //copy data to GlobalBuffer
      }
      else
      {
      Temp = //space left;
      // copy data into GlobalBuffer
      // copy data into GlobalBuffer2
      }
      GlobalReadPos += BytesToRead;
      if(GlobalReadPos > MAX_SIZE) GlobalReadPos -= MAX_SIZE;

      GlobalBufferPos -= BytesToRead;
      WriteData(...);
          Temp = BytesToRead
      break;
      

      }
      // return ... ;

      I suppose it made sense at the time.

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Yeah, probably. I would assume that originally they wanted the while and later decided otherwise; I would either have removed the while or made the break conditional.

      1 Reply Last reply
      0
      • B bulg

        Here's one you've probably seen before..

        while ( GlobalBufferPos > BytesToRead )
        {
        if (//enough space)
        {
        //copy data to GlobalBuffer
        }
        else
        {
        Temp = //space left;
        // copy data into GlobalBuffer
        // copy data into GlobalBuffer2
        }
        GlobalReadPos += BytesToRead;
        if(GlobalReadPos > MAX_SIZE) GlobalReadPos -= MAX_SIZE;

        GlobalBufferPos -= BytesToRead;
        WriteData(...);
            Temp = BytesToRead
        break;
        

        }
        // return ... ;

        I suppose it made sense at the time.

        S Offline
        S Offline
        supercat9
        wrote on last edited by
        #3

        I have on occasion used:

        {
        ...
        do {
        ...
        if (condition) break;
        ...
        if (condition) break;
        ...
        } while(0);
        ...

        with the while() "loop" being merely a block from which the code could "break". I wonder if the code here began as something similar, but replacing an "if()" and "do {} while()" with a "while()" and a "break".

        P 1 Reply Last reply
        0
        • S supercat9

          I have on occasion used:

          {
          ...
          do {
          ...
          if (condition) break;
          ...
          if (condition) break;
          ...
          } while(0);
          ...

          with the while() "loop" being merely a block from which the code could "break". I wonder if the code here began as something similar, but replacing an "if()" and "do {} while()" with a "while()" and a "break".

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          No, that horror is by people who think it's less of a horror than a goto.

          S 1 Reply Last reply
          0
          • P PIEBALDconsult

            No, that horror is by people who think it's less of a horror than a goto.

            S Offline
            S Offline
            supercat9
            wrote on last edited by
            #5

            PIEBALDconsult wrote:

            No, that horror is by people who think it's less of a horror than a goto.

            Many modern languages support exceptions on the basis that conventional structured programming constructs can't practically handle everything that programs need to do. In languages which support exceptions, they should be used when appropriate. What would you suggest doing, if not a 'goto' or 'break', in situations where an exception would be appropriate but the language doesn't support them? BTW, I wish some standards agency would draft an extension of the C99 standard which would allow something like exceptions but only within a particular function (C++, which allows full-fledged extensions, already exists, but run-time support for exceptions requires too much overhead to be practical on some smaller microcontrollers; limited support for exceptions within a function could improve program structure without impacting code size, speed, or RAM requirements.

            P 1 Reply Last reply
            0
            • S supercat9

              PIEBALDconsult wrote:

              No, that horror is by people who think it's less of a horror than a goto.

              Many modern languages support exceptions on the basis that conventional structured programming constructs can't practically handle everything that programs need to do. In languages which support exceptions, they should be used when appropriate. What would you suggest doing, if not a 'goto' or 'break', in situations where an exception would be appropriate but the language doesn't support them? BTW, I wish some standards agency would draft an extension of the C99 standard which would allow something like exceptions but only within a particular function (C++, which allows full-fledged extensions, already exists, but run-time support for exceptions requires too much overhead to be practical on some smaller microcontrollers; limited support for exceptions within a function could improve program structure without impacting code size, speed, or RAM requirements.

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              Huh? I use goto when necessary (which is very rare).

              N 1 Reply Last reply
              0
              • P PIEBALDconsult

                Huh? I use goto when necessary (which is very rare).

                N Offline
                N Offline
                Nagy Vilmos
                wrote on last edited by
                #7

                goto bathroom. goto kitchen. goto pub. They seem necessary :-D


                Panic, Chaos, Destruction. My work here is done.

                P 1 Reply Last reply
                0
                • N Nagy Vilmos

                  goto bathroom. goto kitchen. goto pub. They seem necessary :-D


                  Panic, Chaos, Destruction. My work here is done.

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  Only the bathroom is a necessary.

                  B 1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    Only the bathroom is a necessary.

                    B Offline
                    B Offline
                    bolivar123
                    wrote on last edited by
                    #9

                    Just an optional requirement..... :laugh:

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups