For loop
In computer science, a for-loop (or “for loop”) is used to run specified lines of code repeatedly. In different languages, there are different keywords to s…
In computer science, a for-loop (or “for loop”) is used to run specified lines of code repeatedly. In different languages, there are different keywords to specify this statement—ways to identify a for loop—but they all do the same task. Most languages use either “for” or “do” as keywords.
A for-loop has two parts: a header, and a body of code. The body consists of a set of instructions (lines of code) that run for each repetition of the loop. The header often declares how many loops to complete, generally indicated by either a loop counter or a loop variable. For-loops are typically used when the number of repetitions are known. For-loops are similar to “while-loops”, but usually have a known number of repetitions.
Sample for-loop in Python:
for i in range(10):
print('Hello, world!')
This for loop prints outs "Hello, world!" ten times.
The name for-loop comes from the English word for, which is the direct translation of the earlier German für, used by Heinz Rutishauser, who also helped define ALGOL 58 and 60.
A for-loop is available in most common programming languages. While for-loops in different languages will all fulfill the same task, there are differences on how each programming language deals with for loops.
Loop Counter
In computer science, a loop counter is a variable that controls how many repetitions the loop will do. Often, the loop counter variable is an integer value that increments by 1 for each completed loop. However, loop counters can also decrement, and can have step sizes other than 1.
Loop counters change each iteration. With each repeat, there is a unique counter value. The loop counter is the deciding factor for when a loop should end and go onto the next section of code.
Common identifier names for loop counters are i, j, and k (and so on, if needed).
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.