## The step by step learning path to start coding with Dip.

Dip is aimed at beginners starting to learn to code. It is by no means complete, and does not aim to compete with the mainstream languages of today. However, it does prove a good place to start learning to code.

I recommend starting with section 1 and doing the exercises. After you're done with those, you should move on to section 2, and start doing the exercises there. Next, move on to section 3 and complete your transition from a Dip learner to a Dip user.

Don't worry if you're confused. You can always look at the documentation, ask a question on the forum, and if the problem persists, mail me personally.

### Section 3: Dip programming for veterans

Try the programs from the learning path in your browser!

## Section 1: Learning to code with Dip

Getting started with programming can be a daunting task. That's why I want to make it simple for you. Start out doing the following experiments with Dip, and familiarise yourself with the language

## 1A. Calculating With Dip's Environment

Learn to use Dip's environment like a calculator. Start by trying to add two numbers, like so:

`Dip> 1 + 1`

This will give you the output of 2. Try to familiarise yourself with the Dip environment by trying similar operations. Try using the '-' symbol to subtract numbers, the '*' symbol to multiply values, and the '/' symbol to divide values. Some examples could be:

`Dip> 45 - 24`
`Dip> 6 * 4`
`Dip> 7 / 7`
`Dip> 5 + (10 * 4) / 5`

Next, you can also try to use the '^' symbol to denote exponents, and the '%' symbol to calculate the modulus of a value

`Dip> 7 ^ 7`
`Dip> 5 % 5`

Great! You've learned how to use the Dip environment for making calculations, and that's a great step. Let's move on to building a simple application with Dip

## 1B. Writing your first Dip program: Greeting the user

Now you've learnt to use Dip's environment like a calculator, let's write our first program in Dip!

Open the Dip terminal and type the following (I recommend trying it yourself before looking at this):

`Dip> function greet(name) -> print("Hi, " + name)`

You should see this as the output:

`function greet`

The above line is Dip's way of letting you know that you've created a function called `greet`

Now, type `Dip> greet("Raghav")` into the Dip interface, to see the result of your work.

The output you should see is:

`Hi, Raghav`

How does it feel after creating your first program in Dip! Now, you can move on to section 1C, and learn to create a simple program to add two numbers!

## 1C. Looking Further - Creating a simple calculator in Dip

So you've learnt how to use the Dip editor as a calculator, and have written your first program. What better than to join these together, and create a program that adds two numbers?

Open the Dip terminal and type the following (As usual, try it out yourself before looking at this :) ):

`Dip> function add(num1, num2) -> print(num1 + num2)`

You should see this as the output:

`function add`

`Dip> add(2, 5)`

`7`

`Dip> add("Hi, ", "Raghav")`

`Hi, Raghav`

Feel free to customise this program, substituting it with subtraction, multiplication or division!

Great! You've learnt the basics of Dip! Move on to the intermediate section to write more complicated programs in Dip

## Section 2: Writing Complex Programs With Dip

Learn to run external files with Dip, recreate an old method of encryption, and pick up from were you left off in the previous section!

## 2A. Evaluating external files with Dip

Now that you've learnt to use Dip's basic features, let's move on to writing some complex programs with Dip.

It's possible to load external files written in Dip by calling the ‘run’ function inside the Dip interface, followed by the path to the file in brackets.

Please not that the run function only works when you install Dip, and not in the browser version.

```Dip Beta (Version 0.1) Type "help", "copyright", "credits" or "about" for more information. ```

This can be done like so

```Dip> run("/Users/raghav/Desktop/hello_dip.dip") Hello, Dip ```

Great! You've learnt how to evaluate external files. Now, we'll try to make a simple program that checks (upon getting the angles) whether a triangle is valid or not. To create this, let's first think about a few rules triangles have.

1. All triangles have 3 angles
2. The sum of these angles must be equal to 180 degrees
3. No angle can be equal to zero

Now that we know a bit about triangles, make a new file called `triangle.dip` in which we'll start designing this program

Let's start by asking the user for 3 inputs for the three angles of the triangle.

``````variable angle1 = input_integer("Enter the measure of the first angle:  ")

variable angle2 = input_integer("Enter the measure of the second angle:  ")

variable angle3 = input_integer("Enter the measure of the third angle:  ")
``````

Next, let's add all the angles and see if their sum is equal to 180. Also, let's ensure that none of the angles equals to zero.

``````variable sum = angle1 + angle2 + angle3

