LinkedIn principal author Doug Winnie explains how for loops work and how you can use them as a programmer and coder to create loops that run a specific number of times. The for loop contains a special variable called an iterator, a conditional, and a step that works together to create the loop. For loops can be nested within each other to process through more complex situations requiring multiple dimensions of data.
- View Offline
- 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 iterator variable which is often just called i.…
Join Doug Winnie as he explains the principles of programming and helps you connect to core concepts by exploring three ways that programmers perform their jobs. Doug starts by sharing the history of coding and then dives into functions, values, variables, and parameters used to define actions. He covers capturing input from users, creating conditional tests, using loops with arrays, and object-oriented programming basics. He also takes you beyond programming, into processes like debugging, refactoring, and building iteratively.
- Working with values and variables
- Breaking down tasks
- Customizing functions and parameters
- Building conditional tests
- Creating and changing arrays
- Working with objects and classes
- Debugging and refactoring code
- Going beyond the code as a programmer