For Loop

4 minutes
Share the link to this page
Copied
  Completed
In computer science, a for-loop (or simply for loop) is a control flow statement for specifying iteration, which allows code to be executed repeatedly. The syntax of a for-loop is based on the heritage of the language and the prior programming languages it borrowed from, so programming languages that are descendants of or offshoots of a language that originally provided an iterator will often use the same keyword to name an iterator, e.g., descendants of ALGOL use "for", while descendants of Fortran use "do." There are other possibilities, for example, COBOL which uses "PERFORM VARYING".

Transcript

Hello, and welcome to the 23rd tutorial on c++ programming series. And in this part we're going to be looking at the following loops essentially allow you to repeat code helping reduce code as well. This could be useful for them if you have an array of high scores, and you want to print out the score and you could loop through the array. And if let's say the array only only holds five scores, you could individually print out doing one nine for each print, but imagine if you had 1400 scores or even more, then that's gonna be a lot on the cover the fees up front now to make it dynamic, but you can also repeat colors to show you the syntax of a following. The next few tutorials will just be covering the different types of loops. For loops are probably what you're used to most.

And the syntax of follow up keyword for then go back keyword for then you open close bracket and the first part is you initialize any variable so it can be int or equals zero. The next section, as I said is the condition. So as long as this condition is true, the loop will be run, you generally don't want an infinite loop. So you do want the condition to end the i less than five so while it is less than five, it starts at zero. And this is usually the section where you affect this variable. So it's going to put i plus plus.

And then in here, you just put what you want to do, and we're just gonna do STD C and then at this printout, I want to say iteration, colon long as TT Right, and if we run that flow property boom, there we go is printed out 01234. We started at zero, while i is less than five not equal for both we put less than or equal to no and fine. That is literally it for for loops. It's that simple, obviously. So this is something that I say you will use a lot. And it is immensely powerful and it's something uses experimentation to actually truly get to grips with this VISTA overview of the syntax and what if, and if no, obviously have nested for loops if you have a for loop within an if statement or an F within the for loop and what have you.

Like a if statement, you don't have to have the brackets. But if you don't have the brackets, then he only runs the line immediately following Then I mean the actual code line because I'll put a space there are new lines until then see that as a actual code in line but he only wrote the code in line immediately following the following within that loop so if I were to do here STD c STD line and run this in only printf 44 one because it's not within the for loop if you don't put brackets on it, one nine days included within the for loop. But if you do actually put brackets you can put as many lines as you want. Except that's it for this tutorial. If you have any question feel free to message us at support at some law systems that call AK the email will be in the description you can come to this video with historically messages via YouTube or the recoil info source code will also be in the description in the next part will be looking at the while loop very similar to the for loop.

Thank you for watching and I hope you have a nice day. Day

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.