You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/tutorials/branching.md
+30-14
Original file line number
Diff line number
Diff line change
@@ -43,7 +43,7 @@ Now let's put it together:
43
43
44
44
```xpp
45
45
var myAge 20
46
-
if (myAge == 20) "prt 'I am 20 years old'" "prt 'I am not 20 years old'"
46
+
if (myAge == 20) { prt "I am 20 years old" } { prt "I am not 20 years old" }
47
47
```
48
48
49
49
When you run the code, you should see it outputs `"I am 20 years old"`. Try changing the value of `myAge` and see what happens.
@@ -52,38 +52,54 @@ The `else` branch is optional. If you only want to check if the expression is tr
52
52
53
53
```xpp
54
54
var myInteger 5
55
-
if (myInteger == 5) "prt 'My integer is 5'"
55
+
if (myInteger == 5) { prt "My integer is 5" }
56
56
```
57
57
58
58
With that knowledge, you can now output a special text if the user name matches yours:
59
59
60
60
```xpp
61
61
:: main.xpp
62
62
read "What is your name? " ?name
63
-
upr name
64
63
read "What is your age? " ?age
65
-
int age
66
-
sub 2023 age ?birthYear
67
-
if (name == "BOB") "prt 'Welcome, Bob!'"
68
-
prt "Your name is $(name) and you were born in $(birthYear)"
64
+
65
+
:: Keep a copy of their original string
66
+
var ogName name
67
+
68
+
:: Calculate birth year by using the current year
69
+
int age :: Ensure it's an integer
70
+
sub 2024 age ?birthYear
71
+
72
+
:: Convert the name to lowercase (so we can check it)
73
+
if ((lwr name) == "bob") { prt "Welcome, Bob!" }
74
+
75
+
:: Show them their info
76
+
prt "Your name is $(ogName) and you were born in $(birthYear)."
69
77
```
70
78
71
79
There are many more comparators, such as the `greater than` (`>`) or `not equal` (`!=`). They work the same way as the `equal to` comparator.
72
80
73
81
Now's your turn. Check if the user's age is equal to or above 16 and output `"You can also drive a car"` after you output their name and their birth year if it is true.
74
82
75
-
Did you get something like this:
83
+
Did you get something like this?
76
84
77
85
```xpp
78
86
:: main.xpp
79
87
read "What is your name? " ?name
80
-
upr name
81
88
read "What is your age? " ?age
82
-
int age
83
-
sub 2023 age ?birthYear
84
-
if (name == "BOB") "prt 'Welcome, Bob!'"
85
-
prt "Your name is $(name) and you were born in $(birthYear)"
86
-
if (age >= 16) "prt 'You can also drive a car'"
89
+
90
+
:: Keep a copy of their original string
91
+
var ogName name
92
+
93
+
:: Calculate birth year by using the current year
94
+
int age :: Ensure it's an integer
95
+
sub 2024 age ?birthYear
96
+
97
+
:: Convert the name to lowercase (so we can check it)
98
+
if ((lwr name) == "bob") { prt "Welcome, Bob!" }
99
+
100
+
:: Show them their info
101
+
prt "Your name is $(ogName) and you were born in $(birthYear)."
102
+
if (age >= 16) { prt "You can also drive a car." }
87
103
```
88
104
89
105
In the next lesson, you will be learning how to make a calculator using x++.
Copy file name to clipboardExpand all lines: docs/tutorials/calculator.md
+12-12
Original file line number
Diff line number
Diff line change
@@ -24,7 +24,7 @@ From the last three lessons, you learned how to print values into the terminal,
24
24
25
25
Let's introduce the program and get some inputs from the user using the `prt` and `read` operators:
26
26
27
-
```xt
27
+
```xpp
28
28
:: main.xpp
29
29
prt "Welcome to the x++ calculator!"
30
30
prt "-----"
@@ -74,7 +74,7 @@ upr o
74
74
75
75
Now we can use the `if` operator to check what operator the user selected and act accordingly. Currently, there are four types of mathematical operators: `add`, `sub`, `mul`, and `div`. Let's use them:
76
76
77
-
```xt
77
+
```xpp
78
78
:: main.xpp
79
79
prt "Welcome to the x++ calculator!"
80
80
prt "-----"
@@ -91,15 +91,15 @@ prt "-----"
91
91
int a
92
92
int b
93
93
upr o
94
-
if (o == "A") "add a b ?c"
95
-
if (o == "S") "sub a b ?c"
96
-
if (o == "M") "mul a b ?c"
97
-
if (o == "D") "div a b ?c"
94
+
if (o == "A") { add a b ?c } \
95
+
(o == "S") { sub a b ?c } \
96
+
(o == "M") { mul a b ?c } \
97
+
(o == "D") { div a b ?c }
98
98
```
99
99
100
100
Since you defined `c` as the answer to the equation, you can simply output it to the terminal. Using string interpolation, you can also use variables within your strings:
101
101
102
-
```xt
102
+
```xpp
103
103
:: main.xpp
104
104
prt "Welcome to the x++ calculator!"
105
105
prt "-----"
@@ -116,11 +116,11 @@ prt "-----"
116
116
int a
117
117
int b
118
118
upr o
119
-
if (o == "A") "add a b ?c"
120
-
if (o == "S") "sub a b ?c"
121
-
if (o == "M") "mul a b ?c"
122
-
if (o == "D") "div a b ?c"
123
-
prt "The answer to that equation is $(c)"
119
+
if (o == "A") { add a b ?c } \
120
+
(o == "S") { sub a b ?c } \
121
+
(o == "M") { mul a b ?c } \
122
+
(o == "D") { div a b ?c }
123
+
prt "The answer to that equation is $(c)."
124
124
```
125
125
126
126
Tada! You got a working calculator. How cool is that?
Copy file name to clipboardExpand all lines: docs/tutorials/hello-world.md
+4-4
Original file line number
Diff line number
Diff line change
@@ -60,7 +60,7 @@ Let's put that in your code!
60
60
:: main.xpp
61
61
var name "Bob"
62
62
var age 20
63
-
prt "My name is Bob and I am 20 years old"
63
+
prt "My name is Bob and I am 20 years old."
64
64
```
65
65
66
66
Your can put your variables into your string using `string interpolation`. String interpolation is the process of inserting another statement within a string. This is usually done so by wrapping them in `$()`.
@@ -69,8 +69,8 @@ The statement you can wrap inside of `$()` can be any valid x++ syntax, however
69
69
70
70
```xpp
71
71
var x 5
72
-
prt "$(x) should be 5"
73
-
:: 5 should be 5
72
+
prt "$(x) should be 5."
73
+
:: 5 should be 5.
74
74
```
75
75
76
76
Let's try it!
@@ -79,7 +79,7 @@ Let's try it!
79
79
:: main.xpp
80
80
var name "Bob"
81
81
var age 20
82
-
prt "My name is $(name) and I am $(age) years old"
82
+
prt "My name is $(name) and I am $(age) years old."
Copy file name to clipboardExpand all lines: docs/tutorials/user-input.md
+6-6
Original file line number
Diff line number
Diff line change
@@ -28,7 +28,7 @@ So far, you should've gotten this in your `main.xpp` file:
28
28
:: main.xpp
29
29
var name "Bob"
30
30
var age 20
31
-
prt "My name is $(name) and I am $(age) years old"
31
+
prt "My name is $(name) and I am $(age) years old."
32
32
```
33
33
34
34
What if you want to ask what the user's name is instead of yours? You can use the `read` operator to get user input from the terminal. The `read` operator takes in two arguments, the `prompt` and the `output`. The prompt is what the user will see when you get a user input. You can think of it as asking a question. The output is the answer from the user input.
@@ -41,11 +41,11 @@ read "What is your favorite color? " ?favoriteColor
41
41
42
42
You can replace your `var` operators and use the `read` operators instead:
43
43
44
-
```xt
44
+
```xpp
45
45
:: main.xpp
46
46
read "What is your name? " ?name
47
47
read "What is your age? " ?age
48
-
prt "Your name is $(name) and you are $(age) years old"
48
+
prt "Your name is $(name) and you are $(age) years old."
49
49
```
50
50
51
51
You can also make the name more standout from the rest of the string by making it all capitalized. You can uppercase all the letters in a string by using the `upr` operator:
@@ -63,7 +63,7 @@ Let's try it:
63
63
read "What is your name? " ?name
64
64
upr name
65
65
read "What is your age? " ?age
66
-
prt "Your name is $(name) and you are $(age) years old"
66
+
prt "Your name is $(name) and you are $(age) years old."
67
67
```
68
68
69
69
You can also use mathematical operators to calculate the user's birth year. By subtracting the user's age from the current year, you get their birth year. You can use the `sub` operator for this purpose:
@@ -89,8 +89,8 @@ read "What is your name? " ?name
89
89
upr name
90
90
read "What is your age? " ?age
91
91
int age
92
-
sub 2023 age ?birthYear
93
-
prt "Your name is $(name) and you were born in $(birthYear)"
92
+
sub 2024 age ?birthYear
93
+
prt "Your name is $(name) and you were born in $(birthYear)."
94
94
```
95
95
96
96
Now it will ask the user their name and age and output their name and birth year. Incredible isn't it?
0 commit comments