Introduction to Programming By: wajid Khaksar: Introduction to Programming By: wajid Khaksar . What Can a Program Do?: What Can a Program Do? A program can only instruct a computer to: Read Input Maintain Sequence Calculate Store data Compare and branch Iterate or Loop Write Output Sequence Control Structures: Sequence Control Structures Sequence control structures direct the order of program instructions. The fact that one instruction follows another—in sequence—establishes the control and order of operations . Calculate: Calculate A program can instruct a computer to perform mathematical operations. Store: Store A program will often instruct a computer to store intermediate results. Compare and Branch: Compare and Branch A program can instruct a computer to compare two items and do something based on a match or mismatch which, in turn, redirect the sequence of programming instructions. IF IF-ELSE IF-THEN: IF-THEN Test condition p false true Entry Exit True statement a IF-THEN-ELSE: IF-THEN-ELSE false true Entry Exit Test condition p “true” statement a “false” statement a Iterate: Iterate A program loop is a form of iteration. A computer can be instructed to repeat instructions under certain conditions. No Iteration Control Structures: Iteration Control Structures Iteration control structures are looping mechanisms. Loops repeat an activity until stopped. The location of the stopping mechanism determines how the loop will work: Leading decisions Trailing decisions Leading Decisions: Leading Decisions If the stop is at the beginning of the iteration, then the control is called a leading decision. The command WHILE performs the iteration and places the stop at the beginning. WHILE Loop: WHILE Loop No Yes Entry Exit Test condition p Loop statement a Trailing Decisions: Trailing Decisions If the stop is at the end of the iteration, the control mechanism is called a trailing decision. The command DO UNTIL performs the iteration and puts the stop at the end of the loop. DO UNTIL Loop: DO UNTIL Loop Loop statement a No Yes Entry Test condition p Exit Programs are Solutions to Problems: Programs are Solutions to Problems Programmers arrive at these solutions by using one or more of these devices: Words Logic flowcharts Pseudocode Programming language Example 1:: Example 1: Write an algorithm to determine a student’s final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks. In words : In words Input a set of 4 marks Calculate their average by summing and dividing by 4 if average is below 50 Print “FAIL” else Print “PASS” Logic Flowcharts: Logic Flowcharts These represent the flow of logic in a program and help programmers “see” program design . It is a way of visually presenting the flow of data, the operations performed within the system and the sequence in which they are performed. Common Flowchart Symbols: Terminator. Shows the starting and ending points of the program. A terminator has flowlines in only one direction, either in (a stop node) or out (a start node). Data Input or Output . Allows the user to inputdata and results to be displayed. Decision . The diamond indicates a decision structure. A diamond always has two flowlines out. One flowlineout is labeled the “yes” branch and the other is labeled the “no” branch. Common Flowchart Symbols Common Flowchart Symbols Common Flowchart Symbols: Off-page connector . Even fairly small programs can have flowcharts that extend several pages. The off-page connector indicates the continuation of the flowchart on another page. Off-page connectors come in pairs. Flowline . Flowlines connect the flowchart symbols and show the sequence of operations during the program execution . Common Flowchart Symbols Processing . Indicates an operation performed by the computer, such as a variable assignment or mathematical operation. Rules for Drawing a Flowchart : Rules for Drawing a Flowchart It should contain only one starter and one end symbol. The direction of arrows should be top to bottom and left to right/right to left. It should be simple and drawn clearly and neatly. The branches of decision box must be labeled. Advantages / Disadvantages : Advantages / Disadvantages Advantages Provides convenient method to understand the solution. Helps in debugging process Provides guide for coding. Disadvantages Not suitable for large programs. Calculate Pay: Calculate Pay The program computes the sum, average and product of three numbers:: The program computes the sum, average and product of three numbers: Sum of two numbers: Sum of two numbers Example: Example Step 1: Input M1,M2,M3,M4 Step 2: GRADE (M1+M2+M3+M4)/4 Step 3: if (GRADE <50) then Print “FAIL” else Print “PASS” end if STOP PRINT “PASS” START Input M1,M2,M3,M4 GRADE (M1+M2+M3+M4)/4 IS GRADE<50 PRINT “FAIL” Y N Pseudo code: Pseudo code This device is not visual but is considered a “first draft” of the actual program. Pseudocode is written in the programmer’s native language and concentrates on the logic in a program—not the syntax of a programming language. Pseudocode: Pseudocode Step 1: Input M1,M2,M3,M4 Step 2: GRADE (M1+M2+M3+M4)/4 Step 3: if (GRADE < 50) then Print “FAIL” else Print “PASS” endif The program computes the average of three numbers: The program computes the average of three numbers Begin input x input y input z sum = x + y + z avg = sum / 3.0 print avg End Calculate Pay: Calculate Pay Sum Of Two Numbers: Sum Of Two Numbers Begin input x, y sum = x + y print sum END Practice: Practice Draw flowchart for computing Factorial of N Practice Solution: Practice Solution Programs: Programs A program is a set of step-by-step instructions that directs the computer to do the tasks you want it to do and produce the results you want. Computer programs are written in programming languages . Programmer : Programmer A person who develops a program is called programmer. The programmer develops programs to instruct the computer how to process data to convert into information Programmers use programming languages to write programs Programming Languages: Programming Languages A computer language is a means of communication between user and computer. A programming language is a set of rules that provides a way of telling a computer what operations to perform. Levels of Programming Languages: Levels of Programming Languages 1.Low Level Languages: The languages are near to computer and far from human languages . Computer can understand these languages easily. Following are low level languages: 1.Machine language 2.Assembly Language Low Level Languages: : Low Level Languages: 1.Machine Languages A type of language in which instructions are written in binary form . It is only language that is understood by the computer. It is fundamental language of computer. Program written in machine language can be executed very fast by the computer It is also known as first generation language. Machine Languages: Machine Languages different for each computer processor 0100 001101 100000 001101 110001 00101 10001 10000 01110 111001 . . . 2.Assembly Languages: 2.Assembly Languages It is low-level language It is one step higher than machine language In assembly language, symbols are used instead of binary code These symbols are called mnemonics For example: sub instruction is used to subtract two numbers. It is also called symbolic language It is mostly used for writing system software. Assembly language is also known as second generation language. Assembly Languages: Assembly Languages different for each computer processor main proc pay mov ax, 4 mov bx, 3 add ax, bx Levels of Programming Languages: Levels of Programming Languages 2.High Level Languages A type of language that that is close to human languages is called high level language. High level languages are easy to understand Instruction of these languages are written in English-like words such as input, print etc. High level languages are further divided into following categories: 1.Procedural Languages 2.Object Oriented Languages 3.Database Query Languages High-Level Languages: High-Level Languages 1.Procedural Languages: Procedural Languages are also known as third generation languages or 3GL. In these languages program are written in predefined set of instructions Computer execute these instruction in the same sequence in which these instructions are written. Each instruction in this language tells the computer what to do and how to do. Procedural Languages: : Procedural Languages: Some procedural languages are as follows FORTRAN (Formula Translation) BASIC (Beginners All Purpose Symbolic Instruction Code) COBOL (Common Business Oriented Language) PASCAL C 2.Object Oriented Languages: 2.Object Oriented Languages OOP is a technique in which programs are written on basis of objects. An object is a collection of data and functions Object may represent a person ,place or things in real world. In OOP data and all possible functions on data are grouped together C++ and Java are two most popular object-oriented languages. Database Query Languages: Database Query Languages Database is collection of related data in an organized way Database query languages are used to retrieve ,insert ,delete or search data from database These are non-procedural languages Non-procedural languages are also called fourth generation languages or 4GL In non-Procedural languages user only needs to tell the computer what to do not how to do. Language Translator: Language Translator Language processor or translator is software that converts these programs into machine language Each computer language has its own translators. Different types of language translator are : compilers Interpreters Assembler Compiler : Compiler Translates the entire program to machine code before running it. A program written in high level language is called source program. The compiler convert source program in the machine code called object program. The object program can be executed many times. Compiler also checks syntax errors in the program A source program containing an error cannot be compiled Interpreter: Interpreter Translates instructions to machine code line-by-line or statement by statement. It executes statement before translating the next statement of the source program If there is an error in the statement , the interpreter stops working and displays an errors message. The disadvantage of interpreter is that it is not very efficient The interpreter does not produce an object code .it must convert the program each time it is executed . Visual Basic uses interpreter. Programmer’s Lingo: Programmer’s Lingo Interpreter Process Assembler: Assembler An assembler is a translating program that translates the instructions of a program written in assembly language into machine language . Assembly language program Object code Assembler Programmer’s Lingo: Programmer’s Lingo Program - detailed set of instructions for a computer Programming Language - tool used to create a program; defined by semantics and syntax Semantics - the meaning of words in a language Syntax - rules for combining symbols of a language Programmer’s Lingo: Programmer’s Lingo Source Code (code) - program you write using a programming language ASSIGNMENT: ASSIGNMENT Draw flowchart for calculating the average of 25 exam scores. OOPS!!!