User Tools

Site Tools


programming_tutorial

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
programming_tutorial [2012/10/07 20:21] – [Variables] javapimpprogramming_tutorial [2023/08/18 18:15] (current) – external edit 127.0.0.1
Line 50: Line 50:
  
 You can call ''print'' more than once to print multiple items. For example: You can call ''print'' more than once to print multiple items. For example:
-<code cpp example1.cpp>+<code cpp example2.cpp>
 #include "tutorial.h" #include "tutorial.h"
  
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 "tutorial.h" #include "tutorial.h"
  
Line 92: Line 92:
  
 In this next example, we misspell the ''print'' command. In this next example, we misspell the ''print'' command.
-<code cpp example3.cpp>+<code cpp example4.cpp>
 #include "tutorial.h" #include "tutorial.h"
  
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 "tutorial.h" #include "tutorial.h"
  
-start() +start
-{+
         // This prints a message to the screen         // This prints a message to the screen
         print("Hello, world!");         print("Hello, world!");
-}+end
 </code> </code>
  
Line 131: Line 130:
  
 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: 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 example5.cpp>+<code cpp example6.cpp>
 #include "tutorial.h" #include "tutorial.h"
  
-start() +start
-{+
         integer i = 5;         integer i = 5;
  
         // prints the value of 'i' to the screen         // prints the value of 'i' to the screen
         print(i);         print(i);
-}+end
 </code> </code>
  
-This example creates a variable named ''i''. A variable is defined by first giving it a type, in this case it is an ''integer''. (An ''integer'' is a positive or negative whole number or zero. e.g ''..., -2, -1, 0, 1, 2, ...)+This example creates a variable named ''i''. A variable is defined by first giving it a type, in this case it is an ''integer''. (An ''integer'' is a positive or negative whole number or zero. e.g ''..., -2, -1, 0, 1, 2, ...'')
  
 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 ''5''. 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 ''5''.
Line 154: Line 152:
 </code> </code>
  
-<code cpp example6.cpp>+Changing the value in a variable is done simply by supplying the new value. In the following example, we change the value from ''5'' to ''6'' and print the result. 
 + 
 +<code cpp example7.cpp>
 #include "tutorial.h" #include "tutorial.h"
  
-start() +start 
-{+        integer i = 5; 
 +        print("i = ")
 +        print(i); // prints the value of 'i' to the screen 
 +        i = 6; 
 +        print(", now i = "); 
 +        print(i); 
 +end 
 +</code> 
 + 
 +The output of this should look like the following: 
 +<code> 
 +i = 5, now i = 6 
 +</code> 
 + 
 +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 ''i'' and put it into the variable ''j'' and then print out ''j'': 
 + 
 +<code cpp example8.cpp> 
 +#include "tutorial.h" 
 + 
 +start 
 +        integer i = 5; 
 +        integer j = i; 
 +        print(j); 
 +end 
 +</code> 
 + 
 +Output of this program should look like this: 
 +<code> 
 +
 +</code> 
 + 
 +===== Practice ===== 
 +  - Write a program that prints your age. 
 +  - Write a program that prints today's date. 
 + 
 +====== 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 ''x''
 + 
 +In the following example, perform some calculations: 
 +<code cpp example9.cpp> 
 +#include "tutorial.h" 
 + 
 +start 
 +        integer i = 7 + 1; 
 +        print("i = "); 
 +        print(i); 
 +         
 +        integer j = 8 - 2; 
 +        print(", j = "); 
 +        print(j); 
 +         
 +        integer k = 6 * 5; 
 +        print(", k = "); 
 +        print(k); 
 +         
 +        integer l = 30 / 5; 
 +        print(", l = "); 
 +        print(l); 
 +end 
 +</code> 
 + 
 +The output from this program looks like: 
 +<code> 
 +i = 8, j = 6, k = 30, l = 6 
 +</code> 
 + 
 +We can also perform calculations using variables as terms to the calculation: 
 + 
 +<code cpp example10.cpp> 
 +#include "tutorial.h" 
 + 
 +start 
 +        integer i = 7 + 1; 
 +        print("i = "); 
 +        print(i); 
 +         
 +        integer j = i - 2; 
 +        print(", j = "); 
 +        print(j); 
 +         
 +        integer k = j * 5; 
 +        print(", k = "); 
 +        print(k); 
 +         
 +        integer l = k / 5; 
 +        print(", l = "); 
 +        print(l); 
 +end 
 +</code> 
 + 
 +This program's looks like before only we used variables in the calculations: 
 +<code> 
 +i = 8, j = 6, k = 30, l = 6 
 +</code> 
 + 
 +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, the result of the calculation then replaces the old value that was stored in the variable: 
 + 
 +<code cpp example11.cpp> 
 +#include "tutorial.h" 
 + 
 +start 
 +        integer i = 1; 
 +        print(i); 
 +        i = i + 1; // add 1 to 'i' 
 +        print(", "); 
 +        print(i); 
 +end 
 +</code> 
 + 
 +The output from this program looks like this: 
 +<code> 
 +1, 2 
 +</code> 
 + 
 +In the next example, we'll write a program that counts from 1 to 10. Notice that this program uses only one variable ''i''
 +<code cpp example12.cpp> 
 +#include "tutorial.h" 
 + 
 +start
         integer i = 1;         integer i = 1;
         print(i);         print(i);
Line 179: Line 298:
         i = i + 1;         i = i + 1;
         print(i);         print(i);
-}+end
 </code> </code>
  
