Top Banner
LECTURE LECTURE Introduction to Programming Logic Introduction to Programming Logic and Technique and Technique DTNhung – NIIT HAIPHONG DTNhung – NIIT HAIPHONG
97

LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

Jan 12, 2016

Download

Documents

Debra Manning
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

LECTURELECTURE

Introduction to Programming Logic and TechniqueIntroduction to Programming Logic and Technique

DTNhung – NIIT HAIPHONGDTNhung – NIIT HAIPHONG

Page 2: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

22Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Start-up

1. Can you list a serial generation language programming?

2. The vocabulary of commonly spoken communication is ________ the vocabulary of a programming language?a. Greater than b. Less than c. Equal to

3. What programming language do you like?

Page 3: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

33Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Solutions

1. Generations of programming languages 1st generation is machine language. 2nd generation is assembly language. 3rd generation is high-level languages such

as BASIC, Pascal, C…

2. a. Greater than

3. Suggestions: Assembly, Pascal, C, C++, Visual Basic, Visual C++, Java, Delphi, ASP, JSP, Pearl…

Page 4: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

44Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Introduction to Programming Logic and TechniqueIntroduction to Programming Logic and Technique

Chapter 1 Introducing to Programming Concept

Chapter 2 Representing the logic of Programming using Pseudocodes.

Chapter 3 Representing the logic of Programming using Flowcharts

Chapter 4 Understanding Iterations and Implementing Modular

Programming

Page 5: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

55Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Introducing to Programming Concept

Computer follows an I-P-O cycle. It needs set of instructions called program to specify the input required, process to be followed, and the output required.

Program is written in a specific language called Programming language, so that computer can understand the instructions.

Process

Input

Output

Feedback

Page 6: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

66Hai Phong Software ParkHai Phong Software ParkNIITNIIT

A set of instructions to perform a particular job is called a program. Therefore, for each job that you want the computer to perform, you require a separate program. Instructions in a program can be:

Sequential: Instructions that are executed one after the other. „Decision Making: Instructions that evaluate an expression (relation or condition) first and then, depending upon whether the value of the expression is 'true' (non-zero) or 'false' (zero), it transfers the control to a particular statement. „ Iterative: Instructions that are executed repeatedly, depending on the value of an expression (relation or condition)

Introducing to Programming Concepts

Page 7: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

77Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Programming Languages MACHINE LANGUAGE A sequence of 0s and 1s can be used to

describe any physical operation that the computer performs. Therefore, a computer system uses a number system that consists of only two digits, 0 and 1. This number system is known as the binary number system.

The language that the computer understands is called the machine language.

It is doubtful if you would be comfortable writing a program in machine language. It is certainly difficult for anybody to remember instructions in the form of 0s and 1s.

Command Machine code

ADD 00000001

SUBTRACT 00000010

MULTIPLY 00000100

DIVIDE 00001000

READ FROM KEYBOARD

00010000

WRITE ON SCREEN

00100000

WRITE ON PRINTER

01000000

Page 8: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

88Hai Phong Software ParkHai Phong Software ParkNIITNIIT

1000 010 11 1

01100101 011

0001 0 1 010 1

The binary number system uses the base of 2. For example, 101 in the binary system is equal to 5 in the decimal system. The conversion can be done as:

101=1*22 + 0*21+ 1* 20 =1*4 + 0*2 + 1*1 = 4 + 0 + 1=5

Machine language

No need translater (compiler)

Page 9: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

99Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Assembly language

In the above program: The line number one loads register Ax with the value, 9. The line number two loads register Bx with the value, 10. The line number three adds the value of register Bx to the value of register Ax. The line number four stores the value of register Ax in the main memory

location, 100. The line number five uses JMP to jump to register Bx to transfer the control to

register Bx. The line number six stops the program execution.

LD Ax, 9 LD Bx, 10 ADD Ax,Bx LD (100),Ax JMP Bx HLT

Assembler100 0 00

11 11 000 0 0 1 00 0 111

Page 10: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

