Monday, 9 May 2011

Real life metaphor

You are looking for your wallet. You keep on looking around until you find your wallet.

The key features of a while loop is that a boolean condition is evaluated first. In this case it is the question of do you have your wallet, true or false? If you have your wallet, true, then you don't search for it (complete the actions/code within the while loop), obviously. But if you don't have your wallet, false, then you search for it until you have found it (keep on evaluating and working your way through your action/code until the boolean condition changes), changing the original boolean condition of do you have your wallet to true so you no longer repeat your process of searching for your wallet.

So it satasfies the fact that a boolean condition is evaluted before the execution of the code/action - do you have your wallet. It keeps on repeating itself until the original condition changes - you find your wallet.

In coding terms:

while (Do you have your wallet == false){
    search bag;
    search room;
    search car;
    search under couch;
    etc...
}

No comments:

Post a Comment