3.2.1, 3.2.7 - Data types, input and output (C#)
Choose suitable types for a program's data, read keyboard input and display useful results. C# examples and written answers are native C#. C# execution is unavailable here; the runnable activity is an optional Python demonstration of the same idea.
Choosing a data type
A data type describes the kind of value a program stores and the operations it can perform on that value.
Data type
A classification of data, such as integer, real, Boolean, character or string.
Consider an entrance-ticket program. A count of visitors needs whole numbers; a ticket price may need a fractional part. A statement such as “the visitor is a member” has only two possible truth values. An initial is one character, while a name is a sequence of characters.
| General exam name | C# type | Suitable example |
|---|---|---|
| Integer | int | Number of tickets: 3 |
| Real | double | Measured height: 1.72 |
| Boolean | bool | Membership status: true |
| Character | char | Initial: 'R' |
| String | string | Name: "Riya" |
The general exam name real may be called float in some languages; double is an appropriate native C# real-number type. A Boolean is true or false, not the strings "true" and "false". Single quotes enclose a C# character and double quotes enclose a string. A postcode is a string even if part of it contains digits: you do not calculate with it, and letters or leading zeroes must be preserved.
These are declarations, each giving a type, a meaningful name and a starting value:
int ticketCount = 3;
double height = 1.72;
bool isMember = true;
char initial = 'R';
string visitorName = "Riya";
The examples in this lesson are statements inside the Main method of a console program with using System;. You do not need to reproduce that surrounding boilerplate when a written question asks for a fragment.
Reading keyboard input
Keyboard input lets a program work with data supplied while it is running, rather than only values chosen by the programmer.
Console.ReadLine() reads the entered line as a string. Display a prompt first so the user knows what to supply, then store the returned text in a variable.
Console.Write("Visitor name: ");
string visitorName = Console.ReadLine();
Console.Write("Tickets: ");
int ticketCount = int.Parse(Console.ReadLine());
The ticket input initially arrives as text. int.Parse converts valid integer text such as "3" into integer 3, so it can be used in arithmetic. double.Parse similarly converts valid real-number text. These exam examples assume input has the requested data type; implementing type-error checks is not required for GCSE examination input. A later lesson teaches required checks such as a permitted numeric range.
In the optional Python demonstration, predict what entering Riya and 3 will display. Run it with those inputs. Then change the multiplier from 4 to 5, predict the new total, and rerun with the same inputs. This explores input conversion; it is not a C# answer.
Optional Python demonstration: input and conversion
{"code":"visitor_name = input("Visitor name: ")\nticket_count = int(input("Tickets: "))\nprint(visitor_name)\nprint(ticket_count * 4)","instructions":"Enter Riya and 3. Then change ticket_count * 4 to ticket_count * 5 and rerun with the same inputs."}
Displaying results
Output communicates a program's result to the user, so a correct calculation should be accompanied by a useful display.
Console.WriteLine displays a value and then moves to a new line. Console.Write leaves the next output on the same line, which is useful for an input prompt. A label tells the reader what a result means. The + below joins text to the stored name or number for display.
int ticketCount = 3;
int totalCost = ticketCount * 4;
Console.WriteLine("Tickets: " + ticketCount);
Console.WriteLine("Total cost: £" + totalCost);
Three tickets
Example runTickets: 3
Total cost: £12
Calculating totalCost changes program data. Displaying it lets the user see that data; output alone does not store a new value. Choose names and labels that make this difference clear.