Introduction to Computers and Programming - Chapter 1 PDF
Document Details
Uploaded by Deleted User
Tags
Summary
This document is an introduction to computer programming and provides a basic overview. It covers topics like why programming is important, different programming languages, and definitions of a program, variables, and more. The content is suitable for an introductory computer science course and focuses on the foundations of programming.
Full Transcript
Introduction to computers and programming Chapter 1 Why program? Computers can do many different jobs because they are programmable. Every profession has tools that make its job easier to do. Carpenters (hammers, saws, and measuring...
Introduction to computers and programming Chapter 1 Why program? Computers can do many different jobs because they are programmable. Every profession has tools that make its job easier to do. Carpenters (hammers, saws, and measuring tapes). Mechanics (wrenches, screwdrivers, and ratchets). Electronics technicians (probes, scopes, and meters) Surgeons (have tools designed specifically for surgical operations ) Computer is a tool that is used by so many professions that it cannot be easily categorized. What makes the computer so useful? It is a machine specifically designed to follow instructions. Because of the computer’s programmability, it doesn’t belong to any single profession. Computers are designed to do whatever task their programs, or software, tell them to do. Computer programmers They create software that transforms computers into the specialized tools of many trades. Without programmers, the users of computers would have no software, and without software, computers would not be able to do anything. Programmers must learn special languages like C++ because computers do not understand English or other human languages. Languages such as C++ have strict rules that must be carefully followed. Programming Languages A program is a set of instructions a computer follows in order to perform a task. A programming language is a special language used to write computer programs. What is a Program? Computers are designed to follow instructions. A computer program is a set of instructions that tells the computer how to solve a problem or perform a task. Algorithm Instructions are called an algorithm An algorithm is a set of well-defined steps for performing a task or solving a problem. Programming Languages There are two categories of programming languages: Low-Level and High-Level Low-Level Language A low-level language is close to the level of the computer, which means it resembles the numeric machine language of the computer more than the natural language of humans. High-Level Languages The easiest languages for people to learn are high- level languages. They are called “high-level” because they are closer to the level of human-readability than computer-readability. What is IDE? Integrated Development Environment. To write any program in C++ language you will need an editor. After that to convert that program into machine language, you will need a compiler. Source Code, Object Code, and Executable Code Source Code: The statements written by the programmer are called source code, and the file they are saved in is called the source file. Object Code The process of translating source file to machine language. If the program is free of syntax errors, the compiler stores the translated machine language instructions, which are called object code, in an object file. Executable Code Although an object file contains machine language instructions, it is not a complete program. Once the linker has finished with this step, an executable file is created. The executable file contains machine language instructions, or executable code, and is ready to run on the computer. What is a program made of ? All programming languages have a few things in common: Key Words Programmer-Defined Identifiers Operators Punctuation Syntax Key Words Words that have a special meaning Key words may only be used for their intended purpose. Key words are also known as reserved words. In C++, key words are written in all lowercase. cannot be used for anything other than their designated purposes ( int, double, using , cin ,cout…). Programmer-Defined Identifiers Words or names defined by the programmer. They are symbolic names that refer to variables or programming routines. Identifier: a C++ identifier consists of letters, digits, and the underscore character ( _ ) and must begin with a letter or underscore Operators Operators perform operations on one or more operands. An operand is usually a piece of data, like a number. Punctuation Punctuation characters that mark the beginning or ending of a statement, or separate items in a list. Like: # ; , There are rules that govern where semicolons are required and where they are not. Syntax Rules that must be followed when constructing a program. Syntax dictates how key words and operators may be used, and where punctuation symbols must appear. Variables A variable is a named storage location in the computer’s memory for holding a piece of information. The information stored in variables may change while the program is running. Programming process The programming process consists of several steps, which include design, creation, testing, and debugging activities. Designing and Creating a Program 1. Clearly define what the program is to do. 2. Visualize the program running on the computer. 3. Use design tools such as a hierarchy chart, flowcharts, or pseudo- code to create a model of the program. 4. Check the model for logical errors. 5. Type the code, save it, and compile it. 6. Correct any errors found during compilation. Repeat Steps 5 and 6 as many times as necessary. 7. Run the program with test data for input. 8. Correct any errors found while running the program. Repeat Steps 5 through 8 as many times as necessary. 9. Validate the results of the program. 1. Clearly define what the program is to do Example: Purpose To calculate the user’s gross pay. Input Number of hours worked, hourly pay rate. Process Multiply number of hours worked by hourly pay rate. The result is the user’s gross pay. Output Display a message indicating the user’s gross pay. 2. Visualize the program running on the computer Try to imagine what the computer screen looks like while the program is running. Example: How many hours did you work? 10 How much do you get paid per hour? 15 You have earned $150 3. Use design tools such as a hierarchy chart, flowcharts, or pseudo-code to create a model of the program Get payroll data. Calculate gross pay. Display gross pay. 4. Check the model for logical errors Logical errors are mistakes that cause the program to produce erroneous results. Once a hierarchy chart, flowchart, or pseudo-code model of the program is assembled, it should be checked for these errors. The programmer should trace through the charts or pseudo-code, checking the logic of each step. 5. Type the code, save it, and compile it The programmer saves the source code to a file, and begins the process of translating it to machine language. During this step the compiler will find any syntax errors that may exist in the program. 6. Correct any errors found during compilation If the compiler reports any errors, they must be corrected. Steps 5 and 6 must be repeated until the program is free of compile-time errors. 7. Run the program with test data for input A run-time error is an error that occurs while the program is running. These are usually logical errors, such as mathematical mistakes. Testing for run-time errors requires that the program be executed with sample data or sample input. If the program does not produce the correct output, a logical error is present in the program. 8. Correct any run-time errors found while running the program Desk-checking is a process that can help locate run-time errors. The term desk-checking means the programmer starts reading the program, or a portion of the program, and steps through each statement. 9. Validate the results of the program When you believe you have corrected all the run-time errors, enter test data and determine whether the program solves the original problem. Review Questions: 1. What is the difference between a high-level language and a low-level language? 2. Explain what is stored in a source file, an object file, and an executable file. 3. What is an integrated development environment? 4. Describe what a compiler does with a program’s source code. 5. Why must programs written in a high-level language be translated into machine language before they can be run? 6. Why is it easier to write a program in a high-level language than in machine language? 7. Explain the difference between an object file and an executable file. 8. What is the difference between a syntax error and a logical error? Questions ?