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:47] – [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 190: Line 189:
 </code> </code>
  
-====== Arithmetic ======+===== Practice ===== 
 +  - Write a program that prints your age. 
 +  - Write a program that prints today's date.
  
-<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 ''x''
 + 
 +In the following example, perform some calculations: 
 +<code cpp example9.cpp>
 #include "tutorial.h" #include "tutorial.h"
  
-start() +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 217: 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 245: 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 291: 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 305: Line 398:
         print(number);         print(number);
         print("!");         print("!");
-} 
 </code> </code>
  
  
programming_tutorial.1349642850.txt.gz · Last modified: 2023/08/18 18:15 (external edit)