1010Hai Phong Software ParkHai Phong Software ParkNIITNIIT

A high-level programming language consists of a set of instructions, which are represented using simple English words. So, when you want the computer to display the output on the screen, you type the instruction ‘WRITE ON SCREEN’ and not ‘00100000’.

There are many high-level programming languages available, such as C, C++, and Java. Each language has its own advantages.

Programming languages also have a vocabulary, which is referred to as the set of keywords of that language, and a grammar, which is referred to as the syntax.

High level language

Page 11: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

1111Hai Phong Software ParkHai Phong Software ParkNIITNIIT

As stated earlier, a program written in any programming language is a set of logically related instructions. These instructions have two parts, as shown in the following figure:

The two parts of a programming language instruction are: „ Operation code (opcode): This part instructs a computer about the operation

to be performed. „ Operand: This part instructs the computer about the location of the data on

which the operation specified by the opcode is to be performed.

For example, in the instruction Add A and B, Add is the opcode and A and B are operands

High level language

Operation code (Opcode)

Operand(Address)

Page 12: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

1212Hai Phong Software ParkHai Phong Software ParkNIITNIIT

High level language

beginnumeric nNumber1, nNumber2, nNumber3nNumber1 = 15nNumber2 = 2nNumber3 =nNumber1% nNumber2display nNumber3

end

Page 13: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

1313Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Algorithm

An algorithm is a sequence of steps required to solve a problem An algorithm follows the I-P-O cycle to solve the problem.

The two levels of algorithm are:

Macro-level: An algorithm that contains brief steps about a process is called a macro-level algorithm.

Micro-level: An algorithm that contains detailed steps about a process is called a micro-level algorithm.

An algorithm is represented using tools such as:

Pseudocode

Flowcharts

Page 14: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

1414Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Algorithm

An algorithm has the following five characteristics: An algorithm ends after a fixed number of steps. Each step in an algorithm clearly specifies the action to be

performed. The steps in an algorithm specify basic operations. These

operations could include calculations, input/output operations, and comparisons.

An algorithm accepts input data, in a defined format, before it can be processed.

An algorithm generates one or more outputs after the input is processed. The resultant information termed as output can be displayed or stored for future reference.

Page 15: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

1515Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Pseudocode

VocabularyDeclare (variables, literals, procedures,

functions…): khai báoAssign: gán giá trịFunction: hàmProcedure: thủ tụcFlowchart: lược đồ thuật toán Pseudocode /sj:udoucode/: mã giảVariable: biếnConstant/literal: hằngExpression: biểu thức

• Operator

Arithmetic: toán tử toán học

Relational: toán tử quan hệ

Logical: toán tử logic

• Operand: toán hạng

• Precedence: mức ưu tiên

• Comment: chú thích

• Dry run table: chạy thô

Page 16: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

1616Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Algorithm - PseudocodeBegin

numeric nNum1, nResult

numeric nCons = 10

Display ‘Enter the first number’

Accept nNum1

nResult = (nNum1 * nCons) / 73

If nResult > nCons

Display “ the number is greater than 10’

else

Display “ The number is less than 10’

Display nResult

End

Page 17: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

1717Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Pseudocode

Advantages of pseudocode are:It is easier and faster to write as it uses English like statements .It does not need to be rewritten if any changes are made because each step is

independent and may be modified without altering the other steps.It can be converted to a program using any programming language.

Disadvantages of pseudocode are:Pseudocode does not provide a graphical representation of an algorithm.Pseudocode depicting too many nested conditions may be difficult to

understand.

Page 18: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

1818Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Vocabulary - Pseudocode Declare (variables, literals, procedures, functions…): khai báo Assign (values to variables): gán Operator

Arithmetic: toán tử toán học Relational: toán tử quan hệ Logical: toán tử logic

Operand: toán hạng Operator precedence: toán tử ưu tiên Comment: chú thích Dry run table: chạy thô

Page 19: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

1919Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Pseudocode

