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. General Programming
  3. C / C++ / MFC
  4. What should I use instead of for(;;)

What should I use instead of for(;;)

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++helpannouncement
4 Posts 4 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.
  • M Offline
    M Offline
    Micie
    wrote on last edited by
    #1

    Like in topic.. this question may be silly, but I'm new programmer so be gentle (plz). What should I do if I want to "refresh" the value of variables in DOS version? I'll include here a part of my source code. Treat this as a sample of my problem.

    for( ;; )
    {
    int result = GetKeyState(VK_SHIFT);
       if(retult == 1)
       {
           ..code..
       }
    }
    

    This code will slow down every comp - I think. I need a solution of that problem. :| Thanks in advance. **__________ I'm made in C++... and I'm proud of it!_**

    G A H 3 Replies Last reply
    0
    • M Micie

      Like in topic.. this question may be silly, but I'm new programmer so be gentle (plz). What should I do if I want to "refresh" the value of variables in DOS version? I'll include here a part of my source code. Treat this as a sample of my problem.

      for( ;; )
      {
      int result = GetKeyState(VK_SHIFT);
         if(retult == 1)
         {
             ..code..
         }
      }
      

      This code will slow down every comp - I think. I need a solution of that problem. :| Thanks in advance. **__________ I'm made in C++... and I'm proud of it!_**

      G Offline
      G Offline
      gamitech
      wrote on last edited by
      #2

      SetTimer(NULL,NULL,1,NULL); case WM_TIMER: int result = GetKeyState(VK_SHIFT); if(retult == 1) { ..code.. } break; or you can use your code but put this extra for( ;; ) { int result = GetKeyState(VK_SHIFT); if(retult == 1) { ..code.. } Sleep(1); } gabby

      1 Reply Last reply
      0
      • M Micie

        Like in topic.. this question may be silly, but I'm new programmer so be gentle (plz). What should I do if I want to "refresh" the value of variables in DOS version? I'll include here a part of my source code. Treat this as a sample of my problem.

        for( ;; )
        {
        int result = GetKeyState(VK_SHIFT);
           if(retult == 1)
           {
               ..code..
           }
        }
        

        This code will slow down every comp - I think. I need a solution of that problem. :| Thanks in advance. **__________ I'm made in C++... and I'm proud of it!_**

        A Offline
        A Offline
        Archer282
        wrote on last edited by
        #3

        although for(;;) is valid i suggest you use while(TRUE) instead (the for loop wasnt really intended for infinite loops) i doubt that it is your infinite loop that is slowing down the computer, try to quiting some process first i have run many instances of programs that do more than your code seems to be doing, and it didnt affect the speed.

        1 Reply Last reply
        0
        • M Micie

          Like in topic.. this question may be silly, but I'm new programmer so be gentle (plz). What should I do if I want to "refresh" the value of variables in DOS version? I'll include here a part of my source code. Treat this as a sample of my problem.

          for( ;; )
          {
          int result = GetKeyState(VK_SHIFT);
             if(retult == 1)
             {
                 ..code..
             }
          }
          

          This code will slow down every comp - I think. I need a solution of that problem. :| Thanks in advance. **__________ I'm made in C++... and I'm proud of it!_**

          H Offline
          H Offline
          Henry miller
          wrote on last edited by
          #4

          Before going down this path, see if there is a way to register to recive key pressed events that gives you what you need. What you are doing is called polling, and it ALWAYS slows a computer down. To get around it computers have interupts of various sorts so that you can be told that something happened. So you have something like: while(someWaitForKeyEvent()) { if(GetKeyState(VK_SHIFT) == 1) ..code.. Assuming you have to poll I'd do it something like: while (1 != (result = GetKeyState(VK_SHIFT)) { sleep(1); // wait one second before polling again } ..code.. lookup sleep (including other forms of it like BSleep), see if there is one that will yield the rest of your CPU cycles to other tasks. I suspect sleep(1) is too long so you will want, so find something that does the same thing, but for less time. Basicly what is happening is you can run GetKeyState several hundred times in your timeslot, but you can call GetKeyState a lot faster than a human could press the keyboard. (look up debouncing sometime, it is taken care of for you, but it will help you see what is going on) So you are looking for a way to tell the computer "I won't be able to do anything for a while, do something else" One last point: instead of if(result == 1) you should use a constant or an enumeration so you can do if(result == KEY_DOWN) this makes it clearer to readers what you mean. As you wrote you code I'm not sure what it means, while KEY_DOWN tells me something. (There might even be defined constants in a headerfile someplace. When I first saw your code I was thinking of key as in encryption key, and figured you were waiting on a thread to generate the key! It took me a minute to realise that it was a windows function and then I had to look it up to see what it did)

          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