Contact us

Get In Touch

Please contact us, if you have a plan or design in mind that you'd like to share or work with us.

Follow Us On:

User Ajay
597

Before we actually start writing a program, we need to derive procedure to solve the problem.There are various tools available to derive solution to a given problem, such as pseudoode and flowcharts.


Pseudocode is comprehnsive depiction of a solution to the problem and provides programmers a detailed template for writing instructions in a specific programming language.


OBJECTIVES

  1. Problem solving using pseudocode
  2. Variables and Constants
  3. Data types
  4. Operators
  5. Condition execution

1. Problem solving using pseudocode


Keyword Usage
//
  1. Represents a comment entry. A comment entry is used to provide additional information about the stepsin the pseudocode.
  2. The comment line always starts with the '//'.
  3. For example : //here we are going to write the comment.
begin...end
  1. All the steps of the pseudocode are written between the begin...end block.
  2. The first statement in the pseudocode is a begin statement and the last statement is an end statement.
accept
  1. Is used to accept input. For example, to accept the name of the user, the following statement can be used :
    accept UserName
compute
  1. Represents a comment entry. A comment entry is used to provide additional information about the stepsin the pseudocode.
  2. The comment line always starts with the '//'.
  3. For example : //here we are going to write the comment.
display
  1. Represents a comment entry. A comment entry is used to provide additional information about the stepsin the pseudocode.
  2. The comment line always starts with the '//'.
  3. For example : //here we are going to write the comment.
if..else
  1. Represents a comment entry. A comment entry is used to provide additional information about the stepsin the pseudocode.
  2. The comment line always starts with the '//'.
  3. For example : //here we are going to write the comment.
repeat...until
  1. Represents a comment entry. A comment entry is used to provide additional information about the stepsin the pseudocode.
  2. The comment line always starts with the '//'.
  3. For example : //here we are going to write the comment.
while
  1. Represents a comment entry. A comment entry is used to provide additional information about the stepsin the pseudocode.
  2. The comment line always starts with the '//'.
  3. For example : //here we are going to write the comment.
for( ; ; )
  1. Represents a comment entry. A comment entry is used to provide additional information about the stepsin the pseudocode.
  2. The comment line always starts with the '//'.
  3. For example : //here we are going to write the comment.
call
subroutine_name
  1. Represents a comment entry. A comment entry is used to provide additional information about the stepsin the pseudocode.
  2. The comment line always starts with the '//'.
  3. For example : //here we are going to write the comment.


2. Variables and Constants


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, and Sum. The variable, first_number stores the first number entered by the user and maps to the memory address, 4560. The variable, second_number stores the second number entered by the user and maps to the memory address, 4562. The variable, Sum stores the sum of the two numbers and maps to the memory address, 4564. The following figure shows the graphical representation of the three variables in the memory.





In the preceding scenario, to calculate the sum of the two numbers entered by the user, the program needs to simply perform the following computation:



Sum = first_number + second_number



During computation, the program would refer to the variable, first_number to retrieve the first number, 10, entered by the user and the variable, second number to retrieve the second number, 15, entered by the user. The variable, Sum will store the sum of the two numbers, 25.



3. Data Types



While interacting with the computer, a user may enter any type of value, such as name, address, age, and date. These values can be broadly categorized in the following types:



A. Numeric:



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.



B. Character:



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.



Therefore, a variable should be declared along with the data type. The data type specifies the type of value to be stored by the variable. This ensures that correct data is stored in the variables.



Variable Naming Conventions :



Though there are no fixed naming conventions for variables, you might find the following guidelines useful for naming variables.



1. 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 Hungarian Notation, which is widely used in programming these days.

2. The variable name should clearly describe its purpose. For example, nScore is a numeric variable to store the score.

3. 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.

4. 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 :

A. Direct assignment
B. Accept statement

A. Direct Assignment



Values can be assigned to variables in the direct assignment method using the equal to () sign or using the Compute keyword, as shown in the following syntax:



variable_name=value
Or
Compute variable_name as value

The following pseudo code segment depicts direct variable assignment:

begin
numeric nHeight, nAge, nCounter
character cCode
nHeight = 172
nAge = 35
nCounter = nAge+10
cCode = 'xyz'
end



B. Accept Statement



Values can be assigned to variables by using the accept keyword that gets the value from a user, as shown in the following pseudo code segment :



begin
...
accept variable_name
...
end


Some examples of assignments using the accept keyword are shown in the following pseudo code segment :

begin
character cName
numeric nAge
display 'Enter your name:'
accept cName
display 'Enter your age:'
accept nAge
...
...
end



4. Operators

Programs are required to perform a lot more than just simple input and output operations. All computer languages provide tools for some predefined operations. These tools are known as operators.

The operators are categorized in the following ways:

1. Arithmetic operators: Are used for computation purposes. The symbols that represent arithmetic operations are called arithmetic operators. These include*, /, +, -, and %.

2. Relational operators: Are used for comparisons and are used for decision-making. Some of the relational operators include ==, <, and >.

3. Logical operators: Are used for decision-making. Some of the logical operators include AND, OR, and NoT.

To know the operators in more details click here..



4. Conditional Execution

Most problems in real life may not be as simple as the simple computation of numbers. Many problems require decisions to be made. Let us look at an example where two values have to be accepted from the user. The values have to be then compared and a result indicating whether they are equal or not has to be displayed.

The following types of decision-making constructs can be used in pseudocode

A. if construct
B. switch...case construct



A. The if Constructs :



The if decision-making construct specifies a conditional expression that needs to be evaluated. A decision is made based on the result of the conditional expression. There are many types of the if construct.

The three main types of if constructs are :

A. Simple if construct
B. if ...else construct
C. Nested if... else construct



2. The switch...case Construct



Another conditional construct available is, switch...case construct. It is used when a variable needs to be compared with multiple values to make a decision.

The switch...case construct enables you to make a decision by selecting from a number of choices. This construct consists of one switch statement, a number of case statements, and a default statement.

To know the Conditional Execution in more details click here..

Shares  

Comments: