Fundamentals of Programming: Input and output

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

UNIT 1 - ⇑ Fundamentals of Programming ⇑

← Comments Input and output Arithmetic operators →


An important part of computer code is allowing your user to input data into the program, things such as text, key presses or even a data feed from a motion sensor games controller.

Once the data is in the program you want the system to output responses, these may take the form of output to a screen: graphics, text, output to a sound device, printed documents or data output to another program.

For this unit you need to be familiar with a few input and output commands. The little black box that appears on your screen is called the console. In VB.NET all the commands to read and write from this are accessed from the console. command, let's take a look at some:

Outputs[edit]

For the AS course it is unlikely you will need anything beyond the print to screen command and the write to file command (see later). There are two write to screen commands that you should be familiar with:

console.write("Hello") 'writes the word hello to the screen
console.writeline("Hello") 'writes the word Hello to the screen and adds a carriage return (new line)

Let's see an example of this in action:

console.write("Hello ")
console.write("how ")
console.write("are ")
console.writeline("you?")
console.writeline("I'm fine thank ")
console.write("you.")

This would output the following:

  Blank.svg Code Output
Down arrow Hexagonal Icon.svg

Hello how are you?
I'm fine thank
you.


Notice the difference between a writeline and a write command.

Extension: Console methods

We can do a lot of things with the console command. If you are using Visual Studio then type the following:

console.

Up should pop some suggestions. Play around with BackgroundColor and ForegroundColor

There is also a command to output a small and annoying sound, it's unlikely you'll need to know this for the exam, and your teachers will most likely hate it.

console.beep() 'plain beep
console.beep(2000,500) 'beep at 2000Hz for 0.5 seconds

Find out more about the console command on the MSDN website

Inputs[edit]

To make your program interactive you'll need your user to input commands, for the exam these will most likely be text instructions or reading from a file (covered later). You might look at buttons and games controllers next year. In VB.NET there are two types of input command that you need to know:

variable1 = console.readline() 'reads input until user presses enter and stores it in variable1
variable2 = console.read() 'reads one key press/character and stores the ASCII code in variable2.  We'll learn more about this later

Let's take a look at a quick example of where this might be used:

dim name as string   'declare a variable called name
console.write("Please insert your name:") ' write "Hello" and the name to the screen
name = console.readline() 'assign the user's input into the variable name
console.writeline("Hello " & name) ' write "Hello" and the name to the screen
  Blank.svg Code Output
Down arrow Hexagonal Icon.svg

Please insert you name: Ali
Hello Ali


There is also the console.ReadKey()command that reads in a single character. Take a look at this example:

dim urChoice as char 'declare the name
console.writeline("Please select from options 1,2,3 or 4:")
urChoice = console.Readline() 'read the users input into the variable name
console.writeline("You chose : " & urChoice )
  Blank.svg Code Output
Down arrow Hexagonal Icon.svg

Please select from options 1,2,3 or 4:
4
You chose : 4


Exercise: Inputs and Outputs
What does the following code output:
console.writeline("The ")
console.write("Cat")
console.write("sat ")
console.writeline("on ")
console.writeline("the " & "mat")

Answer :

  Blank.svg Code Output
Down arrow Hexagonal Icon.svg

The
Catsat on
the mat

dim num as integer = 19
console.writeline("The average age ")
console.write("of a combat soldier ")
console.write("in Vietnam was " & num)

Answer :

  Blank.svg Code Output
Down arrow Hexagonal Icon.svg

The average age
of a combat soldier in Vietnam was 19

Write code to display the following to the screen:

  Blank.svg Code Output
Down arrow Hexagonal Icon.svg

My favourite colours:
Red
Blue

Answer :

console.writeline("My favourite colours:")
console.writeline("Red")
console.writeline("Blue")

What would the following look like:

dim age as string
dim name as integer
console.writeline("Enter your name: ")
age = console.readline()
console.writeline("Enter your age: ")
name = console.readline()
console.writeline("Hello " & name & ". You are " & age & " years old ")

for the input:

Nobby
16

Answer :

  Blank.svg Code Output
Down arrow Hexagonal Icon.svg

Enter your name: Nobby
Enter your age: 16
Hello 16. You are Nobby years old


Whoops! Fix this:

Answer :

dim age as integer
dim name as string
console.writeline("Enter your name: ")
name = console.readline()
console.writeline("Enter your age: ")
age = console.readline()
console.writeline("Hello " & name & ". You are " & age & " years old ")