Skip to content

Latest commit

 

History

History
9 lines (7 loc) · 777 Bytes

File metadata and controls

9 lines (7 loc) · 777 Bytes

For Loops

What does for loop do:

For loops are used for sequential traversal. For example: traversing a list or string or array etc. In Python, there is no C style for loop, i.e., for (i=0; i<n; i++). Let us learn how to use for in loop for sequential traversals.

A for-loop has two parts: a header specifying the iteration, and a body which is executed once per iteration. The header often declares an explicit loop counter or loop variable, which allows the body to know which iteration is being executed. For-loops are typically used when the number of iterations is known before entering the loop. For-loops can be thought of as shorthands for while-loops which increment and test a loop variable.

for loop syntax:

for iterator_var in sequence: statements(s)