Views
1 week ago

Understanding Variables

Understanding Variables Picture a bank which has a vault full of safety deposit boxes. Customers of the bank can rent a safety deposit box and store anything they like within it such as money, jewellery and documents. Each box will have a number so that it can easily be identified. Customers can visit their safety deposit box whenever they like; add things into the box, take things out of the box or change the contents of the box altogether. When the customers goes to the bank they will use the identifier (number) so that they get the correct box. You can think about variables a little like bank safety deposit boxes. Computer memory has lots of locations in which data (like lives or points in a game) can be stored. During the course of play, the game will need to regularly access a location in memory to store ‘lives’ and then access it again later to update ‘lives’ if the player gains or loses a life. Rather than using a number as an identifier (like a safety deposit box), we usually give each variable a meaningful name or identifier so that we can refer to it throughout a program. A computer game might do something like this: numLives = 10 177

This line of code stores or assigns the number 10 to a variable (a space in memory a little bit like a safety deposit box) with the name or identifier numLives. Perhaps during the game a player would like to know how many lives they have left. print(numLives) 10 This line of code simply reads the value of the variable numLives (by going to that location in memory and fetching the value); it then uses print to display this on the screen. During gameplay we might gain 3 lives. The computer would need to update the variable to reflect this. numLives already exists (we made it at the start of the game and assigned to it the value 10. We now just want to increase the existing value of numLives by 3. numLives = numLives + 3 print(numLives) 13 This line of code means “the new value of numLives should be equal to itself + an extra 3”. numLives was 10 to start with, so now when we print numLives, we get 13. 178

Brookshear - Computer Science - An Overview 9e - Computarium LCD
COMPUTER SCIENCE
Defensive Database Programming with SQL ... - Red Gate Software
ASAM solutions guide 2011 - Science und Computing AG
Computer Science @ Virginia Tech - Computer Science at Virginia ...
National Academy of Sciences - PCIC Personal Computing Industry ...
Education Manual - School of Computer Science Raspberry Pi ...
1 - Department of Computer Science | University of Pittsburgh
Computer Science ~ Contents - McGraw-Hill Books
maps and algorithms - TAMU Computer Science Faculty Pages
CSE 114 – Computer Science I Lecture 1: Introduction
4/10/08 Backup and Recovery - Computer Science
Survey of Computer Science - Mathematics and Computer Science
COMPUTER SCIENCE - Jones & Bartlett Learning
COMPUTER SCIENCE PROGRAM - Polytechnic University