Link to digital lesson Lesson 2 Programming Languages and Languages Processors www.ien.edu.sa A Brief History of the Development Programming Languages Many things have changed since the creation of the first computer to the present day. Computer components and technologies have evolved greatly, as have advanced processing capabilities. Despite this, the concepts of computer operation formulated by von Neumann in 1945 still apply. Fourth-Generation Language High-Level Language Assembly Language Machine Language Hardware وزارة التعليم Ministry of Education 28 ZU24-1446 Figure 1.8: Evolution of programming Programming languages were invented for the purpose of human-machine communication.

Lesson 2 Programming Languages and Languages Processors

A Brief History of the Development Programming Languages

Machine Language In order for a computer to perform any function required from it, it must be given the appropriate sequences in the form of binary numbers, consisting of Os and 1s. This "machine language" will not be understood by humans. Machine languages are very difficult for a programmer to apply. and implement due to the need for a deep knowledge of computer components and their structure, and especially since the machine language of each Central Processing Unit (CPU) is different. A machine language program is a sequence of binary bits, which are the instructions issued to the processor to carry out basic operations. Assembly Language Between machine language and high-level programming languages there is an intermediate language called assembly language, also called symbolic programming language. Assembly language is similar to machine language but is somewhat easier to program as it allows the programmer to replace numbers (0, 1) with symbols. Human comprehensible assembly language commands are converted into corresponding sequences of 0's and 1's for the computer to understand and execute. For example, in assembly language, the word ADD, followed by two numbers, is used for addition. This is easy to understand and memorize for humans, but must be translated into a series of bits in the computer to carry out the required operation. This translation process is carried out by a special program called the assembler. Assembly language commands are made up of symbolic segments that correspond to machine language commands. Challenges of Assembly Language • The use of assembly language makes it easier to program simple operations of unintelligible binary sequences, but it is nevertheless considered a low-level language. • The assembly language used varies depending on the architecture of each computer. • Assembly language does not provide commands to perform more complex functions than simple additions, multiplications and comparisons, forcing the programmer to write long and complex programs that are difficult to understand and debug. • A program cannot be transferred from one computer to another of different architecture. وزارة التعليم Ministry of Education 2024-1446 29

Lesson 2 Programming Languages and Languages Processors

Machine Language

Assembly Language

Challenges of Assembly Language

The following table shows a program with an addition written in a high-level programming language. and its equivalent in assembly and machine language for a computer with a 6502 8-bit CPU. The high-level language program can be used on most computers, while the assembly and machine translation will work only on a computer with the same CPU architecture. Table 1.1: Calculating an addition High-level language Machine language Assembly language sum = 0 LDA #0 10101001 00000000 STA sum 10000101 00000000 LDA sum 10100101 00000000 CLC 00011000 00000000 sum sum + 5 ADC #5 01101001 00000101 STA sum 10000101 00000000 LDA sum 10100101 00000000 print (sum) JSR print 00100000 11100001 High-Level Programming Languages The shortcomings of machine language and assembly language led to a concerted effort to achieve better human-machine communication, which resulted in the emergence of the first high-level programming language in the 1950s. High-level programming languages use programming commands which resemble human language. The resulting programs must be translated into machine language by the computer itself, using a special program called the translator. Compilers and interpreters are types of translators used for different types of programming languages. FORTRAN 1957 COBOL 1960 PASCAL PYTHON C# 1970 1991 2000 وزارة التعليم 30 Ministry of Education 2024-1446 1958 LISP 1964 BASIC 1972 PROLOG C 1995 JAVA JAVASCRIPT Figure 1.9: The evolution of programming languages

Lesson 2 Programming Languages and Languages Processors

The following table shows a program with an addition written

High-Level Programming Languages

