Representing the Logic of Programs Using Pseudocodes - Part 1

Chapter 2 | Representing the Logic of Programs Using Pseudocodes - Part 1

Problem-Solving Using Pseudocode

As discussed earlier, pseudocodes are used to represent the logic of the problem solution. Once the pseudocode is verified, it can be converted into a program using the vocabulary and syntax of a programming language. The following table lists the keywords that are used to write a pseudocode.

 Using Pseudocodes to Represent Sequential Code

As discussed earlier, a sequential code consists of steps that get executed one after the other. Let us now write the pseudocode that accepts two numbers and displays their sum:
The preceding code is accepting two numbers as first_number and second_number. Then, the two numbers are added and the total is stored in sum. Finally, the last but one statement displays the message ‘The sum of two numbers is” followed by the value of sum. The first_number, second_number and sum used in the preceding pseudocode are variables used to store the value of the numbers and their sum respectively. Before proceeding further, let us have a brief discussion on variables.

Variables and Constants

The computer has an internal memory, which is used to store: 
  • The input provided by the user 
  • The instructions to process the input 
  • The result of the processing or the output 
The user input stored in the memory for processing is called data. To understand how a computer processes data, let us discuss the preceding pseudocode. 

When instructions are executed, the value of the first number is accepted and stored in the memory. Similarly, the value of the second number is also accepted and stored in the memory. To add the input numbers, we need to refer to the two values stored in the memory. We also need to refer to the result stored in the memory, in order to display it. Therefore, a computer needs to identify the memory locations to be able to retrieve values from or store values in them. 

Let us refer to the memory location 4560 of the first number as first_number and the memory location 4562 of the second number as second_number. The result of the addition of the two numbers is stored in the memory location 4564 and is referred as Sum. Each time the set of instructions is executed, the values of first_number, second_number, and sum will vary, depending on the input from the user. Therefore, first_number, second_number, and sum are known as variables.
Variable
Unlike a variable, a constant refers to the memory locations that hold values that do not vary throughout the program execution.

Data Types

In one of the example, the values consisted of numbers. Often, we also need to enter non-numeric data like the names and addresses of people. The memory space required in the computer for different types of values is different. Therefore, there is a need to classify the types of data that can be stored. The data types used are as follows:
  • Numeric
  • Character 

Numeric

variables can contain only numbers, such as age of a person and the price of a commodity. These variables can be used in arithmetic operations.

Character

variables can contain any combination of letters, numbers, and special characters. Examples of character variables are the name of a person or the address. These variables cannot be used for calculation even if they contain only numbers. 

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, nScore is 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. Let us rewrite the pseudocode discussed in the previous section with the data type declarations included.

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, as shown in the following syntax:

variable_name = value 

Or 

Compute variable_name as value 


The preceding statement implies that the variable name specified on the left of the equal to sign is used to store the value specified on the right of the equal to sign. 

The following are some examples of a direct variable assignment:

numeric nHeight, nAge, nCounter 

character cCode 

nHeight = 172 

nAge = 35 

nCounter = 0 

cCode = “XYZ” 

 Accept Statement

Values can be assigned to variables using the accept statement, as shown in the following syntax:

accept variable_name 

Some examples of assignments using the accept statement are shown in the following pseudocode snippet:

character cName 

numeric nAge 

display “Enter your name:” 

accept cName 

display “Enter your age:” 

accept nAge

0 Komentar untuk " Representing the Logic of Programs Using Pseudocodes - Part 1 "

Back To Top