if sum == 180 and angle1 != 0 and angle2 != 0 and angle3 != 0 then
print("Yay, it's a valid triangle :)")
else
print("Sorry, not a valid triangle :(")
end``````

That's it! We can now run this program from the Dip interface using the run command and see the results!

## 2B. The Encryption Game: Implementing Caesar's cipher

Great! You've got so far! Now, it's time to make something interesting, and put your skills to the test, and try to replicate the Caesar cipher in Dip.

First, let's get to know a bit about Caesar's method of encrypting messages.

## The Caesar Cipher

Supposedly, Caesar (yes, that Caesar) used to “encrypt” (i.e., conceal in a reversible way) confidential messages by shifting each letter therein by some number of places. For instance, he might write A as B, B as C, C as D, …, and, wrapping around alphabetically, Z as A. And so, to say HELLO to someone, Caesar might write IFMMP. Upon receiving such messages from Caesar, recipients would have to “decrypt” them by shifting letters in the opposite direction by the same number of places.

The secrecy of this “cryptosystem” relied on only Caesar and the recipients knowing a secret, the number of places by which Caesar had shifted his letters (e.g., 1). Not particularly secure by modern standards, but, hey, if you’re perhaps the first in the world to do it, pretty secure! Unencrypted text is generally called plaintext. Encrypted text is generally called ciphertext. And the secret used is called a key.

To be clear, then, here’s how encrypting `HELLO` with a key of 1 yields `IFMMP`:

plaintext H E L L O
+ key 1 1 1 1 1
= ciphertext I F M M P

## Getting Started

Now that you know a bit about the cipher, create a new file in your favourite text editor, and name it caesar.dip

Now, let's start by creating a list of all the alpabets, so we can tell Dip what we consider valid plaintext. Note that this is a very basic version Caesar's cipher, meant to introduce you to Dip's concepts.

Please note that you should probably comment your code using the '#' sybmol. I am not doing it, because my code is accompanied by text explaining it.

`variable alphabet = "abcdefghijklmnopqrstuvwxyz "`.

This line of code is basically telling us that the alphabet consists of these letters.

Next, let's get the user's input, so we can get the plaintext they want us to encrypt.

`variable plaintext = input("Which word / phrase would you like to encrypt: ")`.

Now, let's ask for the key, `variable key = input_integer("What would you like the key to be? ")`<./p>

Note that I have used `input_integer` here in order to ensure the user behaves themselves and only gives us integers as they key.

Now, we can create an empty string called ciphertext, by doing`variable ciphertext = ""`.

After this, we can now start to loop over each letter in the planitext and substitute it with the letter that results, when offset by the key.

``````for character = 0 to length(plaintext) then
for letter = 0 to length(alphabet) then
if plaintext / character == alphabet / letter then
if plaintext / character != " " then
variable index = letter + key
variable ciphertext = ciphertext + alphabet / (index % 26)
else
variable ciphertext = ciphertext + " "
end
end
end
end
``````

This block of code first loops over each letter in the plaintext. Then it also starts looping over each letter in the alphabet, and checks letter by letter, whether the plaintext is part of the English alphabet. If it is, then it substitutes it with the corresponding letter x places ahead of it (here, x is they key provided). Then, it checks if the current character is a space, and adds it to the ciphertext. By the end, the program has added all of the letters to the ciphertext, and the encryption is done!

Please note that indentation of the code is not necessary. I have done this simply to make the program easier to read for you.

To see our encrypted output, we can then print the ciphertext`print("ciphertext: " + ciphertext)`

That's it, we've recreated Caesar's method of encryption in Dip! Try it out with a few words and phrases, by running `run("caesar.dip")` in the Dip environment (Make sure it is in the same directory as the Dip executable)!

#### Examples

``````
Dip>  run("examples/caesar.dip")
Which word / phrase would you like to encrypt: hello
What would you like the key to be? 1
ciphertext: ifmmp
``````
``````
Dip>  run("examples/caesar.dip")
Which word / phrase would you like to encrypt: this is the caesar cipher in dip
What would you like the key to be? 3
ciphertext: wklv lv wkh fdhvdu flskhu lq gls
``````

## 2C. The final stage - Creating a full blown calculator in Dip

Let's pick up from where we left off in the beginner exercises, by creating a program to add two numbers. Now, let's make a full-blown calculator, that prompts the user for their input, asking for two numbers and then adds, subtracts, multiplies or divides the two.

