1 / 97

LECTURE

LECTURE. Introduction to Programming Logic and Technique. DTNhung – NIIT HAIPHONG. Start-up. Can you list a serial generation language programming?

jaimin
Télécharger la présentation

LECTURE

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Start-up • Can you list a serial generation language programming? • The vocabulary of commonly spoken communication is ________ the vocabulary of a programming language?a. Greater than b. Less than c. Equal to • What programming language do you like?

  3. Solutions • 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… • a. Greater than • Suggestions: Assembly, Pascal, C, C++, Visual Basic, Visual C++, Java, Delphi, ASP, JSP, Pearl…

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

  5. 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. Output Process Input Feedback

  6. Introducing to Programming Concepts 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)

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

  8. Machine language 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 No need translater (compiler)

  9. Assembly language LD Ax, 9 LD Bx, 10 ADD Ax,Bx LD (100),Ax JMP Bx HLT • 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. Assembler 100 0 00 11 11 000 0 0 1 00 0 111

  10. High level language • 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.

  11. High level language • 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 Operation code (Opcode) Operand (Address)

  12. High level language begin numeric nNumber1, nNumber2, nNumber3 nNumber1 = 15 nNumber2 = 2 nNumber3 =nNumber1% nNumber2 display nNumber3 end

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

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

  15. Pseudocode Vocabulary • Declare (variables, literals, procedures, functions…): khaibáo • Assign: gángiátrị • Function: hàm • Procedure: thủtục • Flowchart: lượcđồthuậttoán • Pseudocode /sj:udoucode/: mãgiả • Variable: biến • Constant/literal: hằng • Expression: biểuthức • Operator • Arithmetic: toántửtoánhọc • Relational: toántửquanhệ • Logical: toántử logic • Operand: toánhạng • Precedence: mứcưutiên • Comment: chúthích • Dry run table: chạythô

  16. Algorithm - Pseudocode Begin 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

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

  18. 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ô

  19. Pseudocode Begin numeric nNum1, nResult // Declare variables numeric nCons = 10// Direct assignment, initialize first value Display ‘Enter the first number’ Accept nNum1 //Accept statement 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 ================================== Variable: nNum1, nResult Constant: nCons Expression: nResult =(nNum1*nCons)/73 //: Comment Char, numeric: tupe of data

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

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

  22. Pseudocode

  23. Keywords in Pseudocode

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

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

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

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

  28. Variables and Constant

  29. Data Types • Numeric: • 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

  30. 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”

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

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

  33. Operators • Arithmetic: +, -, *, /, % • Relational: >, <. >=, <=, =, != • Logical: AND, OR, NOT Bảng chân trị (chân lý)

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

  35. Priority - Precedence

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

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

  38. 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> • end • else • begin • <statements to be executed if • condition is false> • end • endif

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

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

  41. If Construct • Problem 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

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

  43. If Construct • Solution: 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

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

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

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

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

  48. Problem 1 switch (nCall) begin case 1: //case 1 begins Transfer call to the Marketing department break case 2: //case 2 begins Transfer call to the Finance department break case 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 end end

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

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

More Related