Fundamentals of Programming: Input and output

PAPER 1 - ⇑ Fundamentals of programming ⇑

← Comments Input and output Selection →


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). In VB.NET 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:

   Code Output
 

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


Notice the difference between a writeline and a write command.

Python does not have two separate commands in the way VB.NET does. The print command will by default add a new line on to the end of each string. Here's an example:

print("This is the first line. This is the second half of the first line.")
print("This is the second line.")

will produce the output:

   Code Output
 

This is the first line. This is the second half of the first line.
This is the second line.


If you want to output several things all on the same line you have to add end="" to the print command. This tells Python that instead of ending with a new line you instead want to end with nothing. So the following example outputs all on the same line:

print("This output will", end="")
print(" all appear ", end="")
print("on the same line.", end="")
   Code Output
 

This output will all appear on the same line.


Extension: Console methods in VB.NET

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
   Code Output
 

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 )
   Code Output
 

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


In Python there is just the one command for reading user input from the keyboard which is input. Here's an example:

print("Please enter your name:")
name = input()
print("Hello ", name)

which if you type in your name as Alice will give the following:

   Code Output
 

Please enter your name:
Alice
Hello Alice


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:

   Code Output
 

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:

   Code Output
 

The average age
of a combat soldier in Vietnam was 19

Write code to display the following to the screen:

   Code Output
 

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:

John
16

Answer:

   Code Output
 

Enter your name: John
Enter your age: 16
Hello 16. You are John 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 ")