In some cases, you will want to provide constant values that can be used throughout your code.
For example, you may decide to declare a constant for P.
You should not allow this value to be changed because it will change the underlying logic in your code.
So this means that you should mark the variable as being constant.
So when you do this, the compiler will check the use of the variable and if it is used in the code
that changes the value of the variable, the compiler will usually an error.
So let's create a constant variable here.
Constant double p here 3.14 and 15 and then double radius.
Double radius equals 5.0.
Here.
Sign the 5.0 and double circumference.
Learns to multiply by P.
Android is here.
So in this case the symbol P here is declared as being constant, so it cannot change.
If you subsequently decide to change the constant, the compiler will usually an error.
So once you declare the constant, you can be sure that the compiler will make sure it remains true.
So you can assign the constant with expression as well.
So for example, this could include cmath, include k, C math C math here.
And then let's make a constant.
Double.
Set of two equals.
Constant double square of two equals STD sqrt.
And two here.
So now I want to change.
For example, as you can see, this is the constant.
We cannot change it.
But let's try if we can change our constant value.
So let's assign P to, for example, 99.
As you can see, we got an error cannot assign to variable P with the const qualified type constable.
So in order to work this code, we can.
As you can see here, const, we have to delete the const and now we can assign P on some variable here.
So let's run our code and see what error we will get.
Error assignment of read only variable P.
As you can see here, we got an error in 12 line p 99.
Here.
So.
Now, as I can show you, we can delete this, clear this, and let's delete this line of code as well.
So in this code, as you can see, we got a constant.
We created a new double constant square root of two and we used this cmath.
The library, which you will learn more in next lectures.
So you can also use Constant to declare a constant that will be used as a constant expression for.
So for example, you can declare an array using the square bracket braces syntax.
For example.
Let's create an array here.
Um.
Okay.
Integer values five.
This is an array.
So this declares an array of five integers and the stack and the items are accessed through the values
array variable.
So the five is here is constant expression.
So when you declare an array on the stack, you have to provide the compiler with a constant expression
so it knows how much memory to allocate.
And this means that the size of the array must be known at compile time so you can allocate an array
with a size known only at runtime.
But this requires dynamic memory allocation, which we will explain in next lesson.
Next sections in section named working with memory pointers and arrays.
So in C plus plus you can declare the constant like this two for example, constant integer size equals
five integer my new values and pass this as parameter size here.
So we pass this size and as you can see, we our size equals to five.
So this and this line of code is the same, just a different variable names.
So elsewhere in your code, when you access the values array, you can use the size constant to make
sure that you do not don't access items past end of the day.
So since the size variable is declared in just one place, you need to change the size of the array
at a later stage so you have just one place to make this change.
Also, the const keyword here can also be used on pointers and references which you will learn in next
sections and also on the objects.
Often you will see it used on a parameters to functions.
So this is used to get the compiler to help ensure that pointers, references and objects are used appropriately
as you intended.