Files
oraset/website/docs/md/tutorial.md
T
JGZ_YES 51ece5afed
Build and Package Oraset / build (macos-latest) (push) Waiting to run
Build and Package Oraset / build (ubuntu-latest) (push) Waiting to run
Build and Package Oraset / build (windows-latest) (push) Waiting to run
Build and Package Oraset / release (push) Blocked by required conditions
Initial commit: Oraset B-5
2026-07-23 13:25:11 +08:00

2.6 KiB

教程

通过实际示例学习 Oraset 编程语言 B-5 版本,从基础输出到循环和条件判断。

示例 1:基本输出

// hello.ora
print("Welcome to Oraset B-5!");
print("This is my first program.");

输出:

Welcome to Oraset B-5!
This is my first program.

示例 2:变量和计算

// calc.ora
var a = 10;
var b = 20;
var sum = a + b;
print("The sum is:", sum);

输出:

The sum is: 30

示例 3:条件判断

// grade.ora
var score = 85;
if (score >= 60) {
    print("Pass!");
} else {
    print("Fail!");
}

输出:

Pass!

示例 4:循环

// loop.ora
var i = 1;
while (i <= 5) {
    print("Count:", i);
    i++;
}

输出:

Count: 1
Count: 2
Count: 3
Count: 4
Count: 5

示例 5:不换行输出

// inline.ora
print("Loading", "nel");
print(".");
print(".");
print(" Done!");

输出:

Loading...
 Done!

示例 6:文件级配置

// no_newline.ora
sg<nel>
print("All");
print("on");
print("same");
print("line");

输出:

Allonsameline

示例 7:数组

// array.ora
var arr = [10, 20, 30, 40, 50];
print("Array:", arr);
print("First element:", arr[0]);
print("Array length:", arrlen(arr));

arr[2] = 35;
print("Modified array:", arr);

输出:

Array: [10, 20, 30, 40, 50]
First element: 10
Array length: 5
Modified array: [10, 20, 35, 40, 50]

示例 8:复合赋值

// compound.ora
var x = 10;
x += 5;
print("x += 5:", x);
x *= 2;
print("x *= 2:", x);
x -= 3;
print("x -= 3:", x);

输出:

x += 5: 15
x *= 2: 30
x -= 3: 27

示例 9:函数

// function.ora
function add(a, b) {
    return a + b;
}

function multiply(a, b) {
    return a * b;
}

var result = add(5, 3);
print("5 + 3 =", result);

result = multiply(4, 6);
print("4 * 6 =", result);

输出:

5 + 3 = 8
4 * 6 = 24

示例 10:数学函数

// math.ora
var pi = 3.14159;
print("sin(pi/2) =", sin(pi/2));
print("sqrt(16) =", sqrt(16));
print("abs(-42) =", abs(-42));
print("pow(2, 8) =", pow(2, 8));
print("rand() =", rand());

输出:

sin(pi/2) = 1
sqrt(16) = 4
abs(-42) = 42
pow(2, 8) = 256
rand() = 0.123456

练习题

练习 1

编写一个程序,计算 1 到 10 的和。

练习 2

编写一个程序,判断一个数是奇数还是偶数。

练习 3

编写一个程序,输出 99 乘法表的一行。

练习 4

编写一个程序,使用数组存储 5 个数字,然后计算它们的平均值。


Oraset Project © 2026