Fundamentals of Programming: A program

From Wikibooks, open books for an open world
Jump to navigation Jump to search

PAPER 1 - ⇑ Fundamentals of programming ⇑

Intro to programming Variables →


VB.NET example[edit]

When you first load VB.NET in Visual Studio and select to run a Console Application, you will be presented with some source code:

module module1
  sub main()

  end sub
end module

These lines of code will tell the computer what to do, currently they do very little and we need to get started:

Tradition has it that the first program a programmer should write is "Hello World!". Write the following sourcecode into a command line VB.NET programming environment:

module module1
  sub main()
    console.writeline("Hello World!")
    console.readline()
  end sub
end module

If you are using Visual Studio then you can make the program run by pressing F5 or hitting the Run button that looks a little like this:Media-playback-start-green.svg

You should get the following output:

   Code Output
Down arrow Hexagonal Icon.svg

Hello World!


Python example[edit]

There are many ways to start writing programs in Python on your computer, there are even websites that you can go to and start writing Python code immediately, for example try repl.it

The traditional "Hello World!" program written in Python is just one line of code:

print("Hello World!")

You then need to run the code. If you are using a website like repl.it this is done by simply clicking the run button.

You should get the following output:

   Code Output
Down arrow Hexagonal Icon.svg

Hello World!


A more complex example in both VB.NET and Python[edit]

There it is, you're on your way to becoming a programmer! There is a lot more to learn and over the course of the next few sections you'll get a crash course in programming.

First of all let's look at another program and find out what it's doing (note that the numbers down the left are not part of the program, we often number the lines of code in this so we can say something like "Now look at line 4 which does a cool thing"):

VB.NET Python
1 module module1
2   sub main()
3     console.writeline("Hello there, my name is Peter and my age is 29")
4     console.writeline("6 * 6 = " & 6 * 6)
5     console.readline()
6   end sub
7 end module
1 print("Hello there, my name is Peter and my age is 29")
2 print("6 * 6 = " + str(6 * 6))

We'll take a look at each line of the VB.NET program first:

  1. module module1 - this line tells the computer that this particular program is called module1
  2. sub main defines the section of code that is executed first
  3. console.writeline("Hello...29") - this line writes plain text to the console window. There are lots of other console commands we can perform such as console.beep and console.color. We'll learn about them in the input/output section
  4. console.writeline("6 * 6 = " & 6 * 6) - this writes out a combination of text (everything between the "speech marks") and calculation (6*6), joining the two together with the ampersand &.
  5. console.readline() - If you are running VB from a command line this won't be necessary, but for people using Visual Studio it is. console.readline() waits for you to hit the return key. Modern computers are very fast and if you didn't have this then the words displayed on the screen would appear and then disappear too fast for the eye to see, the screen would appear, then instantly disappear, take this line out and see what I mean.
  6. end sub defines the end of the main code section.
  7. end module - signifies the end of the small program we have written

Now let's look at each line of the Python program:

  1. print("Hello...29") - this line writes plain text to the console window.
  2. print("6 * 6 = " + str(6 * 6)) - this writes a combination of text (everything between the "speech marks") and calculation (6 * 6), joining the two together with the plus symbol +. We have to help Python by casting the result of the calculation as a string using the str command, see the page on Data types to begin to understand why this is necessary.

Both programs should output the following:

   Code Output
Down arrow Hexagonal Icon.svg

Hello there, my name is Peter and my age is 29

6 * 6 = 36

But wait a second, this program isn't much use! Your name probably isn't Peter and you're even less likely to be 29. Time for you to write some code yourself:

Exercise: Hello World!
Create a short program to write the following to the screen, replacing the name Dave with your own name (unless it also happens to be Dave):
   Code Output
Down arrow Hexagonal Icon.svg

Dear Teacher,
My name is Dave and this homework is too easy.
2+2 = 4

Yours Sincerely,
Dave

Answer:

VB.NET answer:

 1 module module1
 2   sub main()
 3     console.writeline("Dear Teacher,")
 4     console.writeline("My name is Dave and this homework is too easy.")
 5     console.writeline("2 + 2 = " & 2 + 2) 'bonus points for using a sum!
 6     console.writeline("")
 7     console.writeline("Yours Sincerely,")
 8     console.writeline("Dave")
 9     console.readline()
10   end sub
11 end module

Python answer:

1 print("Dear Teacher,")
2 print("My name is Dave and this homework is too easy.")
3 print("2 + 2 = ", 2 + 2)
4 print()
5 print("Yours Sincerely,")
6 print("Dave")

You can show your friends and family. But wait! It's a rubbish program if you want to share it amongst your friends! Each one of them will have to go and change the source code, then hit run. Rubbish unless you live in a country where everyone has the same name, let's call that country 'Davia', I'm pretty sure you don't live there. We better look at making a program that is a little more interactive, where people can change parts of the program without having to keep re-writing it. For that we'll need something called a variable.