PG0 - Tutorial

Introduction

PG0 is a programming language designed for learning programming.

It focuses on understanding the basic structures essential to programming: sequential, branching, and looping.
For simplicity, the language specifications are very straightforward, supporting only one type of branching and looping, and handling only integer data types.

How to Use PG0

PG0 Execution Screen
Editor
Variables
Console

PG0 Execution Screen (Mobile)
Editor
Variables
Console

Editor

This is the section where you write your program.
During execution, the current line is highlighted in light blue.
Editing is not possible during execution​.

Variables

This displays the variables and their values during program execution.
Values changed by the current line are highlighted in red.
Arrays can be expanded to view their contents​.

Console

This displays information about the execution (Start, Stop, End).
If a variable is specified with the 'exit' command, its content is displayed in the console as the execution result.
Errors in the program are displayed in red in the console.

Execution

Automatically executes the program written in the editor, line by line.

Step execution

Executes the program step-by-step.
Re-selecting step execution runs the next line.
This feature allows manual execution of the program, one line at a time.

Execute to Cursor

Executes the program up to the line where the editor cursor is.

Stop

Stops the running program. A stopped program cannot be resumed.
For pausing a program, select step execution.

Execution speed

Choose the speed of automatic line-by-line execution.
Selecting "No wait" will execute the program quickly until the end.

Variables

Containers for assigning numerical values.
Use '=' to assign numbers, variables, or computation results to variables.


a = 10 // Assigns 10 to variable a

Variables are prepared when used but can be explicitly declared with 'var'.


var i, j = 100 // Multiple variables can be declared using ','

The result of executing the above in the editor is shown below.

Arrays

Arrays are containers where variables are sequentially arranged.
Specify '[]' after a variable to handle it as an array.


a[0] = 100 // First element
a[1] = 200 // Second element
i = 5
b[i] = 10 // Specify index with a variable

The result of executing the above in the editor is shown below.

To initialize arrays, assign '{value1, value2, value3,...}'.


a[] = {1,2,3} // Equivalent to a[0] = 1; a[1] = 2; a[2] = 3;
b[] = {1,2,{10,20,{100,200,300}},3} // Multi-dimensional initialization

The result of executing the above in the editor is shown below.

Basic Structures

Sequential

Each statement is written on a single line.
Statements are executed in order from the top
Statements perform calculations with variables and numbers, and assign to variables.

ExpressionsDetails
c = a + bAdd a and b and assign to c
c = a - bSubtract a and b and assign to c
c = a * bMultiply a and b and assign to c
c = a / bDivide b from a and assign to c
c = a % bAssigns the remainder of a divided by b into c



a = 1 // First executed, assigns 1 to variable a
b = a + 2 // Second executed, assigns the result of a + 2, which is 3, to variable b

The result of executing the above in the editor is shown below.
Blocks are used to group multiple statements, enclosed with { and }.


{
	a = 1
	b = a + 2
}

Variables prepared in the block are released when exiting the block.


{
	var a = 1
}
b = a	// a inside the block is released, so b becomes 0

To prevent release, declare the variable outside the block.


var a
{
	a = 1
}
b = a	// a is prepared outside the block, so b becomes 1

Branching

if

The 'if' statement controls the execution of statements based on conditions.


if (condition) {
	statement
}

The statements are executed if the condition is true. If not, the statements are skipped.
Conditions involve comparisons of variables and numbers, such as:

ConditionsDetails
a == bTrue if a and b are equal
a != bTrue if a and b are not equal
a < bTrue if a is less than b
a > bTrue if a is greater than b
a <= bTrue if a is less than or equal to b
a >= bTrue if a is greater than or equal to b
a && bTrue if both a and b are true
a || bTrue if either a or b is true



a = 2
b = 1
if (a > b) {
	b = a // Executes when a is greater than b
}

The result of executing the above in the editor is shown below.

else

The 'else' statement specifies the statements to execute if the 'if' condition is not met.
If the 'if' statement executes, the following 'else' statements will not execute.


a = 1
b = 2
if (a > b) {
	b = a  // Executes when a is greater than b
} else {
	a = b  // Executes when a is less than or equal to b
}

The result of executing the above in the editor is shown below.

Looping

while

The 'while' statement performs a loop based on a condition.


while (condition) {
	statement
}

The statements are repeatedly executed as the condition is true.


i = 1
while (i <= 10) { // Loop from 1 to 10
	i = i + 1
}

The result of executing the above in the editor is shown below.

Other

To terminate a program that is running, write 'exit'.


exit

You can set a variable with 'exit', and the content of that variable will be displayed in the console as the execution result.


i = 100
exit i

The result of executing the above in the editor is shown below.

For a comprehensive understanding of PG0 language specification, refer to "pg0.txt".

PG0.5, an advanced version of PG0 with functionalities similar to standard programming languages, is detailed in "pg0.5.txt".
Selecting "PG0.5" mode in the menu allows you to run programs in PG0.5.
The library for PG0.5 allows screen drawing, sound playback and mathematical functions.
PG0.5 - Library Reference

Sample Programs

Here are sample programs for PG0. You can execute these in PG0.

1_var.pg0

This is a sample using variables.


a = 1
b = 2
c = a + b
exit c

2_array.pg0

This is a sample using arrays.


a[] = {1,2,3}
a[3] = 4
b[] = {5,6,7}
c[] = a + b
exit c

3_if.pg0

This is a sample to determine leap years using if statements.


// Determine leap year
var year = 2018
var leapYear
if (year % 400 == 0 ||
	(year % 4 == 0 && year % 100 != 0)) {
	// It's a leap year
	leapYear = 1
} else {
	leapYear = 0
}
exit leapYear

4_while.pg0

This is a sample for adding numbers from 1 to 10 using while statements.


// Adding from 1 to 10
cnt = 0
i = 1
while (i <= 10) {
	cnt = cnt + i
	i = i + 1
}
exit cnt

5_max.pg0

This is a sample to find the maximum value in an array.


// Finding the maximum value
num[] = {3,4,56,25,47,72,2,23,25,9}
size = 10
max = num[0]
i = 1
while (i < size) {
	if (max < num[i]) {
		max = num[i]
	}
	i = i + 1
}
exit max

6_sort.pg0

This is a sample for sorting the contents of an array in ascending order.


// Sorting an array
num[] = {3,4,56,25,47,72,2,23,25,9}
size = 10
i = 0
while (i < size - 1) {
	j = size - 1
	while (j > i) {
		if (num[j - 1] > num[j]) {
			tmp = num[j]
			num[j] = num[j - 1]
			num[j - 1] = tmp
		}
		j = j - 1
	}
	i = i + 1
}
exit num

Links


Close
Copyright © 1996- by Ohno Tomoaki. All rights reserved.