Beginnumeric nNum1, nResult // Declare variablesnumeric nCons = 10// Direct assignment, initialize first valueDisplay ‘Enter the first number’Accept nNum1 //Accept statementnResult = (nNum1 * nCons) / 73 If nResult > nCons Display “ the number is greater than 10’else

Display “ The number is less than 10’Display nResult

End==================================Variable: nNum1, nResultConstant: nConsExpression: nResult =(nNum1*nCons)/73//: CommentChar, numeric: tupe of data

Page 20: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

2020Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Flowchart

A flowchart is a graphical representation of the steps to be ollowed for solving a problem.

It consists of a set of symbols.

Each symbol represents a specific activity.

Page 21: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

2121Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Flowchart Advantages of Flowcharts are:

Flowcharts are a better method of communicating logic.

The flowcharts help in analyzing the problems effectively .

The flowcharts act as a guide during the program development phase.

It is easier to debug errors in logic using a flowchart.

The flowcharts help in maintaining the programs.Disadvantages of Flowcharts are:

A lengthy flowchart may extend over multiple pages, which reduces readability.

As flowcharts symbols cannot be typed, drawing a flowchart using any graphic tool is a time consuming process.

The changes made to a single step may cause redrawing the entire flowchart.

A flowchart representing a complex algorithm may have too many flow lines. This reduces readability, and it is time-consuming to draw and understand the logic Readability.

Page 22: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

2222Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Pseudocode

Page 23: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

2323Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Keywords in Pseudocode

Page 24: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

2424Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Keyword in Pseudocode

begin … end: These keywords are used to start and finish

pseudocode. Begin is the first line and end is the last line of

pseudocode.

accept: This keyword is used to obtain an input from a user.

display: This keyword is used to present a result or an output.

if … else… endif: These keywords are used in decision-

making.

//: Comment

Do … while, for …, repeat … until: Represent loop

Page 25: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

2525Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Using Pseudocodes to Represent Sequential Code

The pseudocode for adding two numbers is as follows:

// starting the pseudocode

begin

// input consists of accepting two numbers

accept first_number, second_number

// process of adding the two numbers

compute sum as first_number + second_number

// output for displaying the sum

display ‘The sum of given two numbers is’ sum

// ending the pseudocode

end

Page 26: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

2626Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Variables and Constants

The internal memory consists of different locations in which data is stored.

A computer needs to identify the memory locations to be able to retrieve values from or store values in them.

A variable refers to the memory location whose value changes during the program execution.

A constant refers to the memory location whose value does not change during the program execution.

Page 27: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

2727Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Variables and Constant You need to declare a variable before using it in a program. The declaration of a variable assigns a name to the variable and specifies the type of data

the variable can store.

Naming conventions: The first letter of the variable name might indicate the data type of the variable: cName

and nAge The variable name should clearly describe the purpose of a variable: nScore The variable name should not contain an embedded space or symbols such as ! @ # $ %

^ & * ( ) { } [ ] . , : ; “ ‘ / and \. You can use an underscore when a space is required in a variable name, for example, nBasic_Salary.

If a variable name consists of multiple words without spaces in between, capitalize the first letter of each word for readability. Some examples are nTotalScore and nSumOfSquares.

Page 28: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

2828Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Variables and Constant

Page 29: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

2929Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Data TypesNumeric:

Numeric variables can contain only numbers.

These variables can be used in arithmetic operations .

Character:Character variables can contain any combination of letters, numbers, and special characters.

These variables cannot be used for calculation.

Let us look to the code of accepting two numbers and displaying the sum with the data type declarations.

begin

numeric nNum1, nNum2, nSum //declaring variables

accept nNum1

accept nNum2

nSum = nNum1 + nNum2

display nSum

end

Page 30: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

3030Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Assign values

The variables can be assigned values in the following two methods:Direct assignment

Accept Statement

In the direct assignment method, values can be assigned to variables using the equal to (=) sign, as shown in the following syntax:

variable_name=value

Examples:numeric nHeight, nAge, nCounter

character cCode

nHeight = 172

nAge = 35

nCounter = 0

cCode = “XYZ”

