Sunteți pe pagina 1din 5

EXCELSIOR COMMUNITY COLLEGE

SCHOOL OF COMPUTER STUDIES

CPRG1201 – INTRODUCTION TO PROGRAMMING

UNIT II: INTRODUCTION TO ALGORITHM, PSEUDOCODE & C#

UNIT OBJECTIVES:

Upon completion of this unit, students, should be able to:


 define the term algorithm
 develop algorithms using real world scenarios
 define an identifier
 list at least four (4) major data types
 define the term pseudocode
 identify the different elements of a pseudocode
 convert an algorithm to its corresponding Pseudocode
 identify the basic structure of a C# program

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.

PSUEDOCODE is using English-like statements to represent the solution to a problem.

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.

DATA VALUE is a statement that represent data items to be stored.

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)

Prepared by: Shonique Clarke 2


OUTPUT STATEMENTS are instructions that display results stored in memory from processing.
For Example: PRINT TotalPrice
PRINT “The Total Price of the item is: ” , TotalPrice

DATA TYPES

Some examples of data types are:


 Integer – positive and negative whole numbers. For example: 3, 2, -5.
 Real – positive and negative decimal numbers (fractional value). For example: 3.14, -5.1, 8.0, 0.05
 Char – a single character, it can be a letter, a digit or a symbol. For example: A, a, !, 4, =.
 String – this is group of characters. For example: “Male”, “Kingston”, “123-4567”.
 Boolean – can only store two values; true or false.

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

Prepared by: Shonique Clarke 3


C# DATA TYPES

The following are commonly used C# data types:


 int – Used to store numbers without decimals (Integers). Integers have several data types with C#,
depending on the size of the number they are supposed to store. For example: sbyte – short,
ushort, unit, long and ulong.
 byte – used to store integers from 0 – 255.
 decimal – used to store a 128-bit value (28-29 digits). It is appropriate for financial and monetary
calculations.
 double - stores 64-bit floating-point values (15-16 digits)
 float – Used to store numbers which may or may not contain decimals (7 digits).
 bool – Contains only two values: True or False. It is used with logic operators like the IF statement.
 enum – used to declare an enumeration, a distinct type that consists of a set of named constants
called the enumerator list.
 char - represents 16-bit Unicode character.
 string - represents a sequence of zero or more Unicode characters

BASIC C# RULES

 The basic rules for naming classes in C# are as follows:


 A name must begin with a letter that could be followed by a sequence of letters, digits (0 - 9) or
underscore. The first character in an identifier cannot be a digit.
 It must not contain any embedded space or symbol such as: ? - + ! @ # % ^ & * ( ) [ ] { } . ; : " ' /
and \. However, an underscore ( _ ) can be used.
 It should not be a C# keyword/ reserved word.

BASIC STRUCTURE OF A C# PROGRAM

PSEUDOCODE C#

PROGRAM WorkersPay using System;


namespace WorkersPay
Declare constants {
Declare variables class Program
{
Statements static void Main(string[] args)
Statements {
Statements /*Declare constants*/
/*Declare variables*/
ENDPROGRAM /* Statements*/
}
}
}

Prepared by: Shonique Clarke 4


BASIC PROGRAM

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();
}
}
}

Prepared by: Shonique Clarke 5

S-ar putea să vă placă și