Skip to content

Commit

Permalink
expand first 2 lessons and add 3rd
Browse files Browse the repository at this point in the history
  • Loading branch information
JalonSolov committed Feb 6, 2024
1 parent 8dadb61 commit 7eb172c
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 2 deletions.
9 changes: 8 additions & 1 deletion lessons/1.what-is-v.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,11 @@ V is a [general purpose programming language](https://en.wikipedia.org/wiki/Gene
This means it can be used to create any type of program you desire, rather than being focused on
any one particular type of program.

You can install it by [following the instructions here](https://github.com/vlang/v/blob/master/README.md#installing-v-from-source).
V is also a statically typed, compiled langauge. Statically typed means that
all variable types are known at compile time, and cannot change type at runtime.

In other words, if you create a string variable, it will always be a string
variable, and cannot be changed to an integer without changing the source and
recompiling your program.

You can install V by [following the instructions here](https://github.com/vlang/v/blob/master/README.md#installing-v-from-source).
39 changes: 38 additions & 1 deletion lessons/2.writing-our-first-program.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,45 @@ Writing Our First Program

In this lesson, we'll examine one of the simplest programs in V.

Yes it is the iconic [Hello World](https://en.wikipedia.org/wiki/%22Hello%2C_World!%22_program)
that is often the first program someone would write in language they are learning.

```v
fn main() {
println('hello world')
println('Hello, World!')
}
```

In V, functions are declared with the `fn` keyword, followed by the name of the
function (in this case `main`), the parameters to the function, if any (in this
case there aren't any), then an opening curly brace, `{`, on the same line.

V also has 2 ways to output text - one that only prints what is given to it,
`print`, and one that automatically adds a linefeed to the end of whatever it
is given, `println`.

Using
```v
println('Hello, World!)`
```
above is the equivalent to
```c
printf("Hello, World!\n");
```
in C.
Finally, the closing curly brace, `}`, to end the function.
One thing to clarify - V doesn't care if you use single quotes (`'`) or double
quotes (`"`) around a string of characters, the opening and closing quotes just
have to match. This means you can't use `'a string"` or `"a string'` but either
`'a string'` or `"a string"` are fine.
This is unusual amongst programming programming languages, where you must only
use one or the other (in C, for example, only double quotes are valid around
strings), or if both types are supported, they have different meanings. In V,
they are both equally valid, and mean exactly the same thing.
Also note that both `print` and `println` are builtin functions in V - nothing extra
is needed to use them, other than typing their names and giving them something
to print.
67 changes: 67 additions & 0 deletions lessons/3.next-level-program.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
Next Level Program
==================

Now, for a slightly more complex version of the `Hello World` program. This is
a copy of one of the examples on the [V Home Page](https://vlang.io).
```v
fn main() {
areas := ['game', 'web', 'tools', 'science', 'systems',
'embedded', 'drivers', 'GUI', 'mobile']
for area in areas {
println('Hello, ${area} developers!')
}
}
```

In this version of the program, several more ideas are introduced.

V variables are declared by giving the name, followed by `:=` then a value to
set both the initial value of the variable, as well as it's type.

In the example,
```v
areas := ['game', 'web', 'tools', 'science', 'systems',
'embedded', 'drivers', 'GUI', 'mobile']
```
`areas` is declared as the name of a variable, and is assigned the type `[]string`
(meaning an array of strings), with the initial values of the array set to the
strings inside the `[]` characters.

Unlike other languages, V does not allow you to create a variable without
assigning a value, as the value sets the variable type.

The 2nd new thing is the [`for`](https://github.com/vlang/v/blob/master/doc/docs.md#for-loop)
loop, which is the only looping statement in V.

For this variation, it is used to loop over the values in the `areas` array, so
that each one can be used in the `println` statement.
```v-ignore
for area in areas {
```
This means "for every string in the `areas` string array, assign the value to a
variable named `area`, then execute the statements inside the `{}`.

The `println` statement is also different in this example:
```v
println('Hello, ${area} developers!')
```
Here, V uses a new concept called "string interpolation". This basically means
that V will take whatever is inside `${}` in the string, treat it as V code,
and use the value as if it was part of the string when it is printed.

For this specific case, it means "use the value of the variable named `area`",
and the result is a string that changes every time through the loop, as the
value of `area` changes.

The output from running this program is
```
Hello, game developers!
Hello, web developers!
Hello, tools developers!
Hello, science developers!
Hello, systems developers!
Hello, embedded developers!
Hello, drivers developers!
Hello, GUI developers!
Hello, mobile developers!
```

0 comments on commit 7eb172c

Please sign in to comment.