Page 31: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

3131Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Assign values

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

accept variable_name

Examples: character cName

numeric nAge

display “Enter your name”

accept cName

display “Enter your age”

accept nAge

Page 32: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

3232Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Variables and Constant

Identify the variable and constant data in the following situation.

Each day, the courier service delivers some letters. The number of letters is different each day. Regardless of the number of letters delivered by the courier service, they are paid a carrying charge of $5.

Variable:

Constant:

Page 33: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

3333Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Operators Arithmetic: +, -, *, /, % Relational: >, <. >=, <=, =, != Logical: AND, OR, NOT

Bảng chân trị (chân lý)

X Y AND(X,Y) OR (X,Y) NOT(X)

0 0 0 0 1

0 1 0 1 1

1 0 0 1 0

1 1 1 1 0

Page 34: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

3434Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Problem 1

Create the pseudocode to accept item name, price, and quantity. You need to calculate value as the product of price and quantity, and display the calculated value and the item name using variables.

Answer: begin accept cItem_name, nPrice, nQuantity,

nSale_value

nSale_value = nPrice * nQuantity

display cItem_name

display nSale_value

end

Page 35: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

3535Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Priority - PrecedenceOperator Description Associativity Precedence

Level

( ) Parentheses 1! Logical NOT 2* Multiplication Left to right

3/ Division% Modulo division+ Addition Left to right 4- Subtraction< Less than Left to right 5<= Less than or equal to

>= Greater than or equal to

> Greater than= Equal to Left to right 6!= Not equal toAND Logical AND Left to right 7OR Logical OR Left to right 8

Operator Precedence

Page 36: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

3636Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Conditional Execution

Many problems require decisions to be made.

All decisions may or may not state an action to be taken if the condition is false.

The following types of decision-making constructs can be used in an algorithm.

if constructs

switch…case constructs

Page 37: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

3737Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Conditional Execution

In simple if construct, if the condition specified is true, the statements contained within the if block are executed.

Pseudocode segment to represent the simple if constructs as follows:

if <condition> begin

<statements to be executed if

condition is true>

end

endif

Page 38: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

3838Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Conditional Execution

In the if...else construct, the statements within the if block are executed for condition being true and the statements within the else block are executed for condition being false.

if <condition> begin

<statements to be executed if condition is true> endelse begin <statements to be executed if condition is false> endendif

Page 39: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

3939Hai Phong Software ParkHai Phong Software ParkNIITNIIT

If Construct

Problem Statement 1:Accept two numbers and print the larger of the two numbers.

Solution: begin

numeric nNum1, nNum2

accept nNum1

accept nNum2

if nNum1 = nNum2

begin

display “The numbers are equal”

end

Page 40: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

4040Hai Phong Software ParkHai Phong Software ParkNIITNIIT

If Construct

else

if nNum1> nNum2

begin

display nNum1

end

else

begin

display nNum2.

end

endif // end of second if statement

endif // end of first if statement

end

Page 41: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

4141Hai Phong Software ParkHai Phong Software ParkNIITNIIT

If ConstructProblem Statement 2:

Print the value of nX only if the value of nX is greater than 10 and nX is an even number.

Solution:begin

numeric nX

accept nX

if nX > 10 //first if-statement

begin

if nX % 2 =0 //second if-statement

display nX

endif // end of second if-statement

end

endif //end of first if statement

end

Page 42: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

4242Hai Phong Software ParkHai Phong Software ParkNIITNIIT

If Construct

Problem Statement 3:To decide about the discount percentage on a TV, the sales person needs to check the type of TV. If the TV is Black and White [B], the discount will be 5 percent of the selling price. If the type of TV is colored [C], then he has to verify the size of TV screen. For 14 inches screen, discount is 8 percent of the selling price and for 21 inches screen, the discount is 10 percent of the selling price. Write a pseudocode to show the discount percentage.

Page 43: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

4343Hai Phong Software ParkHai Phong Software ParkNIITNIIT

If ConstructSolution:

begin

