Presentation is loading. Please wait.

Presentation is loading. Please wait.

C OMPUTER P ROGRAMMING 1 Input and Variables. I/O S TATEMENTS : I NPUT & V ARIABLES Input is the term used to describe the transfer of information from.

Similar presentations


Presentation on theme: "C OMPUTER P ROGRAMMING 1 Input and Variables. I/O S TATEMENTS : I NPUT & V ARIABLES Input is the term used to describe the transfer of information from."— Presentation transcript:

1 C OMPUTER P ROGRAMMING 1 Input and Variables

2 I/O S TATEMENTS : I NPUT & V ARIABLES Input is the term used to describe the transfer of information from peripheral devices to the processor e.g. input may come from the keyboard or from a disk file. Before we describe input statements, let us consider where to store the information to be read in. We must arrange to store the input so that it can be processed. To do this, we need to introduce the concept of variables.

3 I/O S TATEMENTS : I NPUT & V ARIABLES Variables can be used to store the data that we input. Sometimes, the data to be input may have many parts. This may require us to use many variables in our program. So, we need to be able to clearly identify each variable that we are going to use. But how do we identify this variable? The solution is easy: we give each variable a name which we use to identify it. So, a variable can be viewed as a named container for a value.

4 I/O S TATEMENTS : I NPUT & V ARIABLES The statements : char colour[80]; int age; define variables called colour and age. They tell the computer the names of the variables and that colour will hold characters (up to 80 of them), while age will contain a number (in this case a whole number or integer value).

5 I/O S TATEMENTS : I NPUT & V ARIABLES We call such statements declarations they declare the names of the variables and specify what kind of information the variables may contain e.g. characters or numbers. We use the term type to describe the kind information that may be stored in a variable e.g. in the declaration below int age; the variable age is of type int ( i.e. an integer variable)

6 I/O S TATEMENTS : I NPUT & V ARIABLES In C and Java, variables must be declared before they can be used. A common error is to omit the declaration of a variable, which results in a syntax error message when we attempt to use the undeclared variable. We can use any name we wish for variables with the exception of the reserved words mentioned earlier. It is a good idea to choose meaningful names for variables, because it make programs easier to understand. This is more difficult than it sounds but is a fundamental principle in writing readable programs

7 I/O S TATEMENTS : E XAMPLE Consider a program to prompt the user to enter their favourite colour, read this colour and display a message followed by the colour entered by the user. The program may be written in C as follows: /* colour.c: program to prompt for colour and display a message Author: Rem Collier Date: 28/02/2013 */ #include main() { char colour[80]; /* Variable to hold colour */ printf(“Enter your favourite colour: “); gets (colour); /* Read colour from keyboard */ printf(“Yuk! I hate %s“, colour ); }

8 I/O S TATEMENTS : E XAMPLE If we execute the program the following appears on the screen (the bolded text is that entered by the user - we will use this convention throughout the text). Enter your favourite colour: blue Yuk! I hate blue The first statement in the program declares a variable called colour. Its type is that of a list of characters. The printf() statement is used to display the message prompting for input.

9 I/O S TATEMENTS : E XAMPLE The statement gets(colour); handles the input from the keyboard. It reads text from the keyboard, for example the word blue may be entered, and it places the text in the variable colour.

10 I/O S TATEMENTS : E XAMPLE The statement printf(“Yuk ! I hate %s“, colour ); ! instructs the computer to display on the screen the message Yuk! I hate blue Note that %s does NOT appear on the screen, it has been replaced by the value of the variable colour i.e. blue in this example. It is the method used to tell printf() to display the value of a variable. The name of the variable to be displayed, follows the message. The %s indicates to printf() that the variable contains text (also referred to as a string ) so that printf() can display it appropriately. We use %d to display integers and %f to display real numbers

11 I/O S TATEMENTS : I NPUT & V ARIABLES We often represent a computer's memory as a list of boxes or containers. A variable as we have said may be regarded as a container or a box in memory. Pictorially, we can represent the colour variable as a location in memory (in fact it occupies 80 locations in memory): Memory blue Value of the variable Name of the variable colour

12 I/O S TATEMENTS : I NPUT & V ARIABLES We can use gets() to give values to string variables We can use printf() to display the value contained in any variable. We use the expression “ the value of a variable” to mean “the value contained in a variable” We take the phrase “the value of colour is blue” to mean “the value contained in the variable called colour is blue”. We will use the shorter form from now on.

13 I/O S TATEMENTS : I NPUT & V ARIABLES From the program above, we see that printf() has the ability to display messages enclosed in quotation marks the values of variables. For example: printf ( “What’s your favourite colour“ ); displays the string inside the quotation marks on the screen, and printf ( “%s“, colour ); displays the value of the variable colour, which in this example is also a string i.e. a list of characters. A message enclosed in quotes is called a string literal (or string) as it will never change when the program runs.

14 I/O S TATEMENTS : I NPUT & V ARIABLES Make sure you understand the difference between: printf( “colour“ ); and printf( “%s“, colour ); In the first case, a string is displayed, i.e. the word colour appears on the screen. In the second case, the value of a variable called colour is displayed. This could be anything, for example the word blue or whatever value the user has given the variable like red, pink and orange There is nothing to prevent you storing more than one word in a string variable.

15 D OCUMENTING YOUR P ROGRAMS The text between /* and */ is called a comment and is ignored by the computer. This text is used as documentation to help explain to someone reading the program, how the program works. Comments are a very important component of programs. This is because when you read your programs some time after writing them, you may find them difficult to understand, if there are no comments to explain what you were doing. They are even more important if someone else will have to read your programs (e.g. your tutor who is going to grade them!) NOTE: It is good practice to give the program’s filename, the authors name and the date on which the program was written as the first comment in any program.

16 D ELIBERATE E RROR Remove the end of comment characters */ that are in red on the 7th line of the program. Compile and run the program. What happens ? Why ? /* colour.c: program to prompt for colour and display a message Author: Rem Collier Date: 28/02/2013 */ #include main() { char colour[80]; /* Line 7: Variable to hold colour */ printf(“Enter your favourite colour: “); gets(colour); /* Line 10: Read colour from keyboard */ printf(“Yuk! I hate %s“, colour ); }

17 D ELIBERATE E RROR E XPLAINED When you remove the end of comment on line 7, the compiler treats the text on the following lines as part of the comment until it reaches line 10 and finds the end of comment characters there. This means that the printf() and gets() statements do NOT get executed. Thus the last printf() which tries to print the value of colour may behave unpredictably. It could even cause the program to crash. This is because you have not given colour a value and so printf() displays whatever happened to be in the variable colour. At a later stage we will explain why this could cause your program to crash.


Download ppt "C OMPUTER P ROGRAMMING 1 Input and Variables. I/O S TATEMENTS : I NPUT & V ARIABLES Input is the term used to describe the transfer of information from."

Similar presentations


Ads by Google