vi studio

Lessons · The language · 15 of 36

Conditions and loops

if / else, the inline if, and, or, not, for and while.

if runs a block when its condition is true; else if and else cover the other cases. Combine conditions with and, or and not. The short form a if (condition) else b picks a value.

main grades():
  for score in [95, 82, 64]:
    if (score >= 90):
      print(score, "is an A")
    else if (score >= 80):
      print(score, "is a B")
    else:
      print(score, "needs work")
  n = 0
  while (n < 3):
    n = n + 1
  print("counted to", n)
  late = false
  print("on time" if (not late) else "late")

The short form works in attributes too: color: green if (done) else gray.

Every example here runs in Vi Studio: open it in your browser (phones too) and press Start learning on the welcome page. Each code block there has a Try it button that runs it in the phone preview.

More in The language

About Vi. Vi is a free, open-source programming language for building real mobile apps. You describe each screen in plain words, and Vi compiles it to React Native (Expo) for Android, iOS and the web. Its editor, Vi Studio, runs on Windows, in any browser and on your phone, with the app running live as you type. Read more about Vi.