numeric nScreen, nDiscount, SP

character cType

accept nType

accept nScreen

accept SP

if cType = ‘B’ // first if statement

begin

compute nDiscount as 5% of SP

end

else

if cType = ‘C’ // second if statement

begin

if nScreen =14 // third if statement

begin

compute nDiscount as 8% of SP end

Page 44: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

4444Hai Phong Software ParkHai Phong Software ParkNIITNIIT

If Construct

else

begin

if nScreen = 21 // fourth if statement

begin

compute nDiscount as 10% of SP

end

endif // end of fourth if statement

end

endif // end of third if statement

end

endif // end of second if statement

endif // end of first if statement

end

Page 45: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

4545Hai Phong Software ParkHai Phong Software ParkNIITNIIT

The switch…case construct

The switch…case construct enables you to make a decision by selecting from number of choices. The pseudocode of the switch…case construct is as follows:

switch (expression)

begin

case constant 1:

execute these statements

break

case constant 2:

execute these statements

break

default:

execute these statements

end

Page 46: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

4646Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Problem 1

Consider the following problem statement for switch…case construct.Amy is writing the algorithm for automated telephone call transfer to various departments of the company such as Marketing, Finance, Customer Care, Human Resource (HR), and Information. Write the solution using Pseudocode.

Page 47: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

4747Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Problem 1

begin

numeric nCall

display “If you want to get connected to Marketing

department, press 1, Finance

department press 2, Customer Care

department press 3, HR department press 4.

If you are not sure press any number other

than 1 to 4, the call will be transferred to Information

department”

accept nCall

Page 48: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

4848Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Problem 1 switch (nCall)begin

case 1: //case 1 begins Transfer call to the Marketing department breakcase 2: //case 2 begins Transfer call to the Finance department breakcase 3: //case 3 begins Transfer call to the Customer Care department

break case 4: //case 4 begins Transfer call to the HR department break default: //if none of the cases match, the //following line is executed Transfer call to the Information department endend

Page 49: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

4949Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Vocabulary - Programming

Decision-making construct: cấu trúc ra quyết định Loop construct: cấu trúc lặp Procedure: thủ tục Function: hàm to invoke: triệu gọi Parameter: tham số scope (local, global): phạm vi biến modular approach of programming: lập trình tiếp cận theo

hướng chia module

Page 50: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

5050Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Problem Statement 1:

Problem Statement 1:

Write a pseudocode to display the sum of two numbers by using variables.

Solution: begin

accept nNum1 //assigning values to variables

accept nNum2

nSum = nNum1 + nNum2

display nSum

end

Page 51: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

5151Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Problem Statement 2

Problem Statement 2: Write a pseudocode where interest is calculated and displayed for the given balance and rate.

Solution: begin //start

accept Name, Balance, Rate //input

compute Interest as Balance x Rate //process

display Name and Interest //output

end //end

Page 52: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

5252Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Problem Statement 3

Problem Statement 3: Write a pseudocode where a number is incremented by 1

Solution: begin //starting

accept Number //input

compute Number as Number + 1 //process

display Number //output

end //end

Page 53: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

5353Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Problem 4:

Bình 1 Bình 2Bình rỗng

Numeric nNum1, nNum2, nSwapnNum1 = 5nNum2 = 6nSwap = 0nSwap = nNum1nNum1 = nNum2nNum2 = nSwap

Given: nNum1 = 5, nNum2 = 6Swap their value together.

Bình 1 Bình 2Bình rỗng

Bình 1 Bình 2Bình rỗng

Bình 1 Bình 2Bình rỗng

Page 54: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

5454Hai Phong Software ParkHai Phong Software ParkNIITNIIT

A Problem 5:

Page 55: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

5555Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Flowchart

Page 56: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

5656Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Problem-solving using Flowcharts

Page 57: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

5757Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Problem-solving using Flowcharts

Page 58: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

5858Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Drawing Flowchart

Page 59: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

