Write Your First Algorithm in VisuAlg

Understand the four parts every program needs and build a small calculator that reads input and prints a result.

Updated October 1, 20266 min read
Desktop monitor showing a first VisuAlg greeting algorithm with its output

Every VisuAlg program follows the same skeleton. Once you learn it, you can read almost any algorithm your teacher puts on the board. In this tutorial we build two small programs: a greeting and a sum calculator.

The four parts of an algorithm

  1. algoritmo "nome" gives the program a name.
  2. var opens the section where you declare variables.
  3. inicio marks where the commands start.
  4. fimalgoritmo ends the program.

Anything you write after // is a comment, ignored by the interpreter. Use comments to explain your thinking, and your teacher will thank you.

Program 1: a greeting

algoritmo "saudacao"
var
   nome: caractere
inicio
   escreva("Qual o seu nome? ")
   leia(nome)
   escreval("Ola, ", nome, "! Bem-vindo ao VisuAlg.")
fimalgoritmo

escreva prints text and keeps the cursor on the same line. escreval prints and jumps to a new line. leia waits for the user to type a value and stores it in the variable.

Program 2: adding two numbers

algoritmo "soma"
var
   a, b, total: inteiro
inicio
   escreva("Primeiro numero: ")
   leia(a)
   escreva("Segundo numero: ")
   leia(b)
   total <- a + b
   escreval("A soma e ", total)
fimalgoritmo

The arrow <- is the assignment operator. Read it as "receives": total receives a plus b. You cannot use = for assignment, because in VisuAlg = is a comparison.

Running and saving

Press F9 to run. The output appears in a console window and the variables panel shows each value as it changes. Save your work with Ctrl+S; files use the .alg extension and are plain text, so you can open them in any editor later.

Formatting numbers

When you print a real number you can control its width and decimal places with a colon: escreval(media:6:2) prints the value in six characters with two decimals. This is handy for tables and money values.

Indent the lines between inicio and fimalgoritmo by three spaces. VisuAlg does not require it, but indentation makes errors far easier to spot.

Common beginner mistakes

Ready for more? Learn how each data type in VisuAlg behaves.

More VisuAlg guides