Hey Mich, I have recently started my AS Computing course and am so far loving it. We've been doing visual basic and even what I learned very basically in python is helping me. However, one problem that everyone in my class seems to be suffering from is understanding loops and how they work. This is a much general question, but generally in the programs we've been looking at, I can't find many examples of loops anywhere. There are three types, For, While and Do, but which one applies where and if so could you please use examples to explain it to me? Thanks.
For loops are probably the most commonly used loops since you can declare your local variables that you need to use, specify all your conditions for looping, and specify all the updating code that should be executed after each iteration of the loop in one line of code. If you are new to programming, you may have recently learned about arrays which are commonly looped through with for loops. The examples below are valid for languages such as C++, Java, C#, Javascript etc. (those with a C based syntax).
// We declare our local variable "start" that we are going to use as a counter
// While start's value is less than 10, execute whatever is inside the loop
// After the code nested in the loop has been executed, execute the code "start++"
// which increments the value of "start" by 1.
for(
int start = 0;
start < 10;
start++) { // This would make "start" iterate from 0 to 9 which is 10 iterations
// code
}
// We can also do more complex loops. This example isn't very useful, but it shows
// you how you can declare multiple values, specify multiple conditions, and specify
// multiple update commands.
for(int a = 0, b = 100; a < 10 && b > 90; a++, b=b-2) {
// code
}
// If you wanted to loop through an array backwards you would do something like this.
for(int a = array.length; a > -1; a--) {
// code
}
As you can see, for loops are typically used with integers, but this is not always the case. You don't have to use integers in any of the loops.
While loops only provide a place for you to specify the conditions of your loop. The first for loop shown above would look like this if you used a while loop. You have to declare your counter variable ahead of time, and manually increment your variable inside of the loop. This can be useful if you don't always want to increment your variable at the end of the current iteration.
int start = 0;
while(start < 10) {
// code
start++;
}
While loops are used a lot when you need something to continue "while this is true". You could do something like while(aBoolean == false) or while(player.isNotDead()). While loops are also useful for infinite loops (although the other loops can do the same), such as while(true) or while(1). These types of loop will execute forever unless you break them. You have many infinite loops running on your computer right now, such as a loop that is constantly checking for keyboard input or mouse movement.
Do while loops aren't used as much as the others, but they are useful when you want the loop
to execute at least once. Even if the condition is false, the loop will still execute the first time since the condition is at the bottom of the loop. The example below uses a do while because we definitely want to print out to the screen before we test the value of the "answer" variable.
int answer;
do {
cout << "Do you wish to quit or continue?" << endl;
cout << "1 = Continue, 0 = Quit" << endl;
cin >> answer;
} while(answer);