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/02 22:18] – javapimp | programming_tutorial [2023/08/18 18:15] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 2: | Line 2: | ||
This tutorial is a basic introduction to programming. It is written with the assumption that the student has never written a computer program before beginning with the basic program structure and the ever popular " | This tutorial is a basic introduction to programming. It is written with the assumption that the student has never written a computer program before beginning with the basic program structure and the ever popular " | ||
- | The language is based on the C++ programming language with initially | + | The language is based on the C++ programming language with some simplifications. By the end of the tutorial the student should have a good understanding |
====== Program Structure ====== | ====== Program Structure ====== | ||
+ | The example programs in this tutorial use a special header file tutorial.h that is intended to keep the examples extremely simple. This file can be [[http:// | ||
+ | < | ||
+ | [include section] | ||
+ | |||
+ | [declarations] | ||
+ | |||
+ | [functions] | ||
+ | |||
+ | [start] | ||
+ | </ | ||
+ | |||
+ | The programs begin with the '' | ||
+ | |||
+ | Following the include section are the '' | ||
+ | |||
+ | Finally, every program needs a '' | ||
+ | |||
+ | Let's take a look at our first program. | ||
===== Hello, World! ===== | ===== Hello, World! ===== | ||
+ | |||
<code cpp example1.cpp> | <code cpp example1.cpp> | ||
#include " | #include " | ||
- | void t_main() | + | start |
- | { | + | |
print(" | print(" | ||
- | } | + | end |
</ | </ | ||
+ | |||
+ | This program begins with the '' | ||
+ | |||
+ | This program does not have any '' | ||
+ | |||
+ | Our '' | ||
+ | |||
+ | Finally, we have the '' | ||
+ | |||
+ | //INSERT IDE INSTRUCTIONS HERE// | ||
+ | |||
+ | Once you run this program you should see the following output on the screen: | ||
+ | < | ||
+ | Hello, world! | ||
+ | </ | ||
+ | |||
+ | You can call '' | ||
+ | <code cpp example2.cpp> | ||
+ | #include " | ||
+ | |||
+ | start | ||
+ | print(" | ||
+ | print(" | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | This example produces the same output as the previous example. The second print command begins its output where the previous left off. | ||
+ | |||
+ | ==== Practice ==== | ||
+ | - Write a program that prints your name. | ||
+ | - Write a program that prints your family' | ||
+ | - Write a program to print a verse from your favorite poem or a paragraph from your favorite book. | ||
+ | |||
+ | ===== Syntax Errors ===== | ||
+ | While you are programming it is inevitable that you will type something wrong. When you try to run your program and there is a typo the compiler will complain with some pretty cryptic error messages. | ||
+ | |||
+ | Take a look at the following example. In this example we forgot to include the closing quotation mark on our output sentence. | ||
+ | <code cpp example3.cpp> | ||
+ | #include " | ||
+ | |||
+ | start | ||
+ | print(" | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | This produces the following error message: | ||
+ | < | ||
+ | example2.cpp: | ||
+ | example2.cpp: | ||
+ | example2.cpp: | ||
+ | example2.cpp: | ||
+ | example2.cpp: | ||
+ | </ | ||
+ | |||
+ | Pretty cryptic huh? Don't be daunted by these messages. Although they look intimidating they do provide some clues to what might be wrong. In our case the line '' | ||
+ | |||
+ | Note also, there are other errors following that initial error. These errors are a result of the compiler getting confused and are not true errors. Once you fix the first error the others will go away. | ||
+ | |||
+ | In this next example, we misspell the '' | ||
+ | <code cpp example4.cpp> | ||
+ | #include " | ||
+ | |||
+ | start | ||
+ | prnt(" | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | This produces the following error message: | ||
+ | < | ||
+ | example3.cpp: | ||
+ | example3.cpp: | ||
+ | </ | ||
+ | |||
+ | Again cryptic but it tells us that on line 4 it doesn' | ||
+ | |||
+ | ==== Practice ==== | ||
+ | Try to make other errors in the code and see what kinds of error messages you get. This will help you become more familiar with the errors as you make mistakes working through the rest of the exercises. | ||
+ | |||
+ | ===== Comments ===== | ||
+ | Comments are not actually part of the program. They are blocks of free text that you can insert into your code to remind yourself what a piece of code is doing. | ||
+ | |||
+ | Comments begin with the two characters %%//%%. Everything following those characters to the end of the line are considered a comment. | ||
+ | <code cpp example5.cpp> | ||
+ | #include " | ||
+ | |||
+ | start | ||
+ | // This prints a message to the screen | ||
+ | print(" | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | ====== 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. | ||
+ | |||
+ | 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 " | ||
+ | |||
+ | start | ||
+ | integer i = 5; | ||
+ | |||
+ | // prints the value of ' | ||
+ | print(i); | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | 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 " | ||
+ | |||
+ | start | ||
+ | integer i = 5; | ||
+ | print(" | ||
+ | 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; | ||
+ | print(i); | ||
+ | i = i + 1; // add 1 to ' | ||
+ | print(i); | ||
+ | i = i + 1; | ||
+ | print(i); | ||
+ | i = i + 1; | ||
+ | print(i); | ||
+ | i = i + 1; | ||
+ | print(i); | ||
+ | i = i + 1; | ||
+ | print(i); | ||
+ | i = i + 1; | ||
+ | print(i); | ||
+ | i = i + 1; | ||
+ | print(i); | ||
+ | i = i + 1; | ||
+ | print(i); | ||
+ | i = i + 1; | ||
+ | print(i); | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | 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 " | ||
+ | |||
+ | start | ||
+ | integer i = 1; | ||
+ | println(i); | ||
+ | i = i + 1; // add 1 to ' | ||
+ | println(i); | ||
+ | i = i + 1; | ||
+ | println(i); | ||
+ | i = i + 1; | ||
+ | println(i); | ||
+ | i = i + 1; | ||
+ | println(i); | ||
+ | i = i + 1; | ||
+ | println(i); | ||
+ | i = i + 1; | ||
+ | println(i); | ||
+ | i = i + 1; | ||
+ | println(i); | ||
+ | i = i + 1; | ||
+ | println(i); | ||
+ | i = i + 1; | ||
+ | println(i); | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | The output of this code now looks like this: | ||
+ | < | ||
+ | 1 | ||
+ | 2 | ||
+ | 3 | ||
+ | 4 | ||
+ | 5 | ||
+ | 6 | ||
+ | 7 | ||
+ | 8 | ||
+ | 9 | ||
+ | 10 | ||
+ | </ | ||
+ | |||
+ | ===== 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 '' | ||
+ | - Use a variable called '' | ||
+ | - Use a variable called '' | ||
+ | - Print the value of '' | ||
+ | |||
+ | |||
+ | ====== Strings ====== | ||
+ | |||
+ | <code cpp example14.cpp> | ||
+ | #include " | ||
+ | |||
+ | start | ||
+ | string s = " | ||
+ | print(s); | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | ====== Getting Input ====== | ||
+ | |||
+ | <code cpp example15.cpp> | ||
+ | #include " | ||
+ | |||
+ | start | ||
+ | string name; | ||
+ | print(" | ||
+ | read(name); | ||
+ | print(" | ||
+ | print(name); | ||
+ | print(" | ||
+ | end | ||
+ | </ | ||
+ | |||
+ | <code cpp example16.cpp> | ||
+ | #include " | ||
+ | |||
+ | start | ||
+ | integer number; | ||
+ | print(" | ||
+ | read(number); | ||
+ | print(" | ||
+ | print(number); | ||
+ | print(" | ||
+ | </ | ||
+ | |||
programming_tutorial.1349216321.txt.gz · Last modified: 2023/08/18 18:15 (external edit)