From the course: Computer Science Principles: Programming

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Use for loops

Use for loops

From the course: Computer Science Principles: Programming

Start my 1-month free trial

Use for loops

- Loops are helpful ways to take sections of code and run them multiple times. With while and do while loops, you need to have a condition that can evaluate based on existing data and variables. Sometimes you need to create a unique set of information for the loop that you will use for the conditional test. That is when you need a for loop. A for loop is similar to a while loop. At the top, you'll ask a question and then you'll have the code for the loop located afterwards. The new element that is added to a for loop is an iterator. This is a variable that is created only for the loop itself. You define it at the top of the loop, and it will only exist within the loop itself. The intention is that the variable you create at the top of the loop is used in the conditional test. With each repetition of the loop, you need to have a way to change the iterator. So that is the third item that is within the opening code of the for loop. So, the for loop contains the declaration of the…

Contents