Variables and Constants
Programming When a user enters a value, it is stored in a specific location within the memory of the computer. This memory location is given a user-friendly name, which is known as a variable or a constant Details When a user enters a value, it is stored in a specific location within the memory of the computer. This memory location is given a user-friendly name, which is known as a variable or a constant. A variable refers to the memory location that can store only one value at a time but can vary throughout the program execution. However, a constant refers to the memory location that holds a value, which remains fixed throughout the program execution. Consider an example of a program that accepts two numbers and displays the sum. This program needs three variables, two to store the numbers entered by the user and one to store the sum of the two numbers. Each of the variables would map to a specific memory address. For the given scenario, the following variables are declared: first_number, second_number, andSum. The variable, first_numberstores the first number entered by the user and maps to the memory address, 4560. The variable, second_numberstores the second number entered by the user and maps to the memory address, 4562. The variable, Sumstores the sum of the two numbers and maps to the memory address, 4564. Declaring Variables It is essential to declare a variable so that memory is allocated before it is used in a program. The data type of the variable is identified depending on the type of data to be stored in it. Variable Naming Conventions Though there are no fixed naming conventions for variables, you might find the following guidelines useful for naming variables: The first letter of the variable may indicate the data type used. For example, it can be either 'c' or 'n' to indicate a character or numeric variable as in cName, nAge. This variable naming convention is known as Charles Simyoni Hungarion Notation, which is widely used in programming these days. The variable name should clearly describe its purpose. For example, nScoreis a numeric variable to store the score. The variable name should not contain any embedded space and symbols, such as ?, ! @ # $ % ^ & * ( ) { } [ ] . , : ; " ' / \. However, underscores can be used wherever a space is required, for example, nBasic_Salary. In case the variable name consists of multiple words, the first letter of each word could be capitalized for better readability, for example, nTotalScore, nSumOfSquares. Assigning Values to Variables Any variable needs to be assigned a value before its use. This is to ensure that the memory space allocated to the variable is initialized with a valid value. The variables can be assigned values by using the following two methods: Direct assignment Accept statement Direct Assignment Values can be assigned to variables in the direct assignment method using the equal to (=) sign or using the Computekeyword, as shown in the following syntax: variable_name = value Or Compute variable_name as value The following pseudocode segment depicts direct variable assignment: begin numeric nHeight, nAge, nCounter character cCode nHeight = 172 nAge = 35 nCounter = nAge + 10 cCode = 'XYZ' end Accept Statement Values can be assigned to variables by using theaccept keyword that gets the value from a user, as shown in the following pseudocode segment: begin … accept variable_name … end Some examples of assignments using the accept keyword are shown in the following pseudocode segment: begin character cName numeric nAge display 'Enter your name:' accept cName display 'Enter your age:' accept nAge …… end
Sent from Windows Mail