5959Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Rules of Flowcharting The entire logic of a flowchart should be represented using the standard symbols. „ The flowchart should be clear, precise, and easy to follow. „ Flowcharts can have only one start point and one end point. The steps in a flowchart should follow top-to-bottom or left-to-right approach. All necessary inputs should be listed out in logical order. The start and stop symbols should have only a single flow line. The input, process, output, and display symbols should have two flow lines connecting to the previous symbol and to the next symbol. The decision symbol should have one flow line connecting to the previous symbol but two flow lines connecting to the next symbol for each possible solution such as Yes/No, Y/N, True/False. If too many flow lines make the flowchart complex, it is better to use connector symbols to reduce the number of flow lines. Flowcharts should use page connectors if they extend over multiple pages. It is useful to test the logic of flowchart by passing through it with sample test data.

Page 60: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

6060Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Conditional Execution using Flowchart

Page 61: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

6161Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Conditional Execution using Flowchart

Page 62: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

6262Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Conditional Execution using Flowchart

Page 63: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

6363Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Nested If- Statements in Flowchart

Page 64: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

6464Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Nested If- Statements in Flowchart

Page 65: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

6565Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Switch - case

Page 66: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

6666Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Interation

One of the most important characteristics of a computer is its ability to execute a series of instructions repeatedly. This ability of the computer gives you the flexibility to control the number of times a task is to be repeated.

The following types of loops can be used in a pseudocode: � while loop repeat…until loop � for loop � goto �

Page 67: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

6767Hai Phong Software ParkHai Phong Software ParkNIITNIIT

For loop

The for loop: The for loop is used when the number of iterations of the loop is known before the control enters the loop.

The for loop consists of the following three parts separated by semicolons.

Initialization expression: The numeric variable is initialized using a value.

Evaluation expression: The condition is tested at the beginning of iteration of the loop. When the expression evaluates to false, the loop terminates.

Increment/decrement expression: The value of the variable is increased.

In a for loop, initializing a variable, evaluating a condition, and incrementing the value of the variable are all specified in one statement.

Page 68: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

6868Hai Phong Software ParkHai Phong Software ParkNIITNIIT

For loop

begin

initialize the index

for test the index

begin

increment the index

//perform the statements if the test is true

end

endfor //end of for loop

//perform the statements if the test is false

end

Page 69: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

6969Hai Phong Software ParkHai Phong Software ParkNIITNIIT

For Loop

The pseudocode representation of a for loop is as follows:

begin

initialize the index

for test the index

begin

increment the index

// perform the statements if the test is true

end

endfor //end of for loop

// perform the if the test is false

end

Page 70: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

7070Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Problem Statement 1

Problem Statement 1:Draw a flowchart to calculate the sum of 10 numbers entered by the user.

Page 71: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

7171Hai Phong Software ParkHai Phong Software ParkNIITNIIT

The while Loop

:

:

while (condition)

begin

:

:

end

:

:

Page 72: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

7272Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Problem 2begin

numeric nMarks, nTotal, nCounter, nAvg

nMarks = 0

nTotal = 0

nCounter = 0

nAvg = 0

while (nCounter < 30) //while the condition is true

begin

display “Enter the total marks of a student”

accept nMarks

nTotal = nTotal + nMarks

nCounter = nCounter + 1 //increments the counter by one

end

nAvg = nTotal / nCounter //calculates the average

display “The average marks of the class is”

display nAvg //displays the average

end

Page 73: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

7373Hai Phong Software ParkHai Phong Software ParkNIITNIIT

The repeat…until Loop

:

:

repeat

begin

:

:

end until (condition)

:

Page 74: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

7474Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Page 75: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

7575Hai Phong Software ParkHai Phong Software ParkNIITNIIT

The goto Statement A goto statement is used in situations in which the execution of a

pseudocode does not follow the steps in a sequential order. To use a goto statement in a pseudocode, you need to use labels. A label

identifies the position in a pseudocode where the control of execution can reach using a goto statement. While assigning name to a label, a colon should follow the label name.

begin

numeric nCounter

nCounter=2

Label1:

nCounter=nCounter+2

display nCounter //displays all the even numbers

