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 ]

case statement

In C it is called a switch statement.

Example 1

let x = 3

case x
  of 1:
    echo "one"
  of 2:
    echo "two"
  of 3:
    echo "three"
  else:
    echo "something else"

𝥶At the end of the of branches, no break is necessary. Execution won't flow over the next branch.

Example 2

match multiple values
let x = 3

case x
  of 1, 2:
    echo "one or two"
  of 3..5:
    echo "between three and five"
  else:
    echo "something else"
let x = 3

case x
  of 1, 2:
    echo "one or two"
  of 3..4, 5:  # also works
    echo "between three and five"
  else:
    echo "something else"

𝥶You can also match multiple values as well as a range in a branch.

Cover all cases

A case statement must cover all possible cases. If the else part is not important, then discard it:

let x = 10

case x
  of 1:
    echo "one"
  of 2:
    echo "two"
  of 3:
    echo "three"
  else:
    discard

The case expression

case can be an expression, similarly to if:

let x = 3

let status = case x
  of 1, 2:
    "one or two"
  of 3..5:
    "between three and five"
  else:
    "something else"

echo status    # between three and five
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 09, 10:26