Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

* Programming 1 (BI)
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2025/26/2
* archívum


Linkek

* kalendárium
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C#
* D lang.
* Java
* Nim
* Nim2
  + exercises
* XC=BASIC
* old
  ◇C++, ◇Clojure, ◇Scala


[ edit | logout ]
[ sandbox | passwd ]

Finite and infinite iterators

An iterator can be finite or infinite. Let's see an example for each.

Finite iterator

iterator down(n: Positive): int =
  ## Iterate down to 0 (incl.).
  var n: int = n
  while n >= 0:
    yield n
    dec n

proc main() =
  for n in down(3):
    echo n

main()

Output:

3
2
1
0

Infinite iterator

An infinite iterator would never stop generating values. So you must pay attention on the caller side to stop the iteration (with a break, for instance).

iterator up(n: int): int =
  ## Iterate up from `n` (incl.).
  var n = n
  while true:
    yield n
    inc n

proc main() =
  var cnt = 0
  for n in up(10):
    echo n
    cnt += 1
    if cnt >= 5:
      break
    #
  #

main()

Output:

10
11
12
13
14

Here we print just the first five elements and then we stop.

Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2026 May 07, 19:21