Programming – The Purpose Of Loops


What Are Loops?

Loops are a fundamental construct for many programs.  In fact, all but the most basic of programs are likely to include at least one loop in them.  Loops can be very useful and can save you, the developer, a lot of time.

I’ll use some examples to illustrate how your time is saved.  All the examples provided in this article will be in pseudocode.

Printing A Number Sequence

Lets say that we want to print a sequence of numbers.  Lets say, 1 to 100 inclusive.  Now without loops we would have to do the following:

[codebox 1]

……. and so on.

Obviously not only is this time consuming, it is also very tedious.  Now, lets have a look what happens to the code if we use a loop, in this case, a for loop.

[codebox 2]

Now, as you can see, the code above is a lot more slick, easier to write and less time consuming.  The one possible disadvantage could be that its slightly harder to write and read (from a non-developers perspective).  However, even programmers with the most basic of experience should understand what you are doing with the for loop.

The Purpose Of Loops

The purpose of loops is to repeat the same, or similar, code a number of times.  This number of times could be specified to a certain number, or the number of times could be dictated by a certain condition being met.  There are usually a number of different types of loops included in programming languages including for loops, while loops and do….while loops.

Two Ways To Implement Loops

So, the two ways you can use a loop are:

Repeating a block of statements with a specified and previously defined number of iterations to be completed.  In our example above, this is the way we have used looping – to print the numbers 1 to 100 inclusive.  We know to do this that we have to loop 100 times, there is no requirement for a condition to be attached to the loop.

Repeating a block of statements where the number of iterations is unknown and is based on other variables or conditions.

Another Example

For example in a do….while loop you may have the following condition:

[codebox 3]

In this basic example we’ve told the program to keep the sprinklers set to true (or on) while the sun is shining.  This loop will continue while the condition, sun=true, is true.  When this condition becomes false, the loop will exit and the program will continue.

do….while loops must run at least once, regardless of whether the condition is true or not (as the condition isn’t checked before the code is ran the first time. If we only wanted the code to run when the condition is true (i.e. not even run once if it is false) we would use a while loop.

So, in our example, our do…while loop will run at least once and at maximum…… infinity (in theory, if it was possible for the sun to shine 24 hours a day forever!).

The code within the loop could run once, twice, 100 times, 1000 times etc etc.

The do…while loop that we have described here is dependant on the condition (in the brackets with ‘while’ at the end) holding true for it to remain looping.  The number of loops is unknown.  The only time the number of loops is known is when the loop actually exists.  It is not known before or during the loop.

Image: mutednarayan

Will Mountain Lion be the OS X BSOD Superbug Solution?

OS X Mountain Lion LogoApple have today released the latest version of their OS X operating system – Mountain Lion.

Of course, this is usually an exciting time for Mac owners as they browse the new features and improvements and work out just how fast they are going to buy the latest operating system.

However, there is a much more critical reason for a possible OS update for Mac users, especially ones suffering with the OS X BSOD superbug problem.

Although DPS Computing managed to discover a temporary solution (which, by the way, still works :)), Mac users (including ourselves) are eager for a permanent solution that doesn’t mean that we have to effectively disable one of our graphics cards just to stop our Macs freezing every 5-10 minutes.

Has this been fixed in Mountain Lion? – well as yet, we’ve not been able to verify this – we would be particularly eager to hear from anyone (via e-mail, website contact form or a comment on this post) if they have previously suffered from the OS X BSOD superbug problem and have upgraded to Mountain Lion and it has fixed it.

However, this raises further questions.  If this problem with the nVidia graphics card drivers has been fixed in Mountain Lion, why has a free update not been given to Lion users?  After all, it would be unacceptable to effectively charge for what amounts to a critical bug fix, having already paid thousands of pounds for Apple Mac equipment.

Another question is, if this problem with the nVidia graphics card drivers hasn’t been fixed in Mountain Lion, why not?  The issue was first reported on Apple’s community support forums approximately 18 months ago, with Apple acknowledging the issue 12 months ago.

Some would argue that the time spent on ‘fixing’ the problem has already been too long, but if there is no fix, free or otherwise, available still, where does that leave Mac users (especially mid 2010 model users) suffering from this catastrophic bug within the nVidia drivers shipped with Lion?

Also, if no fix is released, does this mean that mid-2010 models of Mac computers are incompatible with OS X Lion, despite being previously confirmed as compatible?

There are still many questions to be asked, and more importantly, answers to be given regarding this problem, which many Mac users will now hope, having coped with the issue for at least 18 months, is close to a resolution to allow them to fully utilise their Mac hardware.

OS X BSOD ‘Superbug’ – gfxCardStatus v2.0 Declared a Success

gfxCardStatus Logo

The recent solution to the OS X BSOD ‘Superbug’ provided by DPS Computing Limited has undergone rigourous testing which has now been completed and declared a success.

