[spoiler title=”Lesson Video”]
Direct Download of Video (For mobile / offline viewing)(right click > save target as)
[/spoiler]
[spoiler title=”Lesson Source Code”]
#include
using namespace std;
int main(){
//THERE IS HOMEWORK, I JUST RAN OUT OF TIME AND FORGOT TO MENTION IT
//Using 1 for and 1 while loop, cout 1-10 and 10-1 counted by 1's, and by 2's respectively.
//Bonus: Allow the user to enter a starting and ending value, as well as a number to count by, implement this using both a for and a while loop
//int i = 0;
//while ( i < 10 ){
// cout << i++ < 10){
shouldRun = false;
}
i += j;
cout << i << endl;
}
/*for (int i = 0; i < 10; i++){
cout << i << endl;
}*/
system("PAUSE");
return 0;
}
[/spoiler]
Homework: https://beginnerscpp.com/forums/index.php/topic,62.0.html
Loops
The over-arching theory of loops is that sometimes you’re going to have a set of data, which will need to be iterated through and have actions performed on each member. You can use if statements to qualify what happens to each item, but the loop itself ensures that every item in a set is able to be iterated through.
While
While loops are the simplest of the loops that we’re going to be discussing. The syntax is exactly the same as an if statement.
while ( condition ) { //code }
//Compared to
if( condition ) { //code }
One of the catches of using an while statement is that there needs to be some kind of a control in place or the loop will run endlessly. The most common way to see a while loop implemented for beginners is by incrementing a number. Here’s an example of how someone would do that:
int counter = 0;
while(counter <10){
cout << counter << endl;
counter++;
}
// ***Please note that this code could be simplified to just cout << counter++ << endl; And have the same effect I believe I did this in the video.
Do_While loops
These loops weren’t actually covered in the videos, as I don’t feel that they are as important in theory as other loops. The purpose of a do_while loop is to ensure that something happens at least once. Typically, in programs you might see a do_while used to make a user enter a number within a certain range. For example:
int counter = 0, choice = 0;
do{
if(counter > 0 ){
cout << "The number you entered is invalid, please enter a number between 1 and 10: ";
cin >> choice;
counter++;
}
else{
cout << "Please enter a number between 1 and 10: ";
cin >> choice;
counter++;
}
}while(choice <1 || choice >10);
cout << "It took you: " << counter << " tries to enter a number in the 1-10 range"
[pre>
This code is just an example, but typically the reason you use a do_while is because the while may not be triggered and you need it to run at least one time. We will get more into this as we progress in the series and out programs get more structurally complex.
For loop
For loops are the most complex type of loop that we will be covering, and they combine a lot of the things that are "lacking" from while loops and reduces their complexity. For loops are often complex for beginners, so they avoid them. This often leads to more complexity in their program due to having more variables, and more lines of code. Below is an example of a simple for loop that emulates the while loop above.
for ( int counter = 0; counter <10; counter++){
cout << counter;
}
As you can see, the above code took 3 lines, instead of 5. Let’s break down what exactly happened in this for loop. For loops are often broken into 4 basic parts:
-
for statement –the literal use of the word “for” at the start of the line
-
Initialization –This part allows you to create a variable or assign an existing variable a new value. An example of this is int counter = 0; as above.
-
Conditional –This is the statement that must remain true (like a while) for the loop to continue running.
-
Increment –This statement typically (although it doesn’t have to) brings your initialized variable closer to satisfying the conditional.
Here is one more example of for loops
int myVariable = 5, goal = 25;
for ( int counter = myVariable; counter < goal; counter++){
cout << "We started this loop at: " << myVariable << " -- we are now at: " << counter << " -- and we're counting up to " << goal << endl;
}
It is strongly recommended that you review this lesson and familiarize yourself with loops in some depth, as the next lesson will be VERY challenging as it deals with nested loops.