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 20:47] – [Variables] 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 |
</ | </ | ||
Line 190: | Line 189: | ||
</ | </ | ||
- | ====== Arithmetic ====== | + | ===== Practice |
+ | - Write a program that prints your age. | ||
+ | - Write a program that prints today' | ||
- | <code cpp example6.cpp> | + | ====== 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 " | #include " | ||
- | start() | + | start |
- | { | + | integer i = 7 + 1; |
+ | print("i = "); | ||
+ | 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 217: | 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 245: | 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 291: | 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 305: | Line 398: | ||
print(number); | print(number); | ||
print(" | print(" | ||
- | } | ||
</ | </ | ||
programming_tutorial.1349642850.txt.gz · Last modified: 2023/08/18 18:15 (external edit)