The Evolution of High-Level Programming Languages The developer chooses the programming language that allows the application to be easily developed in a given environment to implement a software solution, but at the same time the developer also chooses the language based on their personal knowledge, skills and preferences. Each programming language has a unique set of reserved words (words that the language understands) and a syntax that the programmer uses to write instructions. Table 1.2: Basic information of programming languages Programming language Developer Etymology IBM FORTRAN FORmula TRANslation وزارة التعليم Ministry of Education 2024-1446 LISP MIT LIST Processor CODASYL Common Business COBOL Oriented Language Dartmouth College Beginner's All Purpose BASIC Professor Nicholas Wirth PASCAL Dennis Ritchie and Bell Labs C Sun Microsystems JAVA Properties Suitable for solving mathematical and scientific problems, but not suitable for managing data files, for example. A language for artificial intelligence. Suitable for developing commercial applications and general management applications. Symbolic Instruction Code Named after the mathematician Blaise Pascal The C language is named after a prior language named B Named after a type of coffee (Java) A multi-domain programming language. It is famous for introducing structured programming techniques. It adopts program design in a systematic and accurate manner. It is used for UNIX operating system development, and is suitable for different operating systems. It is an Object Oriented programming language used to develop applications that can run on a wide range of computers or different operating systems. 31

Lesson 2 Programming Languages and Languages Processors

The Evolution of High-Level Programming Languages

Table 1.2: Basic information of programming languages

Features of High-Level Programming Languages: High-level programming languages have several advantages over assembly language: • They use logic and programming formulas that are understandable and close to human language. • They are independent of the type of computer, and can be used on any device with or without minor modifications. Developers can learn high-level programming languages more easily and quickly. • Software debugging and maintenance is much easier. In general, high-level programming languages reduce the time and cost of software development significantly compared to low-level programming languages. Fourth-Generation Programming Languages Among high-level programming languages, we note that there are so-called fourth-generation programming languages, which are usually abbreviated as 4GL. Fourth-generation programming languages are closer to human language than other high-level languages and are accessible to people without formal training as programmers because they require less coding. Fourth-generation languages are more programmer-friendly and enhance programming efficiency by using English-like words and phrases, as well as icons, symbolic representations, and graphical interfaces when needed. The key to achieving efficiency with 4GL is compatibility between the tool and the field of application. Computer users in fourth-generation languages can make changes to the program in order to meet a new need and have the ability to solve small problems by themselves. Multiple joint operations can be performed using a single command entered by the programmer. Scripting languages are a type of programming language typically interpreted rather than compiled. They are used to automate repetitive tasks, simplify complex operations, and enable rapid prototyping of software systems. Some common examples of scripting languages include JavaScript, Ruby, PHP and Perl. They often feature rich libraries, and a focus on productivity, making them well-suited for tasks that require rapid prototyping and iteration. However, they may not be as efficient or scalable as compiled languages and may not be suitable for performance-critical or resource-intensive applications. For data operations, a user can create queries and reports using SQL for statistics and scientific projects, and a mathematician or researcher can use software such as SPSS, MATLAB, and LabVIEW to analyze this data. وزارة التعليم Ministry of Education 32 ZU24-1446

Lesson 2 Programming Languages and Languages Processors

Features of High-Level Programming Languages:

Fourth-Generation Programming Languages

Classifications of Programming Languages There are many classifications of programming languages. Languages can be classified in terms of the type of commands used, such as procedural programming languages and object-oriented programming languages. Procedural programming uses a set of instructions to tell the computer what to do step by step. Examples of procedural programming languages are COBOL, Fortran, and C. In object-oriented programming, the program is divided into units called objects. Examples of object-oriented programming languages are C#, C++, Java and Python. Programming languages can also be classified according to what they are used for: 1. General programming languages: In theory, any general programming language can be used to solve any problem, but in practice, each language is designed to solve a specific type of problem. These languages are divided as follows: Science-oriented languages such as Fortran. • Business-oriented languages such as COBOL. • Multi-domain languages such as BASIC and Pascal. • Operating system programming languages such as C. • Artificial intelligence languages such as PROLOG. • Specialized database management languages such as SQL. 2. Specialized languages such as LISP are used for a specific type of application such as robotics or integrated circuits. How Computers Understand Programming Languages Any program written in any programming language is converted into machine language that can be understood and executed by a computer, through the use of special translation programs. There are two ways to run programs written in a high-level language. The most common is to compile the program with a compiler but in some programming languages an interpreter is used instead. Let's go over how to implement these two different methods. Compiler A compiler is a computer program that converts an entire block of code written in a high-level programming language into machine language, which is understood by the computer's processor. Interpreter An interpreter is a computer program that converts each line of code from a block of code written in a high-level language into machine language and sends it for execution directly, before moving on to the next line of code. وزارة التعليم Ministry of Education 2024-1446 33

