大象传媒

Data types, structures and operators - EdexcelInput/output and validation

Programs use data, known as 鈥榲alues鈥. Variables hold values. Each variable in a program must have a data type. Sometimes a programmer needs to store a lot of related data. To do this they use structures such as arrays.

Part of Computer ScienceApplication of computational thinking

Input/output and validation

A typical program inputs , processes it, then outputs it. The data input must be to ensure that the rest of the program functions properly.

Many programs take input from a user and then validate it before it is processed. It is important that the messages to the user are appropriate to reduce the risk of invalid data being entered. Messages should also be polite to encourage people to use the program!

The following section of Visual Basic.NET code asks the user to enter a number in a particular range, checks that it is the required range, and outputs an appropriate message to the user:

Console.WriteLine("Please enter a number between 1 and 10")
        num1 = Int(Console.ReadLine())
            If num1 < 1 Or num1 > 10 Then
                Console.WriteLine("Sorry, this number is not 
                between 1 and 10, try again")
            Else
                Console.WriteLine("Thanks for entering a valid
                number")
            End If