VisuAlg Variables and Data Types Explained

The four primitive types, how to declare them, and the naming rules that save you from confusing errors.

Updated October 1, 20266 min read
Laptop showing VisuAlg variables of type inteiro, real, caractere and logico in the memory panel

A variable is a named box in memory. In VisuAlg every box has a type, and the type decides what you can store in it. Getting types right early prevents most of the "type incompatible" errors new students meet.

The four primitive types

TypeStoresExample
inteiroWhole numbers, positive or negativeidade <- 19
realNumbers with decimalsaltura <- 1.75
caractereText of any lengthnome <- "Ana"
logicoVERDADEIRO or FALSOativo <- VERDADEIRO

Note that decimal numbers use a dot, not a comma: write 1.75, never 1,75.

Declaring variables

var
   idade, ano: inteiro
   altura, peso: real
   nome: caractere
   matriculado: logico

You can declare several variables of the same type on one line, separated by commas. All declarations live in the var section, before inicio.

Naming rules

Mixing types in expressions

You can assign an inteiro to a real variable, but not the reverse. Division with / always produces a real result, so storing it in an inteiro causes an error. When you need whole-number division, use \, and use % (or mod) for the remainder.

algoritmo "divisao"
var
   a, b, q, r: inteiro
   media: real
inicio
   a <- 17
   b <- 5
   q <- a \ b      // 3
   r <- a % b      // 2
   media <- a / b  // 3.4
   escreval(q, " ", r, " ", media)
fimalgoritmo

Logical values in practice

Logical variables shine when you store the result of a comparison: aprovado <- nota >= 7. You can then test se aprovado entao later, which keeps conditions readable.

Watch the variables panel while the program runs. It lists every variable with its type letter (I, R, C or L) and current value, which is the fastest way to understand types.

Converting between types

Built-in functions help when you need to switch types: int(x) drops the decimals of a real, numpcarac(n) turns a number into text and caracpnum(t) turns text into a number.

Now put your variables to work with conditional statements.

More VisuAlg guides