Lesson 2 Programming Languages and Languages Processors

Classifications of Programming Languages

Programming languages can also be classified according to what they are used for:

Compiler

Interpreter

How Computers Understand Programming Languages

Program translation and linking process: • The compiler accepts a program written in a high-level language as the input file (the source code), and produces an equivalent machine language program called object code. • The compiler cannot compile statements that refer to standard libraries or resources outside of the source code, so the process will require an additional step of linking and converting those statements. • Another program called the Linker or Loader handles the linking process and links the object code file with the standard library files, and produces the executable code, which is the final program that the computer executes. وزارة التعليم Ministry of Education 34 2024-1446 Python Source code x = 10 y=x+3 print(y) Compiler Translation Loading Link all commands together Target machine code Figure 1.10: The process of compiling and executing a program using a compiler The source code is the program written in a high level programming language.

Lesson 2 Programming Languages and Languages Processors

Program translation and linking process:

Figure 1.10: The process of compiling and executing a program using a compiler

Source Code if a<b Lib ref do while z=x-y Lib ref Compiler Object Code 11011001 Lib ref 00010111 10101011 Python Source code Linker Executable Code 11011001 01000100 6 00010111 10101011 11111100 Lib ref Library files 11011001 01000100 00010111 10101011 11111100 Figure 1.11: Stages of translating and linking the program Interpreter x = 10 Analysis - Check for errors Send command 1 for execution y=x+2 print(y) Analysis - Check for errors Send command 2 for execution Analysis - Check for errors Send command n for execution Figure 1.12: The process of compiling and executing the program using an interpreter Compilers and interpreters perform the same task, which is to convert the program written in the Phigh-level programming language into machine language, but in two different ways. Ministry of Education 2024-1446 35

Lesson 2 Programming Languages and Languages Processors

Figure 1.11: Stages of translating and linking the program

Figure 1.12: The process of compiling and executing the program using an interpreter

Interpreted and Compiled Programming Languages Most modern programming languages use compilers to produce optimized code quickly, but there are some languages that still use interpreters when there is a need to create a simple program for which speed is not the primary concern. Compiled Languages The C, C++, C#, and Java programming languages use a compiler to build fast, reliable programs. The executable code is created for each type of computer hardware, which means that developers have to know what the end user's computer hardware is. Interpreted Languages Legacy JavaScript, LISP, and BASIC use interpreters, which means that programs run slowly but their source code can run on any computer that has a particular programming language's interpreter. For example, a web application written in JavaScript can run on a Windows computer or an Android tablet with a web browser that integrates the JavaScript interpreter. وزارة التعليم Ministry of Education 36 2024-1446 Interpreter Compiler C LISP BASIC JavaScript C++ Python C# Java Figure 1.13: Python is an interpreted and a compiled language Python is both an interpreted and a compiled language. The Python application compiles each line of code so that it can be read by the interpreter on the hardware in use. The syntax used by the programmer doesn't change because the Python application converts it into the correct form for the interpreter used on that hardware.

Lesson 2 Programming Languages and Languages Processors

Interpreted and Compiled Programming Languages

Interpreted Languages

Python is both an interpreted and a compiled language.

