Further material from our "learning to program in ...." courses ... an introduction to variables and constants
variable basics
Information - data - needs to be stored in a program between statements. Or rather it needs to be stored in the computer's memory. At the lowest of levels, that's a binary pattern of 0s and 1s in a numbered memory location that's encoded in such a way that it can be formed back into something that represents a number, or a string of text. I've programmed computers that work like this - in the very early days - it works, but it's pretty impractical for anything but elementary programs. So what do we do?
- We give descriptive names to the places we need to store the data
- We allow the programming language stuff to decide where to store the data in memory so we don't have to bother
- We have the programming language deal with all the low level formatting too
Variable names are typically of the programmer's choice, subject to a strict series of rules that differ from langauge to language. Typically, they'll be comprise a letter, followed by more letters, digits and underscores. Maximum name length, whether capital and lower case letters have a different meaning, whether a variable name may start with an underscore differ. Also
- In some langauges, variable names are [sometime] preceeded by a special character - a "sigil"
- And in some languages, certain words can't be used as variable names - "reserved words" such as
if and
break
In some languages, the programmer is required to state the names of the variable that wil be used so that the compiler can allocate memory efficiently; that also has the benefit of making the programmer thinks about exactly what's going on. In other languages, it's the langauge internals which work out what storage is needed, and how it's to be used / coded, based on the context in which it's used in the code. Although this latter solution sounds easiest to write and is good, it
does have the disadvantage that it's all too easy for a variable to be misnamed and for the programmer to end up with a 'bug' that's hard to find.
On types and Scope
I've mentioned different types of data that need to be stored .. there are whole numbers, numbers with decimals, pieces of text, and others too - collections or groups of variables, booleans ("true" or "false") and indeed composite varaibles of our own type definition. As you get deeper into programming, you'll need to understand these various type.
Data sometimes need to be converted between types - for example a string of characters input by our user at the keyboard needs converting into a number on which calculations can be done. In sorme languages, this is done automatically for you, but in others you have to request explcitly that it be done.
There's also the matter, as programs grow, of how long the data in them (and the name) is retained - it would be wasteful in a long running program to retain data that's only required for a very short period as the program starts up right through to when the program finished running, but it would be frustrating if the programmer came to use a piece of data to find that it had gone away. There's the further matter here of a program that's got sections written by different programmers, and the need for variable names used by each of the programmers to be distinct from each other. Think of two families living in the same town, both who have daughters who they name "Lorna". That's find and good around the home, but when the two young ladies end up in the same class as school, the teacher says "please stand up Lorna" and both will stand up ... so there needs to be something extra. This subject is called the
scope of a variable, and it's so important that we raise it to make you aware of an upcoming issue even on your very first day of programming, though solutions and detailed discussions must wait until we're further into the course.
Constants
There are some numbers -
"constants" - which won't change (or you don't expect to change) in your program. There are 24 hours in a day, and 7 days in a week. And there are some values which are constant to you - the maximumm number of delegates on a public course might always be 7, the number of working days in the week might be 5, the BMI levels below which and above which a person is regarded as being unhealth may 20.0 and 25.0.
There's nothing to stop you writing these numbers directly into your program, but that's not a good idea:
* You'll find it hard to find all occurrences of the number should it ever change
* You'll write code that's confusing in the extreme if the same constant happens to apply to two things
* The code won't be very descriptive when you come to read it back.
So you'll find that early on we recommend that you assign constants into named locations, and on this first day of the course we'll use variables. However, many languages also support a special notation for named constants, and if you use that:
* Your code can run more efficiently as there needs to be no mechanism to amend a value
* In languges which statically assign memory, a whole heap of complexity can be solved is the constant is a "maximm number of ..."
* The maintenance programmer is clearly told "this value won't be changing at run time"
* The constant can be much more widely scoped so that its's available right through your code without scope conflicts.
(written 2014-11-22)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
J703 - Java - Variables [127] Conversion and coercion in Java - (2004-11-22)
[1448] Question on division (Java) - Also Perl, PHP, Python ... - (2007-11-28)
[2148] Variable scope in Java Servlets and other web applications - (2009-05-01)
[2153] Class Loading and Variable Conversion in Java - (2009-05-02)
[3038] Setting up individual variables, and arrays, in Java - some commented examples - (2010-11-09)
[3041] Java - basic rules for arithmetic, variables and conversion - (2010-11-10)
[3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
[3365] Turning bright delegates into bright and knowledgable ones - (2011-07-21)
[3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
[4345] Incrementing a variable in Java - Pre and Post Increment - (2014-12-08)
R103 - Basic Ruby Language Elements [986] puts - opposite of chomp in Ruby - (2006-12-15)
[2287] Learning to program in Ruby - examples of the programming basics - (2009-07-15)
[2296] Variable scope - what is it, and how does it Ruby? - (2009-07-18)
[2613] Constants in Ruby - (2010-02-01)
[2617] Comparing floating point numbers - a word of caution and a solution - (2010-02-01)
[3430] Sigils - the characters on the start of variable names in Perl, Ruby and Fortran - (2011-09-10)
[3758] Ruby - standard operators are overloaded. Perl - they are not - (2012-06-09)
[4369] Ruby - the second rung of learning the language - (2014-12-28)
[4504] Where does Ruby load modules from, and how to load from current directory - (2015-06-03)
C201 - C and C based languages - C Language Fundamentals [888] Turning C from source to a running program - (2006-10-06)
[1671] Compiling C programs with gcc - an overview - (2008-06-10)
[2005] Variables and pointers and references - C and C++ - (2009-01-23)
[2576] What does const mean? C and C++ - (2010-01-15)
[2842] Staring a C course with Hello World - why? - (2010-06-30)
[3120] Learning to write good programs in C and C++ - separating out repeated code - (2011-01-04)
[3234] Your program - you just provide the filling in the sandwich - (2011-04-08)
[3591] Integer types, and integer overflows, in C - (2012-01-25)
[4555] Preprocessor directives in C and C++ - what they mean - (2015-10-27)
[4566] C - why is slow to write and debug) but fast to run? - (2015-11-01)
P202 - Perl Fundamentals [184] MTBF of coffee machines - (2005-01-20)
[748] Getting rid of variables after you have finished with them - (2006-06-06)
[1312] Some one line Perl tips and techniques - (2007-08-21)
[1726] Hot Courses - Perl - (2008-07-28)
[1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
[1946] Variable Types in Perl - (2008-12-15)
[2442] Variable storage - Perl, Tcl and Python compared - (2009-10-08)
[2832] Are you learning Perl? Some more examples for you! - (2010-06-27)
[2876] Different perl examples - some corners I rarely explore - (2010-07-18)
[3059] Object Orientation in an hour and other Perl Lectures - (2010-11-18)
[3102] AND and OR operators - what is the difference between logical and bitwise varieties? - (2010-12-24)
[3329] Perl from basics - (2011-06-20)
[3398] Perl - making best use of the flexibility, but also using good coding standards - (2011-08-19)
[3542] What order are operations performed in, in a Perl expression? - (2011-12-07)
[3574] Perl functions such as chop change their input parameters - (2012-01-10)
T202 - Tcl/Tk - Tcl Fundamentals [3] Looking for a donkey - (2004-08-05)
[210] Joining lists in Tcl. Indirect variables in Tcl. - (2005-02-12)
[328] Making programs easy for any user to start - (2005-05-29)
[349] Comments in Tcl - (2005-06-16)
[362] The ireallyreallywanna operator - (2005-06-28)
[782] Converting between Hex and Decimal in Tcl - (2006-06-28)
[1136] Buffering output - why it is done and issues raised in Tcl, Perl, Python and PHP - (2007-04-06)
[1282] Stringing together Tcl scripts - (2007-07-29)
[1426] Buffering up in Tcl - the empty coke can comparison - (2007-11-10)
[1469] Curley brackets v double quotes - Tcl, Tk, Expect - (2007-12-12)
[4453] Tcl variable names - no real limits! - (2015-03-10)
U101 - Introduction to Lua. [1111] Training in Lua - (2007-03-16)
[1695] Lua Course - here is the index - (2008-07-03)
[1699] If you are learning Lua, here are some more examples - (2008-07-06)
[1737] Rules, suggestions, considerations for Lua variable names - (2008-08-05)
[1842] Lua Course, and the Wiltshire Countryside too - (2008-10-16)
[2310] Learning to write high quality code in Lua - (2009-07-30)
[3724] Learning to Program in Lua - public / open training course / class - (2012-05-09)
[4271] Line, block and nested comments - Lua compared to other languages - (2014-05-04)
Y102 - Python - Fundamentals [633] Copying a reference, or cloning - (2006-03-05)
[956] Python security - trouble with input - (2006-11-30)
[1430] Integer v float - Python - (2007-11-12)
[1461] Python - input v raw input - (2007-12-06)
[1878] Pascals Triangle in Python and Java - (2008-11-10)
[2368] Python - fresh examples of all the fundamentals - (2009-08-20)
[2778] Learning to program in Python 2 ... and / or in Python 3 - (2010-05-24)
[3083] Python - fresh examples from recent courses - (2010-12-11)
[3181] Beware - a=a+b and a+=b are different - Python - (2011-02-23)
[3551] Some terms used in programming (Biased towards Python) - (2011-12-12)
[3886] Formatting output - why we need to, and first Python example - (2012-10-09)
[4442] Mutable v Immuatble objects in Python, and the implication - (2015-02-24)
[4712] A reminder of the key issues to consider in moving from Python 2 to Python 3 - (2016-10-30)
H103 - PHP - Variables, Operators and Expressions [483] Double Dollars in PHP - (2005-11-02)
[2215] If nothing, make it nothing. - (2009-06-02)
[3916] PHP variables - dynamically typed. What does that mean? - (2012-11-08)
[4642] A small teaching program - demonstration of principles only - (2016-02-08)
Q100 - Object Orientation and General technical topics - Learning to Progam [116] The next generation of programmer - (2004-11-13)
[1605] Learning and understanding scripting programming techniques - (2008-04-08)
[1963] Best source to learn Java (or Perl or PHP or Python) - (2008-12-28)
[1985] Learning to program as a part of your job - (2009-01-10)
[2001] I have not programmed before, and need to learn - (2009-01-19)
[2048] Learning to program in PHP, Python, Java or Lua ... - (2009-02-19)
[2092] Tracking difficult bugs, the programmer / customer relationship - (2009-03-20)
[2286] New to programming? It is natural (but needless) for you to be nervous - (2009-07-14)
[2294] Can you learn to program in 4 days? - (2009-07-16)
[2326] Learn a new programming language this summer. - (2009-08-06)
[2504] Learning to program in ... - (2009-11-15)
[2505] I almost put the bins out this morning - (2009-11-16)
[2898] Programming Standards from the start! - (2010-08-02)
[2973] Learning to program - where to start if you have never programmed before - (2010-09-28)
[3895] Flowchart to program - learning to program with Well House - (2012-10-14)
[4008] Reading and checking user inputs - first lessons - Ruby - (2013-02-17)
[4318] Learning to Program - how we start to teach you at Well House Consultants - (2014-11-16)
[4322] Learning to Program - the conditional statement (if) - (2014-11-21)
[4323] Learning to program - Loop statements such as while - (2014-11-22)
[4325] Learning to program - what are algorithms and design patterns? - (2014-11-22)
[4326] Learning to program - comments, documentation and test code - (2014-11-22)
[4337] Learning to program sample program - past its prime, but still useful - (2014-12-02)
[4575] Learning not just what a program does, but how to design it in the first place. - (2015-11-06)
Some other Articles
Folk music train, Westbury to Swindon round trip, 14th December 2014Good, stable, reliable local businessesLearning to program - variables and constantsAre administration / review charges on hotel guests acceptable?An example of Model-View-Controller techniques in a Perl / CGI scriptPHP - some quick fixes if scripts have issues, and how to use our support