Skip to content
View in the app

A better way to browse. Learn more.

MSFN

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

while(1) vs for(;;)

Featured Replies

As I've been reading through the MSDN for code tips, I've often seen "infinite" loops in C++ written in the form

for(;;)
{
          // Do stuff here
}

Whereas I've always been used to writing them as

while(1)
{
         // Do stuff here
}

Either that or while(true). Of course, within the loop there's a method of breaking out of it, either a break statement or a return.

Is there a reason why the MSDN examples use the for loop instead of the while loop? Does it complile "cleaner" or faster? Or is it simply a matter of preference?

I'm just curious if someone knew the answer.


As far as I know they are completely interchangable. I use "while" in this case because it seems to make more "human logic" sense. I.E. You are doing it while the condition is met or until told to stop. "For" seems to imply a sequential iteration, I.E. do it this number of times or until I say stop. Anyway, thats just how I think of it, like I said, in code terms they are interchangable.

http://msdn.microsoft.com/library/default....e_statement.asp

http://msdn.microsoft.com/library/default....r_statement.asp

Edited by dman

  • Author

Thanks for the reply.

I also find while a bit more intuitive in terms of "infinite" loops. The for loop is something to be done a specific number of times, like parsing through an array or the like.

Cheers!

Yep, dman is right, they are interchangable... Use what is your loging telling you to use :) I am using For (because I used it in basic, dos etc...)

It's mostly a matter of style. I prefer "for(;;)".

"while(1)" may be less efficient than "for (;;)" - logically, the value of 1 must be checked against zero every iteration. In practice, a check would only occur for compilers with poor optimizers or when optimization is disabled.

Also, while(1) is kind of arbitrary - while(666) or while("hello") is just as valid.

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.