Please forget the good Captain's 2nd and 3rd explanations. Even I would find them confusing if I was trying to learn loops. Just concentrate on his first example.
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
I'm sure there are multiple definitions of what a sentinel is, but I think of it as something that triggers an action. For example, if I want to do one thing if the variable called changeFunctions is true and another thing if the changeFunctions is false, then changeFunctions could be considered a sentinel. When sentinels occur within the () after a loop keyword, like while, then the phrase a loop conditional instead of sentinel could be used. The value of the sentinel/loop conditional will control whether the body of the loop is continues or not.
There are three types of loops:
1) for loops are most often used when you know the number of times you want to do something.
2) do/while loops are most often used if you want the body of the loop to be run at least once, no matter what the sentinel/loop conditional value is
3) and while loops are used most often when the body of the loop could be run an indefinite number of times depending on the value of the sentinel/loop conditional.
For a while loop the sentinel/loop conditional must be interpretable as true to get the loop to run the first time through so make sure the value is set to true before you begin the loop if you want it to run at least once. Each time while loop or do while loop you can determine whethet to run the loop again or not. If not, then change the value of the sentinel/loop conditional to be false.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396