if(nCounter<=100)

goto Label1 //transfers the control to Label1

end

Page 76: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

7676Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Modular Approach to Programming

A program needs to be amended periodically to respond to changing conditions or requirements.

This encouraged programmers to adopt a more disciplined approach to program writing.

The techniques that were adopted are known as structured programming techniques.

Structured programming includes features that are designed not only to solve the problem at hand but also to make the logic clear to someone reading the program.

Page 77: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

7777Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Modular Approach to Programming

Long, continuous programs can be broken up into a series of individual modules that are related to each other in a specified manner.

Page 78: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

7878Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Procedure

Procedure:In a modular approach, one of the methods of representing a module is procedure.

The working of a procedure is termed as a call-return mechanism .

The following steps are part of a call–return mechanism:

A procedure is called.

The set of operations stored within the procedure are executed.

The control is returned to the calling code.

Page 79: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

7979Hai Phong Software ParkHai Phong Software ParkNIITNIIT

ProcedureDeclaring, Defining, and Invoking Procedures:

Procedures need to be declared and defined before they can be called. The declaration of procedures is similar to a declaration of any other variable in an pseudocode.

Procedures are declared using the following syntax:procedure <procedure_name>

The procedure is defined when the procedure is expressed with its body. The syntax of the procedure definition is:procedure <procedure_name>

begin //the set of statements of a procedure

end

The calling of a procedure is also called invoking a procedure. The method for invoking procedure is known as a procedure call. The syntax of a procedure call is:call <procedure_name>  

Page 80: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

8080Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Procedure

Problem Statement 1: Accept the test scores for ten students and display their individual averages. The scores of the students cannot be negative.

The following table lists the variables used in the solution.

Variable Data Type Variable Name

Student Name character cStudentName

Score of Test1 numeric nTest1

Score of Test2 numeric nTest2

Score of Test3 numeric nTest3

Average of Test Scores numeric nAverage

Page 81: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

8181Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Procedure

Solution:

//code segment to calculate average marks of 10 students

procedure Accept // declaring subprogram Acceptprocedure Average // declaring subprogram Averagebegin numeric nTest1, nTest2, nTest3, nAverage character cStudentName call Accept // calling subprogram Accept call Average // calling subprogram Avearge display cStudentName nAverageend

// code for the subprograms comes here

:

:

Page 82: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

8282Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Procedure

Display cStudentName, nAverage

Accept cStudentName

Average

Average

nAverage=(nTest1+nTest2 +nTest3) / 3

Return

Accept

Accept

Accept nTest1

Accept nTest2

Accept nTest3

Display “Test score cannot be less than zero”

IsnTest1>=0 ANDnTest2>=0 AND

nTest3>=0 ?

No

Yes

Return

Page 83: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

8383Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Procedure

Problem Statement 2:The total expenditure on salaries for the month needs to be calculated. As per company policy an employee receives a minimum of $500. Depict the logic for automating the task by using pseudocode and flowchart.

Page 84: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

8484Hai Phong Software ParkHai Phong Software ParkNIITNIIT

SolutionSolution::

if cChoice = “ Y”

begin

call Accept //invoking Accept

call Summation //invoking Summation

end

:

:

procedure Accept //statements of the subprogram

:

begin

accept nSalary

Page 85: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

8585Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Solution

if nSalary >=500

begin

return // returning the value

end

else

begin

display ‘Salary cannot be less than $500”

end

endif

end // end of the subprogram

procedure Summation // statements of the subprogram

begin

nTotSalary = nTotSalary +nSalary

return nTotSalary

end

Page 86: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

8686Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Procedure Parameters

Procedure Parameters:

Parameters are like a bridge between the procedure and the calling code.

Parameters comprise data that is used and processed by a procedure.

Parameters can be variables of the numeric or character data type.

Parameters are used to perform the following tasks:Send data to a procedure

Retrieve data from a procedure

Page 87: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

8787Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Function

Function:A function is a block of statements that perform a specific task. The underlying fundamental of functions and procedures are very similar, and they can be used interchangeably.

