PROG0101 Fundamentals of Programming PDF
Document Details
Uploaded by DesirousSunflower9977
FTMS College
null
null
Tags
Summary
This document is an overview of programming basics, discussing various programming languages and their applications. It covers topics including, but not limited to, programming languages, programming processes, and algorithms. The document provides a general introduction to topics within computer programming.
Full Transcript
PROG0101 Fundamentals of Programming ITCS2023 Programming Basics 1 PROG0101 Fundamentals of Programming Programmi ng Computer LanguagesProgram A program is a set of instructions following the rules...
PROG0101 Fundamentals of Programming ITCS2023 Programming Basics 1 PROG0101 Fundamentals of Programming Programmi ng Computer LanguagesProgram A program is a set of instructions following the rules of the chosen language. Without programs, computers are useless. A program is like a recipe. It contains a list of ingredients (called variables) and a list of directions (called statements) that tell the computer what to do with the variables. 2 PROG0101 Fundamentals of Programming Programmi ng Programming Languages Language A vocabulary and set of grammatical rules (syntax) for instructing a computer to perform specific tasks. Programming languages can be used to create computer programs. The term programming language usually refers to high-level languages, such as BASIC, C, C++, COBOL, FORTRAN, Ada, and Pascal. 3 PROG0101 Fundamentals of Programming Programmi ng Programming Languages Language You eventually need to convert your program into machine language so that the computer can understand it. There are two ways to do this: – Compile the program – Interpret the program 4 PROG0101 Fundamentals of Programming Programmi ng Programming Languages Language Compile is to transform a program written in a high- level programming language from source code into object code. This can be done by using a tool called compiler. A compiler reads the whole source code and translates it into a complete machine code program to perform the required tasks which is output as a new file. 5 PROG0101 Fundamentals of Programming Programmi ng Programming Languages Language Interpreter is a program that executes instructions written in a high-level language. An interpreter reads the source code one instruction or line at a time, converts this line into machine code and executes it. 6 PROG0101 Fundamentals of Programming Programmi ng Computer LanguagesProgramming syntax Computer programming is the process of writing, testing, debugging/troubleshooting, and maintaining the source code of computer programs. This source code is written in a programming language like C++, JAVA, Python etc. testing work correctly or not problem solution X+Y logic semantics syntax error 7 PROG0101 Fundamentals of Programming Programmi ng Computer LanguagesProgrammer A programmer is someone who writes computer program. Computer programmers write, test, and maintain programs or software that tell the computer what to do. 8 PROG0101 Fundamentals of Programming Programmi ng What Skills are Required to Become a Languages Programmer? Programming - Writing computer programs for various purposes. Writing - Communicating effectively with others in writing as indicated by the needs of the audience. Reading Comprehension - Understanding written sentences and paragraphs in work-related documents. Critical Thinking - Using logic and analysis to identify the strengths and weaknesses of different approaches. 9 PROG0101 Fundamentals of Programming Programmi ng What Skills are Required to Become a Languages Programmer? Computers and Electronics - Knowledge of electric circuit boards, processors, chips, and computer hardware and software, including applications and programming. Mathematics - Knowledge of numbers, their operations, and interrelationships including arithmetic, algebra, geometry, calculus, statistics, and their applications. Oral Expression - The ability to communicate information and ideas in speaking so others will understand. 10 PROG0101 Fundamentals of Programming Programmi ng What Skills are Required to Become a Languages Programmer? Oral Comprehension - The ability to listen to and understand information and ideas presented through spoken words and sentences. Written Expression - The ability to communicate information and ideas in writing so others will understand. Written Comprehension - The ability to read and understand information and ideas presented in writing. 11 PROG0101 Fundamentals of Programming Programmi ng What Skills are Required to Become a Languages Programmer? Deductive Reasoning - The ability to apply general rules to specific problems to come up with logical answers. It involves deciding if an answer makes sense. Information Organization - Finding ways to structure or classify multiple pieces of information. 12 PROG0101 Fundamentals of Programming Programmi ng Generations Languages of Programming Language The first generation languages, or 1GL, are low- level languages that are machine language. The second generation languages, or 2GL, are also low-level languages that generally consist of assembly languages. The third generation languages, or 3GL, are high- level languages such as C. 13 PROG0101 Fundamentals of Programming Programmi ng Generations Languages of Programming Language The fourth generation languages, or 4GL, are languages that consist of statements similar to statements in a human language. Fourth generation languages are commonly used in database programming and scripts or Artificial intelligence (AI) The fifth generation languages, or 5GL, are programming languages that contain visual tools to help develop a program. A good example of a fifth generation language is Visual Studio. 14 PROG0101 Fundamentals of Programming Programmi ng Types of Programming Language Languages There are three types of programming language: – Machine language (Low-level language) – Assembly language (Low-level language) – High-level language Low-level languages are closer to the language used by a computer, while high-level languages are closer to human languages. 15 PROG0101 Fundamentals of Programming Programmi ng Machine Language Languages Machine language is a collection of binary digits or bits that the computer reads and interprets. Machine languages are the only languages understood by computers. While easily understood by computers, machine languages are almost impossible for humans to use because they consist entirely of numbers. 16 PROG0101 Fundamentals of Programming Programmi ng Machine Language Languages Machine Language 1101010100001111101010101010100101 High level language 5 FOR I=1 TO 1000: PRINT "A";: NEXT I 17 PROG0101 Fundamentals of Programming Programmi ng Assembly LanguagesLanguage A program written in assembly language consists of a series of instructions mnemonics that correspond to a stream of executable instructions, when translated by an assembler, that can be loaded into memory and executed. Assembly languages use keywords and symbols, much like English, to form a programming language but at the same time introduce a new problem. 18 PROG0101 Fundamentals of Programming Programmi ng Assembly LanguagesLanguage The problem is that the computer doesn't understand the assembly code, so we need a way to convert it to machine code, which the computer does understand. Assembly language programs are translated into machine language by a program called an assembler. 19 PROG0101 Fundamentals of Programming Programmi ng Assembly LanguagesLanguage Example: – Machine language : 10110000 01100001 – Assembly language : mov a1, #061h – Meaning: Move the hexadecimal value 61 (97 decimal) into the processor register named "a1". 20 PROG0101 Fundamentals of Programming Programmi ng High Level Language Languages High-level languages allow us to write computer code using instructions resembling everyday spoken language (for example: print, if, while) which are then translated into machine language to be executed. Programs written in a high-level language need to be translated into machine language before they can be executed. Some programming languages use a compiler to perform this translation and others use an interpreter. 21 PROG0101 Fundamentals of Programming Programmi ng High-Level Languages Language Examples of High-level Language: ADA C C++ JAVA Python task : top 10 programming languages 2024 - fields 22 PROG0101 Fundamentals of Programming Overview of Common ComputersFeatures of All Program and Logic All programs could be structured in the following four ways: Sequences of instructions Branches Loops Modules 23 PROG0101 Fundamentals of Programming Overview of Common Computers Features of All Program and Logic Sequences of Instructions The program flows from one step to the next in strict sequence. 24 PROG0101 Fundamentals of Programming Overview of Common ComputersFeatures of All Program and Logic Branches The program reaches a decision point and if the result of the test is true then the program performs the instructions in Path 1, and if false it performs the actions in Path 2 25 PROG0101 Fundamentals of Programming Overview of Common ComputersFeatures of All Program and Logic Loops The program steps are repeated continuously until some test condition is reached, at which point control then flows past the loop into the next piece of program logic 26 PROG0101 Fundamentals of Programming Overview of Common ComputersFeatures of All Program and Logic Modules The program performs an identical sequence of actions several times. For convenience these common actions are placed in a module, which is a kind of mini-program which can be executed from within the main program. 27 PROG0101 Fundamentals of Programming Overview of Common ComputersFeatures of All Program and Logic Modules 28 PROG0101 Fundamentals of Programming Overview of Common ComputersFeatures of All Program and Logic Along with these structures programs also need a few more features to make them useful: Data (we take a closer look at data in the Raw Materials topic) Operations (add, subtract, compare etc) Input/Output capability (e.g. to display results) 29 PROG0101 Fundamentals of Programming Overview of The Programming Process Computers andStep 1: Defining the problem. Logic Step 2: Planning the solution. Step 3: Code the program. Step 4: Test the program. Step 5: Document everything. 30 PROG0101 Fundamentals of Programming Overview of The Programming Process Computers and Logic Step 1: Defining the problem The task of defining the problem consists of identifying what it is you know (input-given data), and what it is you want to obtain (output-the result). Eventually, you produce a written agreement that, among other things, specifies the kind of input, processing, and output required. 31 PROG0101 Fundamentals of Programming Overview of The Programming Process Computers and Logic Step 1: Defining the problem Example: What must the program do? What outputs are required and in what form? What inputs are available and in what form? 32 PROG0101 Fundamentals of Programming Overview of The Programming Process Computers and Logic Step 2: Planning the solution Two common ways of planning the solution to a problem are to draw a flowchart and to write pseudocode, or possibly both. A flowchart is a pictorial representation of a step-by- step solution to a problem. It consists of arrows representing the direction the program takes and boxes and other symbols representing actions. It is a map of what your program is going to do and how it is going to do it. 33 PROG0101 Fundamentals of Programming Overview of The Programming Process Computers and Logic Step 2: Planning the solution Pseudocode is an English-like nonstandard language that lets you state your solution with more precision than you can in plain English but with less precision than is required when using a formal programming language. Pseudocode permits you to focus on the program logic without having to be concerned just yet about the precise syntax of a particular programming language. 34 PROG0101 Fundamentals of Programming Overview of The Programming Process Computers and Logic Step 3: Code the program You will translate the logic from the flowchart or pseudocode-or some other tool-to a programming language. Program Coding means expressing the algorithm developed for solving a problem, in a programming language. 35 PROG0101 Fundamentals of Programming Overview of The Programming Process Computers and Logic Step 4: Test the program Almost all programs may contain a few errors, or bugs. Testing is necessary to find out if the program produces a correct result. Usually it is performed with sample data Debugging is the process of locating and removing errors 36 PROG0101 Fundamentals of Programming Overview of The Programming Process Computers and Logic Step 4: Test the program Types of Error Syntax Errors: Violation of syntactic rules in a Programming Language generates syntax errors. Effect? Interpreter or Compiler finds it in Syntax Check Phase. 37 PROG0101 Fundamentals of Programming Overview of The Programming Process Computers and Logic Step 4: Test the program Types of Error Semantic Errors: Doing logical mistakes causes semantic errors in Source code. Effect? Interpreters and Compilers can not notice them, but on execution, they causes unexpected results. 38 PROG0101 Fundamentals of Programming Overview of The Programming Process Computers and Logic Step 4: Test the program Types of Error Run-time Errors: Occur on program execution. Mostly caused by invalid data entry or tries to use not existing resources. Effect? It occurs on run time and may crash the program execution 39 PROG0101 Fundamentals of Programming Overview of The Programming Process Computers and Logic Step 5: Document everything Documentation is a written detailed description of the programming cycle and specific facts about the program. Typical program documentation materials include the origin and nature of the problem, a brief narrative description of the program, logic tools such as flowcharts and pseudocode, data-record descriptions, program listings, and testing results. Comments in the program itself are also considered an essential part of documentation. 40 PROG0101 Fundamentals of Programming Algorithms Introduction to Algorithms A sequence of instructions. A procedure or formula for solving a problem. It was created mathematician, Mohammed ibn-Musa al-Khwarizmi. Often used for calculation, data processing and programming. Algorithms can be expressed in any language. 4 PROG0101 Fundamentals of Programming Algorithms Introduction to Algorithms Algorithms for making things will often be divided into sections; – the parts/components/ingredients (inputs) required to accomplish the task – actions/steps/methods (processing) to produce the required outcome (output). For example to build a model car, the parts (inputs) are needed plus instructions on how to assemble the car (processing) and the result is the car (output). 4 PROG0101 Fundamentals of Programming Algorithms Pseudocode Pseudocode (which means fake code, because its not really programming code) specifies the steps required to accomplish the task. Pseudocode is a type of structured English that is used to specify an algorithm. Pseudocode cannot be compiled nor executed, and there are no real formatting or syntax rules. 4 PROG0101 Fundamentals of Programming Algorithms Advantages of Pseudocode Reduced complexity. Increased flexibility. Ease of understanding. 4 PROG0101 Fundamentals of Programming Algorithms How to Write Pseudocode Statements? There are six basic computer operations 1. A computer can receive information Read (information from a file) Get (information from the keyboard) 2. A computer can put out information Write (information to a file) Display (information to the screen) 45 PROG0101 Fundamentals of Programming Algorithms How to Write Pseudocode Statements? There are six basic computer operations 3. A computer can perform arithmetic Use actual mathematical symbols or the words for the symbols Example: Add number to total Total = total + number +, -, *, / Calculate, Compute also used 46 PROG0101 Fundamentals of Programming Algorithms How to Write Pseudocode Statements? There are six basic computer operations 4. A computer can assign a value to a piece of data 3 cases i. to give data an initial value Initialize, Set 47 PROG0101 Fundamentals of Programming Algorithms How to Write Pseudocode Statements? There are six basic computer operations 4. A computer can assign a value to a piece of data 3 cases ii. to assign a value as a result of some processing „=‟ x=5+y 48 PROG0101 Fundamentals of Programming Algorithms How to Write Pseudocode Statements? There are six basic computer operations 4. A computer can assign a value to a piece of data 3 cases iii. to keep a piece of information for later use Save, Store 49 PROG0101 Fundamentals of Programming Algorithms How to Write Pseudocode Statements? There are six basic computer operations 5. A computer can compare two piece of information and select one of two alternative actions IF condition THEN some action ELSE alternative action ENDIF 50 PROG0101 Fundamentals of Programming Algorithms How to Write Pseudocode Statements? There are six basic computer operations 6. A computer can repeat a group of actions WHILE condition (is true) some action ENDWHILE FOR a number of times some action ENDFOR 51 PROG0101 Fundamentals of Programming Algorithms Example 1: Finding average of any three numbers. Step 1 Start Step 2 Read values of X, Y, Z Step 3 S=X+Y+Z Step 4 A=S/3 Step 5 Write value of A Step 6 Stop 52 PROG0101 Fundamentals of Programming Algorithms Example 2: Finding square and cube. Step 1 Start Step 2 Read value of N Step 3 S=N*N Step 4 C=S*N Step 5 Write values of S, C Step 6 Stop 53 PROG0101 Fundamentals of Programming Algorithms Example 3: Finding biggest of two numbers. Step 1 Start Step 2 Read A, B Step 3 If A > B, then BIG = A, otherwise BIG = B Step 4 Write BIG Step 5 Stop 54 PROG0101 Fundamentals of Programming Algorithms Example 4: Calculate pay. Step 1 Start Step 2 Input hours Step 3 Input rate Step 4 pay = hours * rate Step 5 Print pay Step 6 End 55 PROG0101 Fundamentals of Programming Algorithms Exercise: Write a Pseudocode for these problems. 1. S = (A + B + C) / Y 2. Convert from Celsius to Fahrenheit. (Multiply by 9, then divide by 5, then add 32 ) 3. Area of Circle (r2) 4. Volume of Sphere ( 4 r3) 3 5. Average speed = Distance traveled Time taken 56 PROG0101 Fundamentals of Programming Algorithms Flowchart A flowchart is a graphical representation of an algorithm. These flowcharts play a vital role in the programming of a problem and are quite helpful in understanding the logic of complicated and lengthy problems. Once the flowchart is drawn, it becomes easy to write the program in any high level language. 57 PROG0101 Fundamentals of Programming Algorithms Flowchart A flowchart can therefore be used to: – Define and analyze processes – Build a step-by-step picture of the process for analysis, discussion, or communication – Define, standardize or find areas for improvement in a process 58 PROG0101 Fundamentals of Programming Algorithms Flowchart Symbols Start and end symbols Represented as lozenges, ovals or rounded rectangles Usually containing the word "Start" or "End", or another phrase signalling the start or end of a process, such as "submit enquiry" or "receive product". 59 PROG0101 Fundamentals of Programming Algorithms Flowchart Symbols Arrows Showing what's called "flow of control" in computer science. An arrow coming from one symbol and ending at another symbol. Represents that control passes to the symbol the arrow points to. 60 PROG0101 Fundamentals of Programming Algorithms Flowchart Symbols Processing steps Represented as rectangles. Examples: “Add 1 to X"; "replace identified part"; "save changes" or similar. 61 PROG0101 Fundamentals of Programming Algorithms Flowchart Symbols Input/Output Represented as a parallelogram. Examples: Get X from the user; display X. 62 PROG0101 Fundamentals of Programming Algorithms Flowchart Symbols Conditional or decision Represented as a diamond (rhombus). These typically contain a Yes/No question or True/False test. 63 PROG0101 Fundamentals of Programming Algorithms Advantages of Using Flowcharts Communication: Flowcharts are better way of communicating the logic of a system to all concerned. Effective analysis: With the help of flowchart, problem can be analysed in more effective way. Proper documentation: Program flowcharts serve as a good program documentation, which is needed for various purposes. Efficient Coding: The flowcharts act as a guide or blueprint during the systems analysis and program development phase. 64 PROG0101 Fundamentals of Programming Algorithms Advantages of Using Flowcharts Proper Debugging: The flowchart helps in debugging process. Efficient Program Maintenance: The maintenance of operating program becomes easy with the help of flowchart. It helps the programmer to put efforts more efficiently on that part. 65 PROG0101 Fundamentals of Programming Algorithms Example 1: Algorithm: Input: two numbers x and y Output: the average of x and y Steps: 1. input x 2. input y 3. sum = x + y 4. average = sum /2 5. output average 66 PROG0101 Fundamentals of Programming Algorithms Example 1: 67 PROG0101 Fundamentals of Programming Algorithms Example 2: Draw a flowchart to find the largest of three numbers A, B, and C. 68 PROG0101 Fundamentals of Programming Algorithms Example 2: No 69