Computer Science Canada [Ada-tut] Declaring variables and simple math |
Author: | wtd [ Sun Oct 10, 2004 12:28 am ] | ||||||||||||||
Post subject: | [Ada-tut] Declaring variables and simple math | ||||||||||||||
Where do I put variable declarations in an Ada program? Very simply, variable declarations follow "is" and precede "begin". Let's create a program which declares two integer variables.
Of course, we can simplify that in a way that should look familiar to Turing programmers, or those used to any Pascal-derived language.
Let's assign some values Assignment is pretty simple in Ada. Again, it's gonna look quite familiar to our resident Turing programmers. ![]()
Now, for a little simple math
Type safety and floating point numbers For safety purposes, Ada95 doesn't make it easy to mix integers and floating point numbers in mathematical operations. Any conversions between types have to be explicit. So let's start with the program we had earlier, but also declare a float Z with a value of 2. This will also demonstrate the ability to give variables initial values when they're declared. Note: floating point constants must contain a decimal point.
Now, to do some math mixing integers and floating point numbers. Let's divide X by Z and put the result back in X.
Of course, it's possible to use something like:
To convert an integer into a float. |