In the last post I had discussed the String or Alpha-Numeric Constant. In this post I shall endeavor to discuss Numeric Constants. They are the most important constants in programming. And you must learn to use them properly if you have to learn programming easily. We must be able to differentiate between alpha-numeric and numeric constant. If a student is unable to this basic task then the whole point of learning programming is defeated.
First question might be what are numeric constants? To put it simply any number without an enclosing quotation mark is a numeric constant. For example
1
23.45
45678
326478
These are all valid numeric constants. Now if we have to see the difference between valid and invalid numeric constants.
Constant | Valid/Invalid |
5.345 | Valid |
"5.345" | Invalid |
Sudeep45 | Invalid |
245 | Valid |
I hope by now you understand the difference between a valid and invalid constant. A numeric constant may be preceded by a sign +.-. For Example
+678
34.56=67 |
"Sudeep"=45 |
34="Sandeep" |
47=67=32 |
Let us analyze these examples and see if they are valid. First line essentially says 34.56 equals to 67 and please note it is not a question but an assertion. Thus computer is trying to put the value of 67 in 34.56. Now that is quite stupid. Both the numbers are different with different values we cannot replace one value with another that is not possible is it?. Second example shows a Alpha-Numeric Constant being given a value of 45. So what it is trying to do is say the word or the name Sudeep means 45. Again it is not question but an assertion. Lets say your name is John and I say John means 23. Does it make any sense. No, it has no logic and as I had mentioned programming is all to do with logic. One more important point to note is that the name Sudeep is enclosed in double quotes thus making it is an alpha-numeric constant. Therefore by definition a constants value cannot be changed. 5litre does not mean 3 tommorow why because 5 has a fixed value or a constant value which cannot be changed. Same way anything inside a double quote has a fixed value and its value cannot be changed.
I hope by now you can differentiate between Numeric and Alpha-Numeric constants. In all programming languages there are few more types of constants which sometime are essential for a particular case. If we have learn programming properly we must know each of them correctly and understand where we must use them. Because these post endevour to teach you programming easily therefore I have left those constants for later discussion.
Just as an exercise try to find out which of these constants are valid and what is their type.
"34.56/12" |
12.56"wer" |
67 |
"Sudeep + 56" |
No comments:
Post a Comment