Open a new file, and call it calculator.dip. Now, I'll leave the implementation up to you. See this as your transition from a Dip learner to a Dip user. However, feel free to look at my implementation down below.

``````# A calculator program in Dip
# Greet the user
print("Welcome to Dip's calculator")

# Give the user four options

print("Press 2 for subtraction")
print("Press 3 for multiplication")
print("Press 4 for division")

# Ensure the code keeps on looping

while true then

# Ask user for the operation they want to perform

variable operation = input_integer("Operation: ")

# If the operation is 1, 2, 3 or 4 then proceed

if operation == 1 or operation == 2 or operation == 3 or operation == 4 then

variable num1 = input_integer("Enter first number: ")
variable num2 = input_integer("Enter second number: ")

if operation == 1 then print(num1 + num2)

if operation == 2 then print(num1 - num2)

if operation == 3 then print(num1 * num2)

if operation == 4 then print(num1 / num2)

# If the operation is not in 1, 2, 3 or 4 then prompt the user again

else
print("Input must be 1, 2, 3 or 4")
continue

end
end

``````

## Section 3: Dip programming for veterans

Don't worry if you're a programming veteran. Test how Dip and Python can work together!

## 3A. The Factorial Function

Open a new file, and write the following code to implement the factorial function! (You should probably try it out yourself before looking at this):

``````function factorial(term)variable fact = 1
if term >= 1 then
for i = 1 to term + 1 then
variable fact = fact * i
end
end
print(fact)
end
``````

That's the factorial function in Dip. Next, you should try out either the fibonacci function, or learn to use Dip with Python. If you want something more challenging, I have also made a list of recommendations!

## 3B. The Fibonacci Function

``````function fib(nterms)variable n1 = 0
variable n2 = 1
variable count = 0

if nterms <= 0 then print("Input a positive integer")
if nterms == 1 then
print("Fibonnaci sequence upto" + " 1" + ":")
print(n1)
else
print("Fibonacci sequence:")
while count < nterms then
print(n1)
variable nth = n1 + n2
variable n1 = n2
variable n2 = nth
variable count = count + 1
end
end
end
``````

An old classic, the fibonacci function. However, keep in mind that there is more than one way to do this. Try out your own implementation of fibonacci (perhaps using recursion), and post it on the forum!

## 3C. Python and Dip: The Dream Team

To start, Python and Dip are clearly not the dream team, that was more of a joke. Dip was written in Python, and that's why it can interact with it, and maybe be useful in some small cases.

I wanted to show you an example of such a case. In this section, you'll create a working GUI application using Dip and Python!

## A simple GUI app to print your name to the interface.

We'll be making both the apps using Python's Tkinter library, since Dip does not have support for GUI (currently).

Let's start with a simple Dip statement asking the user for their name `variable name = input("What's your name? ")`

Now comes the tricky part, connecting Dip with Python. The way Dip aims to connect Dip and Python is by letting Dip communicate with Python. This way we can run python programs through the Dip interface, and can try to do interesting things with Python and Dip.

So, let's create a variable called python_code. This will contain all of our Python code. We can start by creating a GUI window in Python, and initialising it.

`variable python_code = "import tkinter as tk;" + "root = tk.Tk();" + "label = tk.Label(root, text='Hello, " + name + "').pack()"`

The first line of the code, `import tkinter as tk` is used to import the tkinter library in Python, which is a GUI library used to create graphical programs. The second line, `root = tk.Tk()` creates an empty GUI window in Python. This is created through the Tkinter library, which is an external GUI library for Python. Read more about libraries and tkinter using the hyperlinks. The third line, and perhaps the most important line of this program creates a new text box in the GUI window, which Tkinter calls a label. The label function takes 2 arguments, where to put the text box, and what text to put in it. For the first argument, we write `root`, because that is the name of our GUI window. For the second argument, we plug-in our variable name, so that we can make the user's name go in place of the text box's text.

To show our results, we can then write `evaluate(python_code)` to evaluate our Python code

Upon running this program from the Dip interface, and entering our name, this is the result that awaits us:

Great! You've created your first GUI program with Dip and Python. Now that we've learnt how to create GUI application with Python and Dip, you can take this in any direction, creating all sorts of GUI programs with Python and Dip.

## The Final Chapter

And that's it! You've now completed your Dip training! This has paved the way for you to create some truly fun applications with Dip!