The difference between a function and a procedure is that a procedure does not return any value to a calling program while a function returns a value to the calling program after it is executed.

The functions in a pseudocode interact with each other by passing and receiving data. Functions also operate on the call–return mechanism. The following are the steps in the call–return mechanism using functions:

The function is called from the calling code.

The sets of operations within the functions are executed.

The execution is returned to the calling code using a return value

Page 88: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

8888Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Function

Declaring, Defining, and Invoking Functions:

Functions are declared like any other procedure or variable. The declaration format is function <function_name>

The function are defined same as procedures. The only difference is that they have a return statement at the end. The syntax of the function definition is:function <function_name>

begin

//the function statements

return // The function returns some value

end

After a function is declared, it can be invoked from the pseudocode. The method for invoking functions is known as a function call. The syntax for the function call is:

call <function_name>

Page 89: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

8989Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Function Parameters

Function Parameters:Function parameters form an interface between the function and the calling code.

Functions accept values in the form of parameters. If parameters are specified, they should be separated using commas.

Unlike procedures, the functions use the parameters only for receiving data from the calling code.

They send value to the calling code using the return statement. Therefore, functions have only input parameters.

Page 90: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

9090Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Scope of Variables

Scope of Variables:

Variables can be declared within or outside the begin…end block of a main pseudocode, procedure, or function.

Depending on the place where variables are declared the variables have two types of scope.

Local Scope

Global Scope

The variables that are declared inside the begin...end block of a main pseudocode, function, or procedure have local scope and they are called local variables.

Page 91: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

9191Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Scope of Variables

The following pseudocode describes the local scope of the variables.

begin

numeric nNum

display “Enter any number”

accept nNum

if (nNum > 10)

begin

numeric nRemainder

nRemainder = nNum % 2

display nRemainder

end

endif

end

Page 92: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

9292Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Scope of Variables

Following pseudocode describes the local scope using function.begin numeric nNum display “Enter any number” accept nNum if (nNum > 10) begin call displayRemainder(nNum) display nRemainder //an invalid statement end

endif end function displayRemainder (numeric nNumber) begin numeric nRemainder nRemainder = nNumber % 2 display nRemainderend

Page 93: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

9393Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Scope of Variables

Procedure Test()begin numeric nX, nY nX = 6

nY = 10 Display nX, nY Call Test ()

EndProcedure Test()Begin

numeric nXnx = 9Display nX, nY

End

Page 94: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

9494Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Scope of Parameters

Scope of Parameters:

The parameters of a procedure or function are variables that can be accessed only from inside the procedure or the function.

This implies that the parameters act as the local variables of a function or a procedure.

The following comprise the scope of parameters: Parameters exist only inside the function or procedure for which they are defined. They cannot be accessed from outside the function or procedure.

They retain their value until the function or procedure is executed.

Parameters are initialized every time the function or procedure is called.

Page 95: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

9595Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Questions

A compiler is: A high-level language Language-specific A machine language A source code

The symbol that represents comments in a flowchart is:A procedure/subroutine symbolAn input symbolAn annotation symbolA flow line

A detailed flowchart is called a: Macro flowchart Lengthy flowchart Micro flowchart Detailed flowchart

Which of the following is NOT a type of algorithm?

Flowchart Program Pseudocode Decision table

Page 96: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

9696Hai Phong Software ParkHai Phong Software ParkNIITNIIT

Question

Literals are: Values that change continuously Memory locations Values that do not change Names given to variables

A statement consisting of variables, literals, constants, and operators is called a/an:FunctionIdentifierOperatorExpression

Input is accepted using the keyword accept in:PseudocodeA FlowchartAn AlgorithmAn Expressiont

Which operators are used to test the relationship between two variables or between a variable and a constant?Arithmetic operatorsLogical operatorsRelational operatorsSpecial operators

Page 97: LECTURE Introduction to Programming Logic and Technique DTNhung – NIIT HAIPHONG.

THANK YOUTHANK YOU

Any questions?