UNIT II - Notes
UNIT II - Notes
UNIT II - Notes
UNIT OBJECTIVES:
DEFINITION OF TERMS
ALGORITHM is the formal method for setting out the sequence of instructions for the solution of a problem.
An algorithm must set out:
Actions to take (instructions)
In what order (sequence)
When to stop (end/terminate the program)
The instructions must be clear and unambiguous. The flow of control should also be clear and it must have
a terminator to the process. There are two types of algorithm: pseudocode and flowchart.
FLOWCHART is using a diagram consisting of shapes and symbols joined by arrows that represent the
sequence of actions to solve a problem.
IDENTIFIERS are words used to represent storage units in the program. An identifier can represent a
variable, constant, class, function, or any other user-defined item.
VARIABLE is name that represents a stored value that can be changed during the execution of a program.
CONSTANT is name that represents a stored value that does not change during the execution of a
program.
LITERAL is a value written exactly as it's meant to be interpreted; a literal is not a name; it is the value
itself. A literal can be a number, a character, or a string.
DATA TYPE is keyword which indicate the format of the data being stored and the operations which may
be performed on the data.
SENTINEL VALUE is dummy value, once it is entered it signals that there are no more valid items left to be
processed and terminates. It is typically used with control structures that use repetition/iteration.
CONTROL STRUCTURES are programming constructs which determine the order in which instructions are
to be executed (sequence, selection, repetition).
OPERATORS are symbols or keywords that represent computer operations. These operations are usually
(but are not confined) to mathematical operations. Examples include the following: +, -, >, <, AND, OR,
NOT.
MODULES are programming statements which are packaged together to solve a task. The module is
usually given a unique name, takes in one or more inputs and generates an output.
PSEUDOCODE
In writing the solution using pseudocode, the following should be observed:
Statements are in written in simple English
Each instruction is written on a separate line.
Keywords and indentation are used to signify particular control structures.
Each set of instructions is written form top to bottom, with only one entry and one exit.
There are several elements which are usually present within each program, such as, Input/Output
statements, Identifiers (variables & constants), Datatype, Data values, Control structures, Operators and
modules.
ELEMENTS OF A PSEUDOCODE
INPUT STATEMENT are instructions that accept data into the computer and store the value in the location
with the given variable names. For Example: READ A, B
ASSIGNMENT STATEMENT – this is used to assign a value to a variable or to store the results from
processing. The assignment symbol is the equal sign =.
For Example: Age = 18. The variable Age will be stored with the value 18.
Assignment statements are also used for processing to store results of expressions or calculations
(processing statement). For Example: TotalPrice = Price + (Price * 0.1)
DATA TYPES
OPERATORS: ARITHMETIC
OPERATOR MEANING EXAMPLE
+ Add TotalPay = BasicPay + Bonus
– Subtract NetPay = TotalPay – Tax
* Multiply Days = Week * 7
/ Divide Days = Hours/24
OPERATORS: RELATIONAL
OPERATOR MEANING EXAMPLE
< Is less than 4 < 100
> Is greater than X>Y
= Is equal to (X*Y) = 10
<= Is less than or equal to Age <= 18
>= Is greater than or equal to Height >= 1.5
<> Is not equal to X <> 0
OPERATORS: LOGICAL
OPERATOR MEANING EXAMPLE
AND Both statements X and Y are true (X) AND (Y)
OR At least one of the statements X or Y is true (X) OR (Y)
NOT Statement X is not true NOT (X)
AND combines 2 or more conditions; the combined statement is true only if all the individual conditions
are true.
OR combines 2 or more conditions; the combined is statement is true if one of the individual conditions
is true.
NOT converts a true value to false and vice versa
BASIC C# RULES
PSEUDOCODE C#
PSEUDOCODE C#
using System;
namespace WorkersPay
PROGRAM WorkersPay {
class Program
Declare constants {
static void Main(string[] args)
Rate = 35 {
Declare variables const double Rate = 35;
Hours, Pay as real double Hours, Pay:
Console.Write("Enter the hours worked:
");
PRINT “Enter the hours worked” Hours =
READ Hours Convert.ToDouble(Console.ReadLine());
Pay = Hours * Rate Console.Write("Enter the hourly
rate:");
PRINT “Pay is: $”, Pay
Pay = Hours * Rate;
Console.WriteLine("Pay: ${0}", Pay);
ENDPROGRAM
Console.ReadLine();
}
}
}