Categories: All Variables and Constants  String manipulation  Builtin functions  Input and output  Arrays  Conditional logic  Repetition / Iteration  Functions / Procedures 

Show:

This resource is designed as a quick reference or revision guide. It has not been endorsed by any exam boards. If you spot any mistakes, please let me know and I'll fix them asap.

Input and output

The purpose of almost every program is to read data in from somewhere, process it into useful information and then send it out somewhere else.

Inputs can be the keyboard, mouse or reading from a file or network.

Outputs can be the screen, speakers or saving to a file or network.

Top Tips: 

  • Think about the type of data you're reading in and where you're going to store it.
    Integers are whole numbers; strings are text; booleans are either true or false and reals are numbers that can have a decimal point.
  • Think about what needs to be done to that data to process it into useful information.
    When you read a value from an input, you often need to save it into a variable so you can use it later. You can use operators (plus +, minus - , times * and divide /) to process integers and reals and you can use concatenation and substrings to process strings.
  • Think about the type of data you're sending out.
    See the first bullet point. It's also worth thinking about the order you want to output information as it will appear so you can plan the order of your lines of code.
Skill
Description
VB.NET
Python
C#
Displaying text
    

Most programs need to output text to the user. 

This example displays the text "Hello" to the screen

# display text print("Hello")
// display text Console.WriteLine("Hello");
Displaying a string variable
   

A string variable lets your program store text that might change as your program runs.

This example creates a string variable called name and displays it to the screen.

# create a variable called name name = "Bob" # display name print(name)
// create a variable called name string name = "Bob"; // display name Console.WriteLine(name);
Displaying an integer variable
   

An integer variable lets your program store a whole number that might change as your program runs.

This example creates an integer variable called score and displays it to the screen.

# create a variable called score score = 10 # convert score to a string then display it print(str(score))
// create a variable called score int score = 10; // convert score to a string then display it Console.WriteLine(score);
Displaying a mixture of text and variables
    

Variables let your program store data of a particular data type.

Integer variables store whole numbers. String variables store text.

Sometimes you need to combine text with integer variables and string variables when you display it to the screen.

This example has a string variable called name (set to "Bob") and an integer variable called score (set to 10). It combines both variables with some text to display the message "Bob has 10 points"

score = 10 name = "Bob" print(name + " has " + str(score) + " points")
int score = 10; string name = "Bob"; Console.WriteLine(name + " has " + score + " points");
Get user input
    

Most programs need to get the user to type in data that controls how the program behaves.

This program asks the user for their name and saves the result into a string variable called name.

# Prompt the user to enter some text name = input("Enter your name")
// Prompt the user to enter some text Console.WriteLine("Enter your name"); string name = Console.ReadLine();
Ask the user to enter a string
   

A string is some text that can contain more than one letter, number, space or punctuation mark. 

This program asks the user their name and saves the result into a string variable called name.

name = input("What is your name?")
Console.WriteLine("Enter your name"); string name = Console.ReadLine();
Ask the user to enter an integer
   

An integer is a whole number.

This program asks the user to type in their age. Whatever the user types in is then saved into an integer variabled called age.

name = int(input("How old are you?"))
Console.WriteLine("How old are you?"); int name = int.Parse(Console.ReadLine());
Write a string to a file
   

A string is some text that can contain more than one letter, number, space or punctuation mark.

This example writes the string "Hello world" to the file "output.txt"

# open the file f = open("output.txt", "w") # write to the file f.write("Hello world") # close the file so it can be read by other programs f.close()
// add this to the top of your file using System.IO; // Open the file using(StreamWriter sw = new StreamWriter("output.txt")) { // write to the file sw.WriteLine("Hello world"); }
Write an integer to a file
   

An integer is a whole number (without any decimal places).

This example writes the numbers 1-10 into a file called numbers.txt

# open the file f = open("numbers.txt", "w") # loop from 1 to 10 for num in range(1, 10): # write number to the file f.write(num) # add blank line to file f.write("\n") # close the file so it can be read by other programs f.close()
// add this to the top of your file using System.IO; // open the file using(StreamWriter sw = new StreamWriter("numbers.txt")) { // loop from 1 to 10 for(int num = 0; num < 10; num++) { // write number to the file sw.WriteLine(num); } }
Write data to a csv file
   

A csv file stands for a Comma-separated-values file. It will open as a spreadsheet (often in Excel) with lots of data organised into rows and columns.

When you open the csv file in a text editor (like notepad), the value for each cell is separated by a comma. Each row is written on a new line.

This example writes the 3 times table to a file called "timestables.csv":

e.g:

1, 3

2, 6

3, 9

4, 12

etc...

# open the file f = open("timestables.csv", "w") # loop from 1 to 10 for num in range(1, 10): # write to the file f.write(num) f.write(",") f.write(num*3) f.write("\n") # close the file so it can be read by other programs f.close()
// add this to the top of your file using System.IO; // open the file using(StreamWriter sw = new StreamWriter("timestables.csv")) { // lop from 1 to 10 for(int num = 1; num < 10; num++){ // write to the file sw.Write(num); sw.Write(","); sw.Write(num*3); sw.Write("\n"); } }
Read all data from a text file
   

The easiest way to read data from a file is to get the whole file contents as a string.

This example loads the contents of the file "data.txt" into a string variable called contents

f = open("data.txt") contents = f.read() f.close()
// add this to the top of your file using System.IO; using(StreamReader sr = new StreamReader("data.txt")) { string contents = sr.ReadToEnd(); }

Loading...