Starting Out with C++: From Control Structures through Objects, 8/E - Tony Gaddis Ray Devore's Notes on Chapter 1: Introduction to Computers and Programming CHAPTER 1 1.1 Introduction to Computers and Programming 1 Why Program? .................................................................................................................... 1 What is a program? A set of instructions Where do we have programs other than computers? What is a computer without a program? Dead weight What is a computer with a program? Just about anything What are some things that we use computers today? 1.2 Computer Systems: Hardware and Software ..................................................................... 2 What is the difference between computer hardware and software? Hardware you can touch, software you cannot touch. Hardware What are some examples of hardware? Hardware internal to the computer Input unit Output unit Memory unit Arithmetic Logic Unit (ALU) Central Processing Unit (CPU) Secondary Storage Unit (disk drive) More hardware (faster, more memory, more disk space) for less money Software What are some examples of software? There are two different types of software: Application software Operating systems Single task vs multitasking Single user vs multiple user How are hardware and software related? Page 1 of 6 Starting Out with C++: From Control Structures through Objects, 8/E - Tony Gaddis Ray Devore's Notes on Chapter 1: Introduction to Computers and Programming 1.3 Programs and Programming Languages ............................................................................. 8 What is a Program? Computers follow instructions Programs provide those instructions Instructions in the proper order create an algorithm Machine language is in bits and bytes – Not human readable Assembly language is detailed direction for what you want the computer to do (simple addition of two numbers takes multiple steps) – human readable, but only programmer understandable. Uses assembler to convert code to machine language. High level language uses more human understandable syntax. Equations look similar to basic algebra. May use an interpreter or a compiler to convert code to machine language. Basic uses an interpreter where each statement is converted to machine language as the program runs, every time it runs. C++ uses a compiler that converts the whole program into an executable machine language module, and uses the executable when it runs. Programming Languages A programming language allows a programmer to develop the set of instructions for the computer to carry out. You can write programs at various levels You can write in machine language (1s and 0s), which is the language the computer understands. You can write in machine language, but it is difficult for a person to understand. You can write in a low-level language, such as Assembler. Slightly better than 1s and 0s, but not much. Programmers used machine language and assembler to write the first operating systems and associated programs. Source Code, Object Code, and Executable Code Today, most programmers use high-level languages. These languages are easier for a person to understand, but must be translated in the machine language for the computer to understand. The high-level languages use two types of programs to translate the human readable language into machine language, either an interpreter or a compiler. Page 2 of 6 Starting Out with C++: From Control Structures through Objects, 8/E - Tony Gaddis Ray Devore's Notes on Chapter 1: Introduction to Computers and Programming An interpreter translates one statement at a time from the programming language to machine language, executes it, and then goes to the next. If you have code in a loop, every time the loop executes, the interpreter translates the code again. Every time the program runs, the interpreter translates the entire program again. This relates to how the interpreters at the UN work with real languages. Every time a person says a word, the interpreter translates that word. If the person says the same word 100 times, the translator translates it 100 times. If the person gives the same speech the next day, the translator translates it all over again. A compiler translates an entire program to machine code in one pass. If you have code in a loop, the compiler translates and stores the entire loop once. This relates more to how a translator would translate a manuscript or a book. Once the translator has translated the document, it does not need translated into that same language again. In this course, we will use a C++ compiler. When we compile our source code, the compiler actually goes through three separate processes. It runs a pre-processor that performs some actions and creates a modified source code. The modified source code from the pre-processor goes to a compiler that converts it to an object file in machine language. A linker combines this object file with other object files and libraries to create the final executable program. NOTE: Beginners All-purpose Symbolic Instruction Code is an after-nym for Basic. Basic is not an acronym. Basic is just the name of the language. 1.4 What Is a Program Made of? ............................................................................................ 14 Language Elements Every programming language has a set of language element. There are some elements common to all languages. Every language has Keywords, Operators, Punctuation, Programmer-defined identifiers, and Syntax. Some languages have directives that aide the language, but are not part of the core of the language. Lines and Statements A line of code takes one line. A statement is a completed action. You may write one statement on one line or multiple lines. On one line, you may write part of a statement, a full statement, or multiple statements. Usually, you will write one statement on one line. A key to writing maintainable code is readability. I will emphasize writing maintainable code throughout this course. Page 3 of 6 Starting Out with C++: From Control Structures through Objects, 8/E - Tony Gaddis Ray Devore's Notes on Chapter 1: Introduction to Computers and Programming While you may write multiple statements on one line, DON’T. Later in the course, we will discuss when you should write one statement on multiple lines. Variables A variable identifier is a name for a location in memory that you can modify. The system keeps track of the memory address for that location so you don’t have to. Variable Definitions To use a variable in a C++ program, you have to first define that variable. When you define a variable, you give it a name and tell the system what type of data will be stored in that location. There are two basic types of data: numbers and characters. C++ contains multiple numeric data types that we will discuss throughout the semester. There is only one character type, and that is char. There is a difference between a variable definition and a variable declaration. A variable definition allocates memory, a variable declaration states that the variable has been defined somewhere. A variable definition is a variable declaration. A variable declaration may not be a variable definition. When applicable, we will discuss the declaration that is not a definition. 1.5 Input, Processing, and Output .......................................................................................... 17 The three primary activities of a program are to receive input, process it, and provide output. Every program will perform these three activities. Discuss data vs information The number 14 is data. Saying there are 14 students in this class provides information. Information is data in context. 1.6 The Programming Process ................................................................................................ 18 Designing and Creating a Program Designing the program is what Program Logic and Design (ITSE 1329) was all about. The following are my modifications of the steps in the book. 1) Clearly define what the program is to do – If you don't know what the program is supposed to do, you cannot make it do it. Page 4 of 6 Starting Out with C++: From Control Structures through Objects, 8/E - Tony Gaddis Ray Devore's Notes on Chapter 1: Introduction to Computers and Programming 2) Visualize the program running on the computer – how would you like to see the program on the screen 3) Use design tools to create a model of the program – flow charts, pseudocode, screen layouts, report layouts 4) Check the model for logical errors – walk through the model on paper with some sample data 5) Develop and type the code – this can be done in any stand-alone text editor or using the text editor within an Integrated Development Environment (IDE) 6) Save the code – a C++ file will have a .cpp extension 7) Compile the code – compilation checks for syntax errors, and when there are none, creates an executable 8) Correct statements with syntax errors. Review statements with warnings and modify as necessary (more about warnings later). If corrections were made, return to step 7. 9) Run the program. Input of good test data and bad test data. 10) When run time errors are found, return to step 5 as necessary. 11) Validate results. If errors are found, return to step 5 as necessary. What is Software Engineering? Software engineering is the process of designing computer software. One method is using a Top-Down Design This method uses a Divide-and-Conquer approach Start with the big picture Break the big picture down into its major processes Break each major process down into successively smaller processes Continue until you have a set of processes where each process serves a single purpose. 1.7 Procedural and Object-Oriented Programming................................................................ 22 Procedural programming focuses on what is being done, the procedure. Object-Oriented programming focuses on what is doing the action, the object/actor. Introduction to C++ focuses on procedural programming, but uses some built in objects. Advanced C++ focuses on Object-Oriented programming, with you developing your own objects. We will start Object-Oriented programming in detail in week 4. Page 5 of 6 Starting Out with C++: From Control Structures through Objects, 8/E - Tony Gaddis Ray Devore's Notes on Chapter 1: Introduction to Computers and Programming Early programming start at the top and go to the bottom until you had everything coded. Structured Systems Analysis and Design Key: what was being done Actions / Verbs FUNCTIONS Cleaner Easier to test and debug Easier to modify Object-Oriented programming Key: What is taking the action Things / Nouns Still using functions, but the functions are related to what is taking the action, instead of just the action. Software Engineering Observation: There are extensive libraries of reusable components on the web, many at no charge. Page 6 of 6