programming_tutorial
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
programming_tutorial [2012/10/07 19:51] – [Comments] javapimp | programming_tutorial [2023/08/18 18:15] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 50: | Line 50: | ||
You can call '' | You can call '' | ||
- | <code cpp example1.cpp> | + | <code cpp example2.cpp> |
#include " | #include " | ||
Line 70: | Line 70: | ||
Take a look at the following example. In this example we forgot to include the closing quotation mark on our output sentence. | Take a look at the following example. In this example we forgot to include the closing quotation mark on our output sentence. | ||
- | <code cpp example2.cpp> | + | <code cpp example3.cpp> |
#include " | #include " | ||
Line 92: | Line 92: | ||
In this next example, we misspell the '' | In this next example, we misspell the '' | ||
- | <code cpp example3.cpp> | + | <code cpp example4.cpp> |
#include " | #include " | ||
Line 115: | Line 115: | ||
Comments begin with the two characters %%//%%. Everything following those characters to the end of the line are considered a comment. | Comments begin with the two characters %%//%%. Everything following those characters to the end of the line are considered a comment. | ||
- | <code cpp example4.cpp> | + | <code cpp example5.cpp> |
#include " | #include " | ||
- | start() | + | start |
- | { | + | |
// This prints a message to the screen | // This prints a message to the screen | ||
print(" | print(" | ||
- | } | + | end |
</ | </ | ||
====== Variables ====== | ====== Variables ====== | ||
+ | Variables are locations in memory that are used to remember things. You can think of them as a box on a shelf. Each box can only hold one thing at a time. When putting a new thing into the box, the old thing is removed and either thrown away or put into a different box. | ||
- | <code cpp example5.cpp> | + | Each variable has a name. You can think of the name as the label on the box. The name just specifies which box you want to work with. |
+ | |||
+ | When using a thing stored in a variable in your program, you refer to it using the variable name. Have a look at this example: | ||
+ | <code cpp example6.cpp> | ||
#include " | #include " | ||
- | start() | + | start |
- | { | + | |
integer i = 5; | integer i = 5; | ||
// prints the value of ' | // prints the value of ' | ||
print(i); | print(i); | ||
- | } | + | end |
</ | </ | ||
- | <code cpp example6.cpp> | + | This example creates a variable named '' |
+ | |||
+ | Following the type is the variable name itself. The variable can be named anything you want. After the name you can optionally give it a value or leave it empty. In this example we give it the value '' | ||
+ | |||
+ | We want to print the value we have stored in our variable. To do that we simply give it to the '' | ||
+ | |||
+ | The output from running this example should look like this: | ||
+ | < | ||
+ | 5 | ||
+ | </ | ||
+ | |||
+ | Changing the value in a variable is done simply by supplying the new value. In the following example, we change the value from '' | ||
+ | |||
+ | <code cpp example7.cpp> | ||
#include " | #include " | ||
- | start() | + | start |
- | { | + | integer i = 5; |
+ | print("i = "); | ||
+ | print(i); // prints the value of ' | ||
+ | i = 6; | ||
+ | print(", | ||
+ | print(i); | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | The output of this should look like the following: | ||
+ | < | ||
+ | i = 5, now i = 6 | ||
+ | </ | ||
+ | |||
+ | Variables can be copied from one to another. We can take the thing stored in one box, and store a copy in another. For example, here we make a copy of what is in '' | ||
+ | |||
+ | <code cpp example8.cpp> | ||
+ | #include " | ||
+ | |||
+ | start | ||
+ | integer i = 5; | ||
+ | integer j = i; | ||
+ | print(j); | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | Output of this program should look like this: | ||
+ | < | ||
+ | 5 | ||
+ | </ | ||
+ | |||
+ | ===== Practice ===== | ||
+ | - Write a program that prints your age. | ||
+ | - Write a program that prints today' | ||
+ | |||
+ | ====== Arithmetic Operations ====== | ||
+ | |||
+ | Arithmetic operations consist of the usual add (+), subtract (-), multiply (*) and divide (/). Notice that the multiplication operator is an astrisk (*) symbol and not an '' | ||
+ | |||
+ | In the following example, perform some calculations: | ||
+ | <code cpp example9.cpp> | ||
+ | #include " | ||
+ | |||
+ | start | ||
+ | integer i = 7 + 1; | ||
+ | print(" | ||
+ | print(i); | ||
+ | |||
+ | integer j = 8 - 2; | ||
+ | print(", | ||
+ | print(j); | ||
+ | |||
+ | integer k = 6 * 5; | ||
+ | print(", | ||
+ | print(k); | ||
+ | |||
+ | integer l = 30 / 5; | ||
+ | print(", | ||
+ | print(l); | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | The output from this program looks like: | ||
+ | < | ||
+ | i = 8, j = 6, k = 30, l = 6 | ||
+ | </ | ||
+ | |||
+ | We can also perform calculations using variables as terms to the calculation: | ||
+ | |||
+ | <code cpp example10.cpp> | ||
+ | #include " | ||
+ | |||
+ | start | ||
+ | integer i = 7 + 1; | ||
+ | print(" | ||
+ | print(i); | ||
+ | |||
+ | integer j = i - 2; | ||
+ | print(", | ||
+ | print(j); | ||
+ | |||
+ | integer k = j * 5; | ||
+ | print(", | ||
+ | print(k); | ||
+ | |||
+ | integer l = k / 5; | ||
+ | print(", | ||
+ | print(l); | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | This program' | ||
+ | < | ||
+ | i = 8, j = 6, k = 30, l = 6 | ||
+ | </ | ||
+ | |||
+ | A variable can also be used in a calculation that updates itself. In this case, the value in the variable is used in the calculation, | ||
+ | |||
+ | <code cpp example11.cpp> | ||
+ | #include " | ||
+ | |||
+ | start | ||
+ | integer i = 1; | ||
+ | print(i); | ||
+ | i = i + 1; // add 1 to ' | ||
+ | print(", | ||
+ | print(i); | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | The output from this program looks like this: | ||
+ | < | ||
+ | 1, 2 | ||
+ | </ | ||
+ | |||
+ | In the next example, we'll write a program that counts from 1 to 10. Notice that this program uses only one variable '' | ||
+ | <code cpp example12.cpp> | ||
+ | #include " | ||
+ | |||
+ | start | ||
integer i = 1; | integer i = 1; | ||
print(i); | print(i); | ||
Line 164: | Line 298: | ||
i = i + 1; | i = i + 1; | ||
print(i); | print(i); | ||
- | } | + | end |
</ | </ | ||
- | <code cpp example7.cpp> | + | The output from this program looks like this: |
+ | < | ||
+ | 12345678910 | ||
+ | </ | ||
+ | |||
+ | ===== println ===== | ||
+ | |||
+ | Notice that all the numbers in the last example are up against each other. That's because we didn't print any formatting like we did before. What if we wanted to print each number on its own line? We can use a new command '' | ||
+ | |||
+ | <code cpp example13.cpp> | ||
#include " | #include " | ||
- | start() | + | start |
- | { | + | |
integer i = 1; | integer i = 1; | ||
println(i); | println(i); | ||
Line 192: | Line 334: | ||
i = i + 1; | i = i + 1; | ||
println(i); | println(i); | ||
- | } | + | end |
</ | </ | ||
- | < | + | The output of this code now looks like this: |
- | #include " | + | < |
- | + | 1 | |
- | start() | + | 2 |
- | { | + | 3 |
- | integer i = 1; | + | 4 |
- | | + | 5 |
- | i = i * 10; // multiply ' | + | 6 |
- | | + | 7 |
- | i = i * 10; | + | 8 |
- | | + | 9 |
- | i = i * 10; | + | 10 |
- | | + | |
- | i = i * 10; | + | |
- | | + | |
- | i = i * 10; | + | |
- | println(i); | + | |
- | i = i * 10; | + | |
- | println(i); | + | |
- | } | + | |
</ | </ | ||
- | <code cpp example9.cpp> | + | ===== Practice ===== |
+ | - Write a program that prints the powers of 10. | ||
+ | - Start with a variable set to 1. | ||
+ | - End when the variable reaches 1, | ||
+ | - Write a program that calculates how many days are in two weeks. | ||
+ | - Use a variable called '' | ||
+ | - Use a variable called '' | ||
+ | - Use a variable called '' | ||
+ | - Print the value of '' | ||
+ | |||
+ | |||
+ | ====== Strings ====== | ||
+ | |||
+ | <code cpp example14.cpp> | ||
#include " | #include " | ||
- | start() | + | start |
- | { | + | |
string s = " | string s = " | ||
print(s); | print(s); | ||
- | } | + | end |
</ | </ | ||
- | <code cpp example10.cpp> | + | ====== Getting Input ====== |
+ | |||
+ | <code cpp example15.cpp> | ||
#include " | #include " | ||
- | start() | + | start |
- | { | + | |
string name; | string name; | ||
print(" | print(" | ||
Line 238: | Line 385: | ||
print(name); | print(name); | ||
print(" | print(" | ||
- | } | + | end |
</ | </ | ||
- | <code cpp example11.cpp> | + | <code cpp example16.cpp> |
#include " | #include " | ||
- | start() | + | start |
- | { | + | |
integer number; | integer number; | ||
print(" | print(" | ||
Line 252: | Line 398: | ||
print(number); | print(number); | ||
print(" | print(" | ||
- | } | ||
</ | </ | ||
programming_tutorial.1349639505.txt.gz · Last modified: 2023/08/18 18:15 (external edit)