Many users, mainly of mid-2010 MacBook Pros have been desperately searching for answers to the OS X BSOD ‘superbug’ which has been rendering their Apple machines useless.  With kernel panics, due to this problem, occurring every 5 to 10 minutes after boot, the problem all but disabled many of Apple’s flagship products.

Multiple avenues of investigation were performed by DPS Computing, including hardware issues, driver issues and third party software ‘triggers’.  Apple have failed to come up with a solution currently, after first acknowledging the problem 12 months ago.

Although there was initial problems when using gfxCardStatus 2.2.1, these problems have been confirmed to be rectified by using gfxCardStatus 2.0.

Apple are due to release the next major version of OS X – Mountain Lion, later on this month with predictions for the date being the 25th July, based on information regarding the release of Lion.

It is widely believed that Apple may release a permanent solution to the OS X BSOD superbug, to be included with Mountain Lion, by possibly including a rewritten version of the nVidia graphics card driver.  This would be an ideal solution for Apple customers, who would then be able to resume using dynamic switching and getting the full performance from their Macs.

UPDATE: OS X BSOD ‘Superbug’ : gfxCardStatus v2.0 Results Promising

gfxCardStatus Logo

Results from our initial thorough testing of gfxCardStatus version 2 in response the OS X BSOD ‘superbug’ are very promising which means that we now have a fully working and documented solution which has been documented previously.

Although not ideal this will no doubt come as a relief to many MacBook Pro mid-2010 model owners who are currently experiencing the disabling OS X BSOD bug.

Tests show that after multiple reboots the “Integrated only” option continues to work with each reboot, unlike the experience we had with gfxCardStatus v2.2.1 in testing.  The developer has put this down to the updated gfxCardStatus ‘triggering’ a defect related to the nVidia graphics card which in turn triggers the OS X BSOD.

One thing that is key to note from our testing of v2.0 is that even with the ‘run at startup’ option enabled, gfxCardStatus doesn’t appear to actually start upon booting and therefore it is necessary to run the application explicitly by double clicking it in the Applications folder (or an alternative location where you have copied it to, if not to Applications).

This problem is however fairly minor compared to the consequences of not using it, so I’m sure we can live with temporarily.  It would be advisable that users pin gfxCardStatus to the dock to serve as a reminder to start the application upon reboot.

To make life easier, so that the application is just ‘click and run’ for the purposes of what we need it to do some default settings should be changed (which then will obviously, not subsequently require resetting first time).  These settings changes are necessary to prevent the nVidia card being used in anyway or the ‘dynamic switching’ function becoming inadvertently activated.

Firstly, you should notice an ‘i’ at the top of your display – this may display as an ‘n’ in some circumstances, or may appear as a ‘d’ which means that the nVidia card is currently in use.  The ‘i’ refers to the Intel card.  After we change the settings, you want to verify that the ‘i’ is definitely showing, otherwise something has gone wrong.

Firstly, click on the ‘i’ and then select ‘Intel only’ from the menu:

Then we need to click on the ‘i’ again and select ‘Preferences’:

On the preferences menu you should ensure that under the ‘Switching’ category, ‘Restore last used mode’ at startup is ticked, ‘Use power based switching’ is unticked, and both of the ‘Intel’ buttons have been selected at the bottom.  You preferences should now look the same as those below:

And there you go, you’ve now got gfxCardStatus all set up to banish the OS X BSOD superbug!

Don’t forget, always remember to start up gfxCardStatus after a reboot if it fails to start itself!

With Mac OS X Mountain Lion due out later this month many MacBook Pro mid-2010 model users will be hoping that the long anticipated fix to the OS X BSOD superbug will be included with this release.

Link: gfxCardStatus v2.0 download.

UPDATE: OS X BSOD ‘Superbug’ – gfxCardStatus 2.0

gfxCardStatus Logo

Following the discovery by DPS Computing that gfxCardStatus 2.2.1 (the latest stable release) was a ‘one time wonder’ in regards to solving the OS X BSOD ‘superbug’ we decided to follow the advice of the gfxCardStatus developer and downgrade to 2.1, which shouldn’t have the same issue.

We can confirm during testing that downgrading to 2.1 had the same effect as using 2.2.1 – after one reboot, the screen would be garbled and unreadable using the ‘Integrated Only’ option.

Another solution that was suggested by the developer is to downgrade to any version of gfxCardStatus 2.0 or above, especially if it has worked previously for you.

Unfortunately, we were not using gfxCardStatus before the problems started affecting our testing machine, however we have downgraded to version 2.0 of gfxCardStatus to see if this helps resolve the issue.  Testing is currently ongoing however initial results are promising (although this was the case while using the initial 2.2.1 version).

That having been said, we are hopeful that version 2.0 may work for us, as there has been some positive feedback on the gfxCardStatus forums stating that this has worked for users trying to thwart the robust OS X BSOD superbug.

We will continue testing gfxCardStatus version 2 and update you.  We are planning to continue to run the test plans involving actions where the OS X BSOD and kernel panics frequently occur and reboot the machine several times to check that the solution continues to work after reboot.