Table 1.3: Compiler vs Interpreter comparison Main Function Input Output Memory Implementation process Error checking Link files Speed Dependence on hardware and operating systems Compiler Converts the entire source code written in a high-level programming language into machine language, and produces an executable program. The compiler takes the entire source code as input. The compiler generates and stores an object code file as output. Requires more memory due to object code generation. The compilation process takes place for the entire source code before execution begins. The compiler displays all language errors and warnings when compiling the program. You cannot run the program until all errors are corrected. Needs a program to associate the object file with standard library files to create the executable. Availability of .exe file makes execution faster. The executable file generated by the compiler depends on the target hardware. It cannot run on different CPU architectures or different operating systems. Interpreter Converts the block of code into machine language so that it translates and then executes the block of code, moving to the next block while the program is running. The interpreter takes one of the source code instructions as input each time. The interpreter does not generate an object code file. Less memory is required. The interpretation process for each code statement takes place in parallel with the execution process. The interpreter reads one line of code and displays any errors in it. This error must be corrected before moving on to the next line. Does not need the link process, and does not create an executable file. The execution process is slower because the executable file is not available. The program is interpreted again on each execution. The interpreter is hardware and operating system independent. For example, a Python interpreter can run on Windows and Linux with the same source code and give the same results. وزارة التعليم Ministry of Education 2024-1446 37

Lesson 2 Programming Languages and Languages Processors

Table 1.3: Compiler vs Interpreter comparison

Dealing with Software Errors Compilers and interpreters operate differently when they face errors and bugs in the source code. Compiler: 1. Program creation. 2. The compiler will analyze and process all lines of code and make sure that they are correct. 3. If there is an error, an error message will appear. 4. If there is no error, the compiler will convert the source code into machine language. Multiple code files will be associated with a single executable program (known as an EXE file). Interpreter: 1. Program creation. 2. The interpreter reads one line of code and displays any syntax error. This error must be corrected before moving on to the next line. 3. All the lines of the source code code are executed line by line during program execution by the interpreter. Debugging Process The source code in its first version may often contain many errors, which are divided into three types: • Logical errors: Errors in the logic of the program. • Runtime errors: Errors that occur during the execution of the program. • Syntax errors: Errors in the syntax of the code. Logical errors and runtime errors only occur when the program is executed, while syntax errors occur during compilation. The program is executed only if the source program contains no syntax errors. وزارة التعليم Ministry of Education 38 2024-1446 Debugging Syntax Errors: • In the first step, the compiler or interpreter detects syntax errors and presents messages indicating the error and its location. Some of them can specify the cause of the error. • The next step is to correct errors in the program. Finally, the corrected program compiles correctly, without any error messages.

Lesson 2 Programming Languages and Languages Processors

Dealing with Software Errors

Correction of Errors During the Debugging Process

Debugging Syntax Errors:

1 2 Exercises What are the shortcomings of assembly language? Draw a diagram to show the difference between the process of translating and implementing code in a compiler and in an interpreter. وزارة التعليم Ministry of Education 2024-1446 39

Lesson 2 Programming Languages and Languages Processors

What are the shortcomings of assembly language?

Draw a diagram to show the difference between the process of translating and implementing code in a compiler and in an interpreter.

3 4 Write three advantages of high-level programming languages. Relate each programming language to the classification it belongs to. وزارة التعليم Ministry of Education 40 2024-1446 Python 1 BASIC 2 PROLOG 3 S+ 4 О Object-oriented languages Artificial intelligence languages Multi-domain languages Systems programming languages

Lesson 2 Programming Languages and Languages Processors

Write three advantages of high-level programming languages.

Relate each programming language to the classification it belongs to.

5 Choose the appropriate word or phrase to complete the sentences (Not all options. apply to a blank line): link memory syntax errors interpreted languages standard libraries object code runtime errors the compiler the source code 1. accepts the source program as input, and produces an equivalent machine language program called 2. The 3. Using execution is slower. used by the interpreter is less than that used by the compiler. is an advantage in terms of real-time debugging, but program 4. The compiler cannot convert statements that refer to concatenate and convert those statements. 5. The executable can be created if there are no 6. Errors that occur during program execution are called وزارة التعليم Ministry of Education 2024-1446 so it needs to in the source program. 41

Lesson 2 Programming Languages and Languages Processors

Choose the appropriate word or phrase to complete the sentences (Not all options apply to a blank line