## **Introduction**
Understanding the structure of a Nuts program and how to use variables effectively is crucial for writing efficient and functional Nuts code. In this guide, I will walk you through the core structure of a Nuts program and explain how variables work, how to manipulate them, and how to use them in your programs.
---
## **Chapter 1: Nuts Program Structure**
A Nuts program consists of three key parts:
1. **Head** – Initializes the program and switches to the Nuts programming environment.
2. **Body** – Contains the main program logic and commands.
3. **End Point** – Terminates the execution of the program.
### **1.1 Head (Program Initialization)**
The head of a Nuts program must switch the execution to the Nuts environment and define the first file to execute. Here is an example of a proper head section:
```nuts
set dst=program1
nuts
```
In this example, `dst` is set to `program1`, meaning execution will continue in the file named `program1`. The `nuts` command switches the program into the Nuts environment.
### **1.2 Body (Main Program Logic)**
The body consists of commands that perform operations such as displaying messages, calculations, and user interactions. For example:
```nuts
set dst=program2
set emt=Hello, welcome to Nuts!
emt
```
This code sets the next execution file as `program2` and displays the message "Hello, welcome to Nuts!" on the screen.
### **1.3 End Point (Terminating Execution)**
The end of a Nuts program is determined by setting `dst` to `exit` or a non-existent file:
```nuts
set dst=exit
brk
```
This stops execution and ends the program.
---
## **Chapter 2: Understanding Nuts Variables**
### **2.1 What Are Variables?**
A variable is a container for storing values. In Nuts, variables store either:
- **Integers** – Used for mathematical operations like addition and subtraction.
- **Strings** – Text values that can be combined but not calculated.
### **2.2 Creating Variables**
To create a variable, use the `set` command followed by the variable name and value:
```nuts
set name=John
set age=25
```
Here, `name` is assigned "John", and `age` is assigned 25.
### **2.3 Changing Variables**
To update a variable, simply assign a new value:
```nuts
set age=30
```
This changes `age` to 30.
### **2.4 Referring to Variables**
To use the value of a variable, enclose its name in `%` symbols:
```nuts
set num1=20
set num2=%num1%
```
Now, `num2` has the same value as `num1`.
### **2.5 Merging Variables**
You can merge two variables by referring to both in a new variable:
```nuts
set first=Hello
set second=World
set message=%first% %second%
```
Now, `message` contains "Hello World".
### **2.6 Resetting Variables**
To reset a variable, assign it an empty value:
```nuts
set name=
```
This clears the variable `name`.
---
## **Chapter 3: Essential Nuts Variables**
Some variables are essential in Nuts programming because certain commands require them. Below is a list of key variables:
| Variable | Usage |
|----------|------------------------------------------------|
| `dst` | Specifies the next execution file (required by most commands). |
| `emt` | Holds the message displayed by the `emt` command. |
| `cta` | Stores user input received during execution. |
| `num1` | First number used in mathematical and conditional operations. |
| `num2` | Second number used in mathematical and conditional operations. |
| `action` | Specifies where execution goes if a condition is met (used with `chke`). |
| `dly` | Specifies the delay duration in seconds before executing the next command. |
---
## **Conclusion**
Understanding the structure of a Nuts program and mastering variables is fundamental for writing efficient programs. With this knowledge, you can now create, modify, and manipulate variables to control the flow and functionality of your Nuts applications. Experiment with different variables and commands to improve your skills!
0 Comments