-<code cpp example7.cpp>+The output from this program looks like this: 
 +<code> 
 +12345678910 
 +</code> 
 + 
 +===== 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 ''println'' to do this. ''println'' works just like the previous print command except that it forces the next print to start on the next line. 
 + 
 +<code cpp example13.cpp>
 #include "tutorial.h" #include "tutorial.h"
  
-start() +start
-{+
         integer i = 1;         integer i = 1;
         println(i);         println(i);
Line 207: Line 334:
         i = i + 1;         i = i + 1;
         println(i);         println(i);
-}+end
 </code> </code>
  
-<code cpp example8.cpp+The output of this code now looks like this: 
-#include "tutorial.h" +<code> 
- +
-start() +2 
-+3 
-        integer i = 1; +4 
-        println(i); +5 
-        i = i * 10; // multiply 'i' by 10 +6 
-        println(i); +7 
-        i = i * 10; +8 
-        println(i); +9 
-        i = i * 10; +10
-        println(i); +
-        i = i * 10; +
-        println(i); +
-        i = i * 10+
-        println(i); +
-        i = i * 10; +
-        println(i); +
-}+
 </code> </code>
  
-<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,000,000. 
 +  - Write a program that calculates how many days are in two weeks. 
 +    - Use a variable called ''daysInAWeek''
 +    - Use a variable called ''numberOfWeeks''
 +    - Use a variable called ''totalDays''
 +    - Print the value of ''totalDays''
 + 
 + 
 +====== Strings ====== 
 + 
 +<code cpp example14.cpp>
 #include "tutorial.h" #include "tutorial.h"
  
-start() +start
-{+
         string s = "Hello, world!";         string s = "Hello, world!";
         print(s);         print(s);
-}+end
 </code> </code>
  
-<code cpp example10.cpp>+====== Getting Input ====== 
 + 
 +<code cpp example15.cpp>
 #include "tutorial.h" #include "tutorial.h"
  
-start() +start
-{+
         string name;         string name;
         print("Hello, what is your name? ");         print("Hello, what is your name? ");
Line 253: Line 385:
         print(name);         print(name);
         print("!");         print("!");
-}+end
 </code> </code>
  
-<code cpp example11.cpp>+<code cpp example16.cpp>
 #include "tutorial.h" #include "tutorial.h"
  
-start() +start
-{+
         integer number;         integer number;
         print("Enter a number: ");         print("Enter a number: ");
Line 267: Line 398:
         print(number);         print(number);
         print("!");         print("!");
-} 
 </code> </code>
  
  
programming_tutorial.1349641317.txt.gz · Last modified: 2023/08/18 18:15 (external edit)