Friday, December 14

Constants

Constants are static values which do not change during the execution of the program.
There are two types of constants. They are :
1. Numeric  Constants
2. String Constants

 1. Numeric Constants

The numeric constants are divided into :
1.1 Numeric Real Constants
1.2 Numeric Integer Constants

1.1 Numeric Real Constants  

 A numeric real constant is one which is written using the digits 0,1,2,3,4,5,6,7,8,9 and the decimal point.
  • The signs '+' or '-' can appear before the constant
  • In the absence of the sign, the number is assumed to be positive.
Table C 2.7 Numeric Real Constants
Valid                                                                       Invalid 
 Numeric Real Constants                                         Numeric Real Constants
                                                            Constants                  Invalid reason
16.291                                                 16.291                 Blank character is not allowed                                                    
0.241                                                   16.2,91                Comma is not permitted
-29.0                                                    25.1+0                a sign (+) can appear only before the constant
205.205
2.5E +2

1.2 Numeric Integer Constants

Numeric integer constants are those numeric values that are written using decimal digits 0,1,2,3,4,56,7,8,and 9.

  • Numeric integer constant is a whole number written without decimal point.
  • The signs '+' and '-' can appear before the constants.
  •  In the absence of sign, the number is assumed to be positive. 
  •  The constants should not contain any special character.
  • The valid range of numeric constants is from -32768 to 32767.

Example 4
Table C 2.8 Numeric Integer Constants

Valid numeric                                               Invalid
integer constants                                          numeric integer constant
                                                                   constant            invalid reason
-32767                                                       120.5               contains a blank
 0                                                               10,120            use of comma is not permitted
125                                                            20.05           decimal point is not allowed value out of range
31291                                                        -32769 

2. String constants            

A sequence of a set of characters enclosed in a pair of double quotes is known as a string (or literal) constant.

  • The string can contain either alphabets or numeric constants or special characters or combination of them.
  • Blank spaces can be freely instated any where in a string with a pair of double quotes.
  • Some system of BASIC don't accept double quote symbol as a part of a string.
Example 5

"We are learning about string constants"               

"MY HOBBY & AMBITION ARE different"
"Rs. 2053"
"2053/04/14"
"SLC Examination"
"My" best "friend  is Raghav" (some system may not accept this example as a valid string)  




No comments:

Post a Comment