system.io
Input/Output operations for console and standard streams.
import system.io as io
Output Functions
println(value)
Print a value followed by a newline.
io.println("Hello, World!")
io.println(42)
io.println([1, 2, 3])
print(value)
Print a value without a trailing newline.
io.print("Enter your name: ")
let name = io.readLine()
io.println("Hello, " + name)
printf(format, ...args)
Formatted output (C-style format string).
io.printf("Name: %s, Age: %d\n", "Alice", 30)
io.printf("Pi: %.2f\n", 3.14159)
io.printf("Hex: %x\n", 255)
Format specifiers:
| Specifier | Description | Example |
|---|---|---|
%s | String | "hello" |
%d | Integer (decimal) | 42 |
%f | Float | 3.14 |
%.Nf | Float with N decimals | 3.14 |
%x | Hexadecimal | ff |
%o | Octal | 77 |
%b | Binary | 1010 |
%% | Literal % | % |
Input Functions
readLine()
Read a line of text from standard input.
io.print("What is your name? ")
let name = io.readLine()
io.println("Hello, " + name + "!")
readInt()
Read and parse an integer from standard input.
io.print("Enter a number: ")
let num = io.readInt()
io.println("You entered: " + num)
readDouble()
Read and parse a floating-point number.
io.print("Enter a decimal: ")
let value = io.readDouble()
io.println("You entered: " + value)
readChar()
Read a single character.
io.print("Press any key: ")
let ch = io.readChar()
io.println("You pressed: " + ch)
Error Output
eprintln(value)
Print to standard error with newline.
io.eprintln("Error: File not found")
eprint(value)
Print to standard error without newline.
io.eprint("Warning: ")
io.eprintln("Low memory")
Stream Control
flush()
Flush standard output buffer.
io.print("Processing...")
io.flush() // Ensure output appears immediately
// ... do work ...
io.println(" done!")
Complete Examples
Interactive Calculator
import system.io as io
io.println("Simple Calculator")
io.println("Enter two numbers:")
io.print("First number: ")
let a = io.readInt()
io.print("Second number: ")
let b = io.readInt()
io.println("")
io.println("Results:")
io.printf(" %d + %d = %d\n", a, b, a + b)
io.printf(" %d - %d = %d\n", a, b, a - b)
io.printf(" %d * %d = %d\n", a, b, a * b)
io.printf(" %d / %d = %.2f\n", a, b, a / b)
Menu System
import system.io as io
fn showMenu() {
io.println("\n=== Menu ===")
io.println("1. Option One")
io.println("2. Option Two")
io.println("3. Exit")
io.print("Choice: ")
return io.readLine()
}
let running = true
while (running) {
let choice = showMenu()
if (choice == "1") {
io.println("You chose Option One")
} else if (choice == "2") {
io.println("You chose Option Two")
} else if (choice == "3") {
running = false
io.println("Goodbye!")
} else {
io.eprintln("Invalid choice!")
}
}
Progress Indicator
import system.io as io
import system.time as time
fn showProgress(current, total) {
let percent = (current * 100) / total
io.printf("\rProgress: [%d/%d] %d%%", current, total, percent)
io.flush()
}
io.println("Processing...")
for (let i = 1; i <= 10; i = i + 1) {
time.sleep(100) // Simulate work
showProgress(i, 10)
}
io.println("\nDone!")
Next: system.math →