Learn how to use a for loop to set up a block of code to repeat a certain number of times. In this video, review the syntax and structure of a for loop and an enhanced for loop.
- [Narrator] What is the syntax for a for loop? The for loop is probably one of the most common types of loops that you'll use in programing. A for loop repeats a block of code a specified number of times. Here's the syntax for a for loop: use the word for, open parenthesis. In this case I'm using the auto to determine the data type i equals one. So since I'm using auto, what data type do you think i is going to be? If you said integer, you're absolutely correct. Because I'm setting i equal to one, i is an integer.
Notice there's a semicolon after that. In the for loop, there's three parts. The first part is your initialization. The second part is your test condition. It says as long as i is less than or equal to 100, I'm going to keep going. As soon as i reaches 101, it's going to stop. Or something greater than 100. Then you have a semicolon. Then the last part, the i++, is what we call the update condition. In this example, I'm incrementing i by one each time.
So this will actually execute 100 times. Let's switch over to the IDE and write program to print out all the odd numbers from one to 100 so we can see the syntax for a for loop in action. I'm going to create a new project. And I'll just call it odd numbers.
Let's start at the very top by adding our using namespace. Now, in our main, this one's actually going to be pretty simple, we don't really need to declare any variables cause all we're going to do is check to see if i is even or odd and if it's odd, we'll print it out. So let's create our for loops. So we take for, open parenthesis, and I'm going to use the auto I'm going to say i equals one, semicolon. And then I'm going to say as long as i is less than or equal to 100, that will allow me to go from one to 100.
Semicolon and I'm going to add one to i each time. Similar to our while loop and do while loop, we enclose the body of the for loop with curly brackets. Notice I did not put a semicolon at the end of the for loop. That would cause an infinite loop. So in here, what I want to do, is I want to check and see if i is even or odd so I'm going to us an if statement. Can you think of an easy way to check and see if i is even? If you said use the modulus operator, that's a great idea.
So we're going to say if i percent sign, and we're going to divide it by two. Remember, that gives us the remainder. So if the remainder is equal to zero, then it's even. So let's actually do the opposite. Let's say if i percent sign two is not equal to zero, that will tell me that it is an odd number. And in the next line, I'm going to go ahead and print out i and I just want to put a space between i and the next number so I'm going to go ahead and put a space there.
I'm not going to do an end out because I don't want the numbers to print on separate lines. So now, that's all the code I need because if it's even, I'm just going to ignore it and go back up to the for loop and get the next number. Let's compile and run our program. And I'll call it odd numbers. And you can see, we have the numbers 1, 3, 5, 7 all the way up to 99. So it looks like our program is working.
I do want to mention that there are some common problems that can be solved using loops. Finding the sum and average of a series of numbers, counting how many times a match occurs in a series of values, finding the first match, finding the max and min values, and comparing adjacent values and running simulations. So loops are a very powerful feature of C++.
- Compiling and running C++ programs
- Creating variables in C++
- Choosing the correct data type to represent variables
- Creating assignment expressions
- Changing data types in C++
- Comparing values in C++
- Using logical operators
- Comparing strings in C++
- Defining and calling a function
- Using arrays in C++