A-level Computing 2009/AQA/Problem Solving, Programming, Data Representation and Practical Exercise/Fundamentals of Programming/Constant Definitions





Constants are like variables in their declarations and the ability to look at the value stored inside them, however you can not change the values while the program is running, the value of a constant remains (rather unsurprisingly) constant.

3.14

If you try to change a constant value it will bring up an error. They are very useful for values that do not change or rarely change such as VAT, pi, e, etc. By using a constant you don't run the risk that you might accidentally change the value, you wouldn't want to accidentally change pi to equal 4 as All your calculations would go wrong!

Write a program that works out the area and circumference of a circle, setting pi to a constant. Use the equations: area = Πr2 and circumference = 2Πr For example:

Insert radius: 5 area = 78.5398163 circumference = 31.4159265

Write a program that works out the cost of two clothing items showing the price, VAT, and price inclusive of VAT. VAT = 17.5%. For example

Insert price of item 1: 10.40 Insert price of item 2: 19.99 item 1 = 10.40 + 1.82 VAT = £12.22 item 2 = 19.99 + 3.49825 VAT = £23.48825

Why might you want to use a constant in your code instead of a normal variable?

Constants are fixed values, so you can't accidentally assign them new values in other parts of your code.

When is it suitable to use a constant?

When you are using a value that doesn't need to change at run time and will be used in more than one location.