Computer Programming -1Lecture 2 A program is a set of instructions that the computer follows to perform a task We start with an algorithm, which is a set of well-defined steps. 1-2 1-3 Although the previous algorithm defines the steps for calculating the Total Salary, it is not ready to be executed on the computer. The computer only executes machine language instructions. 1-4 Machine language instructions are binary numbers, such as 1011010000000101 Rather than writing programs in machine language, programmers use programming languages. Programming Language : An artificial language used to write instructions that can be translated into machine language and then executed by a computer. 1-5 BASIC C++ Ruby Java FORTRAN COBOL C 1-6 C# Visual Basic JavaScript Python Type of programming languages:. 1-low level language (machine language): The only language that the computer can understand and consists of long strings of ones and zeroes (1,0) 2-Assemply language: Contain the same instruction of machine language, but the instructions and variables have names instead of being just 0 and 1. such as: ADD and DIV 3-High level language: closely resemble human language. Example: c++ , cobol, pascal, java # Program written in high-level language are translated into machine language by compiler. a) Create file containing the program with a text editor. b) Run preprocessor to convert source file directives to source code program statements. c) Run compiler to convert source program into machine instructions. d) Run linker to connect hardware-specific code to machine instructions, producing an executable file. 1-9 Steps b – d are often performed by a single command or button click. Errors detected at any step will prevent execution of following steps. Source Code Preprocessor Modified Source Code Compiler 1-10 Object Code Linker Executable Code An integrated development environment, or IDE, combine all the tools needed to write, compile, and debug a program into a single software application. Examples are Microsoft Visual C++, Turbo C++ Explorer, CodeWarrior, etc. 1-11 1-12 Common elements in programming languages: 1-13 Key Words Programmer-Defined Identifiers Operators Punctuation Syntax 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 1-14 // This program calculates the user's pay. #include <iostream> using namespace std; int main() { double hours, rate, pay; // Get the number of hours worked. cout << "How many hours did you work? "; cin >> hours; // Get the hourly pay rate. cout << "How much do you get paid per hour? "; cin >> rate; // Calculate the pay. pay = hours * rate; // Display the pay. cout << "You have earned $" << pay << endl; return 0; } Also known as reserved words Have a special meaning in C++ Can not be used for any other purpose Key words in the Program 1-1: using, namespace, int, double, and return. 1-15 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 1-16 // This program calculates the user's pay. #include <iostream> using namespace std; int main() { double hours, rate, pay; // Get the number of hours worked. cout << "How many hours did you work? "; cin >> hours; // Get the hourly pay rate. cout << "How much do you get paid per hour? "; cin >> rate; // Calculate the pay. pay = hours * rate; } // Display the pay. cout << "You have earned $" << pay << endl; return 0; Names made up by the programmer Not part of the C++ language Used to represent various things: variables (memory locations), functions, etc. In Program 1-1: hours, rate, and pay. 1-17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 1-18 // This program calculates the user's pay. #include <iostream> using namespace std; int main() { double hours, rate, pay; // Get the number of hours worked. cout << "How many hours did you work? "; cin >> hours; // Get the hourly pay rate. cout << "How much do you get paid per hour? "; cin >> rate; // Calculate the pay. pay = hours * rate; // Display the pay. cout << "You have earned $" << pay << endl; return 0; } Used to perform operations on data Many types of operators: Arithmetic - ex: +,-,*,/ Assignment – ex: = Some operators in Program1-1: << >> = * 1-19 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 1-20 // This program calculates the user's pay. #include <iostream> using namespace std; int main() { double hours, rate, pay; // Get the number of hours worked. cout << "How many hours did you work? "; cin >> hours; // Get the hourly pay rate. cout << "How much do you get paid per hour? "; cin >> rate; // Calculate the pay. pay = hours * rate; // Display the pay. cout << "You have earned $" << pay << endl; return 0; } Characters that mark the end of a statement, or that separate items in a list In Program 1-1: , and ; 1-21 1-22 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // This program calculates the user's pay. #include <iostream> using namespace std; int main() { double hours, rate, pay; // Get the number of hours worked. cout << "How many hours did you work? "; cin >> hours; // Get the hourly pay rate. cout << "How much do you get paid per hour? "; cin >> rate; // Calculate the pay. pay = hours * rate; // Display the pay. cout << "You have earned $" << pay << endl; return 0; } The rules of grammar that must be followed when writing a program Controls the use of key words, operators, programmer- defined symbols, and punctuation 1-23 A variable is a named storage location in the computer’s memory for holding a piece of data. In Program 1-1 we used three variables: The hours variable was used to hold the hours worked The rate variable was used to hold the pay rate The pay variable was used to hold the gross pay 1-24 To create a variable in a program you must write a variable definition (also called a variable declaration) Here is the statement from Program 1-1 that defines the variables: double hours, rate, pay; 1-25 There are many different types of data, which you will learn about in this course. A variable holds a specific type of data. The variable definition specifies the type of data a variable can hold, and the variable name. 1-26 Once again, line 7 from Program 1-1: double hours, rate, pay; The word double specifies that the variables can hold double-precision floating point numbers. (You will learn more about that in Chapter 2) 1-27 programming methods (techniques): 1-procedural programming: A procedure is a set of instruction executed one after another. 2- structured programming: Any complex task is divided in to a set of smaller tasks which can be understand. 3-object oriented programming(OOP): programming methodology based on Classes and objects. program construction : Text editor : This is used to create program in c++ form, this is called source file (source files end with .cpp ) Compiler : translates source file into object file, the object file contain the instruction in way that the computer can understand (object file end with .exe) The source file in c++ (high-level language) The object file (low-level language)