AlgoML
FR

Your first program

Write, generate and run a program that reads a value and displays a result.

The smallest program

Algorithm Hello
version '1.0'
language C
begin
write("Hello, world!\n")
end
end algorithm
Output Hello, world!hello.aml on GitHubTry itSoon

Every program has the same frame:

  • Algorithm Hello gives the program its name. The generated Java class and the file names use it.
  • version is a free text, between quotes.
  • language C chooses the language to generate. Change it to JAVA or PYTHON and generate again: the algorithm stays the same.
  • The instructions go between begin and end, and the file ends with end algorithm.

write displays text and values. "\n" goes to the next line.

Reading values

A program becomes useful when it works on data given by the user. Declare variables before begin, then read them with read.

Algorithm Greeting
version '1.0'
language C
variable name : STRING
variable age : INT
begin
write("What is your name? ")
read(name)
write("How old are you? ")
read(age)
write("Hello ", name, ", next year you will be ", age + 1, ".\n")
end
end algorithm
Input Ada 36Output What is your name? How old are you? Hello Ada, next year you will be 37.greeting.aml on GitHubTry itSoon
  • variable name : STRING declares a variable called name that holds text.
  • read(name) waits for the user to type a value and stores it in name.
  • write accepts several values separated by commas and displays them one after the other, with no space added. Put the spaces in your strings.

Generate and run

In VS Code, open the file and run the AlgoML generate command. From a terminal:

algoml-generate greeting.aml .
gcc Greeting.c -o greeting
./greeting

For Java, compile with javac Greeting.java and run java Greeting. For Python, run python3 Greeting.py.

Exercise

Exercise

Read a number of minutes and display it in hours and minutes. For example, 135 gives 2 h 15 min. Hint: div gives the quotient of a division and mod gives the remainder.

Report a problem with this page