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
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Nim2
* Scala


[ edit | logout ]
[ sandbox | passwd ]

break and continue

They exist in Nim and they work just like in Python/C.

However, when you have nested loops, you can break out of an outer loop (Java has this too).

Take this example:

for i in 1..3:
  for j in 1..2:
    echo &"{i} - {j}"

Output:

1 - 1
1 - 2
2 - 1
2 - 2
3 - 1
3 - 2

Say we want to stop the loops after "2 - 2". Here is how to do it:

block outer:
  for i in 1..3:
    for j in 1..2:
      echo &"{i} - {j}"
      if i == 2 and j == 2:
        break outer
    echo ""

A block can have a label, which is optional. Here we need that label.

Output:

1 - 1
1 - 2
2 - 1
2 - 2

The block keyword

The block keyword introduces a new scope.

block introduces a new scope
#include <stdio.h>

int main()
{
    int i = 2;         // let's call it "the old i"
    printf("%d\n", i); // 2

    {
        // new scope
        int i = 10;        // new i variable
        printf("%d\n", i); // 10
        // the new i dies at the end of the block
    }

    printf("%d\n", i); // 2, it's the old i

    return 0;
}
let i = 2
echo i  # 2

block:
  let i = 10
  echo i  # 10

echo i  # 2

Comments are given in the C code (left side). The same thing happens in Nim.

Now the block has no label. It's an anonymous block.

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 April 05, 15:10