Week 4-Programing language-Lecture 3(1).pdf

Full Transcript

ALGORITHMS AND PROGRAMMING LANGUAGES 2021-2022 Computer Science and Health Informatics Learning Objectives  Define the concept of programming.  Explain the link between programming and algorithms.  Differentiate between machine...

ALGORITHMS AND PROGRAMMING LANGUAGES 2021-2022 Computer Science and Health Informatics Learning Objectives  Define the concept of programming.  Explain the link between programming and algorithms.  Differentiate between machine language, assembly language, and high-level language.  Differentiate between the two standard approaches in program translation: interpretation and compilation. PRESENTATION TITLE 2 Algorithms and Programming ‫اﻟﺨﻮارزﻣﯿﺎت واﻟﺒﺮﻣﺠﺔ‬  Programming is all about designing and coding algorithms for solving problems. ‫ﺗﺪور اﻟﺒﺮﻣﺠﺔ ﺣﻮل ﺗﺼﻤﯿﻢ وﺗﺮﻣﯿﺰ اﻟﺨﻮارزﻣﯿﺎت ﻟﺤﻞ اﻟﻤﺸﺎﻛﻞ‬  The intended executor is the computer or a program executing on that computer. ‫اﻟﻤﻨﻔﺬ اﻟﻤﻘﺼﻮد ھﻮ اﻟﻜﻤﺒﯿﻮﺗﺮ أو اﻟﺒﺮﻧﺎﻣﺞ اﻟﺬي ﯾﺘﻢ ﺗﻨﻔﯿﺬه ﻋﻠﻰ ھﺬا اﻟﻜﻤﺒﯿﻮﺗﺮ‬  Instructions are written in programming languages which are more constrained and exact than human languages. ‫ﺗﺘﻢ ﻛﺘﺎﺑﺔ اﻟﺘﻌﻠﯿﻤﺎت ﺑﻠﻐﺎت اﻟﺒﺮﻣﺠﺔ اﻟﺘﻲ ھﻲ أﻛﺜﺮ ﺗﻘﯿﯿﺪا ودﻗﺔ ﻣﻦ اﻟﻠﻐﺎت اﻟﺒﺸﺮﯾﺔ‬ 3 Programming hardware‫ ﻟﻠـ‬assembly language ‫ﯾﺘﺮﺟﻢ‬ 4 Machine Language (0,1)  The first programming languages were known as machine languages..‫ﻛﺎﻧﺖ ﻟﻐﺎت اﻟﺒﺮﻣﺠﺔ اﻷوﻟﻰ ﺗﻌﺮف ﺑﺎﺳﻢ ﻟﻐﺎت اﻵﻻت‬ ‫ﺗﺘﻜﻮن ﻟﻐﺔ اﻵﻟﺔ ﻣﻦ ﺗﻌﻠﯿﻤﺎت‬  A machine language consists of instructions that correspond ‫ﺗﺘﻮاﻓﻖ ﻣﺒﺎﺷﺮة ﻣﻊ ﻋﻤﻠﯿﺎت‬ :‫اﻷﺟﮭﺰة ﻟﺠﮭﺎز ﻣﻌﯿﻦ‬ directly to the hardware operations of a particular machine:  i.e., Instructions deal directly with the computer’s physical components including main memory, registers, memory cells in CPU. ‫أي أن اﻟﺘﻌﻠﯿﻤﺎت ﺗﺘﻌﺎﻣﻞ ﻣﺒﺎﺷﺮة ﻣﻊ اﻟﻤﻜﻮﻧﺎت اﻟﻤﺎدﯾﺔ ﻟﻠﻜﻤﺒﯿﻮﺗﺮ ﺑﻤﺎ ﻓﻲ ذﻟﻚ اﻟﺬاﻛﺮة‬.‫اﻟﺮﺋﯿﺴﯿﺔ واﻟﺴﺠﻼت وﺧﻼﯾﺎ اﻟﺬاﻛﺮة ﻓﻲ وﺣﺪة اﻟﻤﻌﺎﻟﺠﺔ اﻟﻤﺮﻛﺰﯾﺔ‬  - Very low level of abstraction..‫ﻣﺴﺘﻮى ﻣﻨﺨﻔﺾ ﺟﺪا ﻣﻦ اﻟﺘﺠﺮﯾﺪ‬ Low  Machine language instructions are written in binary (0,1)(0,1) ‫ﺗﻌﻠﯿﻤﺎت ﻟﻐﺔ اﻵﻟﺔ ﻣﻜﺘﻮﺑﺔ ﺑﺸﻜﻞ ﺛﻨﺎﺋﻲ‬  Programming in machine language is tedious and error prone ‫اﻟﺒﺮﻣﺠﺔ ﺑﻠﻐﺔ اﻵﻟﺔ ﻣﻤﻠﺔ وﻣﻌﺮﺿﺔ ﻟﻠﺨﻄﺄ‬  Code is nearly impossible to understand and debug ‫ﯾﻜﺎد ﯾﻜﻮن ﻣﻦ اﻟﻤﺴﺘﺤﯿﻞ ﻓﮭﻢ اﻟﻜﻮد وﺗﺼﺤﯿﺤﮫ‬ 5 Machine Languages Excerpt from a machine language program: !‫ﯾﺪوخ وﯾﺼﺪع اﻟﻮاﺣﺪ‬ 6 Assembly Language  In the early 1950’s, Assembly languages evolved from machine languages..‫ ﺗﻄﻮرت ﻟﻐﺎت اﻟﺘﺠﻤﯿﻊ ﻣﻦ ﻟﻐﺎت اﻵﻟﺔ‬،‫ﻓﻲ أواﺋﻞ اﻟﺨﻤﺴﯿﻨﯿﺎت‬  An assembly language substitutes words for binary codes. ‫ﺗﺴﺘﺒﺪل ﻟﻐﺔ اﻟﺘﺠﻤﯿﻊ اﻟﻜﻠﻤﺎت ﺑﺎﻟﺮﻣﻮز اﻟﺜﻨﺎﺋﯿﺔ‬  Much easier to remember and use words, but still a low level of abstraction (instructions correspond to hardware operations). ‫ وﻟﻜﻦ ﻻ ﯾﺰال ﻣﺴﺘﻮى ﻣﻨﺨﻔﻀﺎ ﻣﻦ‬،‫ﻣﻦ اﻷﺳﮭﻞ ﺑﻜﺜﯿﺮ ﺗﺬﻛﺮ اﻟﻜﻠﻤﺎت واﺳﺘﺨﺪاﻣﮭﺎ‬.(‫اﻟﺘﺠﺮﯾﺪ )ﺗﺘﻮاﻓﻖ اﻟﺘﻌﻠﯿﻤﺎت ﻣﻊ ﻋﻤﻠﯿﺎت اﻷﺟﮭﺰة‬ 7 High-Level Languages  In the late 1950's, high-level languages were introduced. ‫ ﺗﻢ ﺗﻘﺪﯾﻢ ﻟﻐﺎت ﻋﺎﻟﯿﺔ اﻟﻤﺴﺘﻮى‬،‫ﻓﻲ أواﺧﺮ اﻟﺨﻤﺴﯿﻨﯿﺎت‬  High-level languages allow the programmer to write code closer to the way ‫ﺗﺴﻤﺢ اﻟﻠﻐﺎت ﻋﺎﻟﯿﺔ اﻟﻤﺴﺘﻮى ﻟﻠﻤﺒﺮﻣﺞ ﺑﻜﺘﺎﺑﺔ اﻟﺘﻌﻠﯿﻤﺎت اﻟﺒﺮﻣﺠﯿﺔ ﺑﺎﻟﻘﺮب ﻣﻦ اﻟﻄﺮﯾﻖ‬ humans think (as opposed to mimicking hardware operations)..(‫ﯾﻔﻜﺮ اﻟﺒﺸﺮ )ﻋﻠﻰ ﻋﻜﺲ ﻣﺤﺎﻛﺎة ﻋﻤﻠﯿﺎت اﻷﺟﮭﺰة‬  A much more natural way to solve problems. ‫طﺮﯾﻘﺔ أﻛﺜﺮ طﺒﯿﻌﯿﺔ ﻟﺤﻞ اﻟﻤﺸﺎﻛﻞ‬  Plus, programs are machine independent. ‫ اﻟﺒﺮاﻣﺞ ﻣﺴﺘﻘﻠﺔ ﻋﻦ اﻵﻟﺔ‬،‫ﺑﺎﻹﺿﺎﻓﺔ إﻟﻰ ذﻟﻚ‬ 8 High-Level Languages Two high level languages that perform the same task (in JavaScript and C++): :(++ C‫ﻟﻐﺘﺎن رﻓﯿﻌﺘﺎ اﻟﻤﺴﺘﻮى ﺗﺆدﯾﺎن ﻧﻔﺲ اﻟﻤﮭﻤﺔ )ﻓﻲ ﺟﺎﻓﺎ ﺳﻜﺮﯾﺒﺖ و‬ 9 Levels of Programming Languages This illustrates how code is translated ‫ﯾﻮﺿﺢ ھﺬا ﻛﯿﻔﯿﺔ ﺗﺮﺟﻤﺔ اﻟﺘﻌﻠﯿﻤﺎت‬ ‫اﻟﺒﺮﻣﺠﯿﺔ ﻣﻦ اﻟﻨﺼﻮص اﻟﻤﻘﺮوءة ﻣﻦ‬ from human readable ‫اﻹﻧﺴﺎن إﻟﻰ رﻣﺰ ﻣﻔﮭﻮم آﻟﯿﺎ‬ texts to a machine understandable code. ‫رﻣﺰ اﻟﺠﮭﺎز اﻟﻘﺎﺑﻞ ﻟﻠﺘﻨﻔﯿﺬ‬ 10 Program Translation  Using a high-level language, the programmer can reason at a high-level of abstraction..‫ ﯾﻤﻜﻦ ﻟﻠﻤﺒﺮﻣﺞ اﻟﺘﻔﻜﯿﺮ ﻋﻠﻰ ﻣﺴﺘﻮى ﻋﺎل ﻣﻦ اﻟﺘﺠﺮﯾﺪ‬،‫ﺑﺎﺳﺘﺨﺪام ﻟﻐﺔ ﻋﺎﻟﯿﺔ اﻟﻤﺴﺘﻮى‬  But programs must still be translated into machine language that the computer hardware can understand/execute..‫ﺗﻨﻔﯿﺬھﺎ‬/‫وﻟﻜﻦ ﻻ ﯾﺰال ﯾﺘﻌﯿﻦ ﺗﺮﺟﻤﺔ اﻟﺒﺮاﻣﺞ إﻟﻰ ﻟﻐﺔ اﻵﻟﺔ اﻟﺘﻲ ﯾﻤﻜﻦ ﻷﺟﮭﺰة اﻟﻜﻤﺒﯿﻮﺗﺮ ﻓﮭﻤﮭﺎ‬  There are two standard approaches to program translation: :‫ھﻨﺎك ﻧﮭﺠﺎن ﻗﯿﺎﺳﯿﺎن ﻟﺘﺮﺟﻤﺔ اﻟﺒﺮاﻣﺞ‬  Interpretation ‫ﺗﻔﺴﯿﺮ‬  Compilation ‫ﻣﺼﻨﻒ‬ 11 Program Translation Real-world analogy: translating a speech from one language to another ‫ ﺗﺮﺟﻤﺔ ﺧﻄﺎب ﻣﻦ ﻟﻐﺔ إﻟﻰ أﺧﺮى‬:‫ﺗﺸﺒﯿﮫ اﻟﻌﺎﻟﻢ اﻟﺤﻘﯿﻘﻲ‬  An Interpreter can be used provide a real-time translation..‫ﯾﻤﻜﻦ اﺳﺘﺨﺪام ﻣﺘﺮﺟﻢ ﺷﻔﻮي ﻟﺘﻮﻓﯿﺮ ﺗﺮﺟﻤﺔ ﻓﻲ اﻟﻮﻗﺖ اﻟﻔﻌﻠﻲ‬  The interpreter hears a phrase, translates, and immediately speaks the translation. ‫ﯾﺴﻤﻊ اﻟﻤﺘﺮﺟﻢ ﻋﺒﺎرة وﯾﺘﺮﺟﻢ وﯾﺘﺤﺪث ﻋﻠﻰ اﻟﻔﻮر‬  ADVANTAGE: the translation is immediate..‫ اﻟﺘﺮﺟﻤﺔ ﻓﻮرﯾﺔ‬:‫اﻟﻤﯿﺰة‬  DISADVANTAGE: if you want to hear the speech again, must interpret all over again. ‫ ﯾﺠﺐ أن ﺗﻔﺴﺮ ﻛﻞ ﺷﻲء‬،‫ إذا ﻛﻨﺖ ﺗﺮﻏﺐ ﻓﻲ ﺳﻤﺎع اﻟﺨﻄﺎب ﻣﺮة أﺧﺮى‬:‫اﻟﻌﯿﺐ‬ 12 Program Translation  A Translator (or compiler) translates the entire speech offline..‫ﯾﺘﺮﺟﻢ اﻟﻤﺘﺮﺟﻢ )أو اﻟﻤﺘﺮﺟﻢ( اﻟﺨﻄﺎب ﺑﺄﻛﻤﻠﮫ ﻓﻲ وﺿﻊ ﻋﺪم اﻻﺗﺼﺎل‬  The translator: Takes a copy of the speech, returns when the entire.‫ وﯾﻌﻮد ﻋﻨﺪﻣﺎ ﯾﻜﻮن ﻛﺎﻣﻞ ﯾﺘﻢ ﺗﺮﺟﻤﺔ اﻟﺨﻄﺎب‬،‫ ﯾﺄﺧﺬ ﻧﺴﺨﺔ ﻣﻦ اﻟﺨﻄﺎب‬:‫اﻟﻤﺘﺮﺟﻢ‬ speech is translated.  ADVANTAGE: Once translated, it can be read over and over very quickly. ‫ ﯾﻤﻜﻦ ﻗﺮاءﺗﮭﺎ ﻣﺮارا وﺗﻜﺮارا ﺑﺴﺮﻋﺔ ﻛﺒﯿﺮة‬،‫ ﺑﻤﺠﺮد ﺗﺮﺟﻤﺘﮭﺎ‬:‫اﻟﻤﯿﺰة‬  DISADVANTAGE: Must wait for the entire speech to be translated. ‫ ﯾﺠﺐ اﻻﻧﺘﻈﺎر ﺣﺘﻰ ﯾﺘﻢ ﺗﺮﺟﻤﺔ اﻟﺨﻄﺎب ﺑﺄﻛﻤﻠﮫ‬:‫اﻟﻌﯿﺐ‬ 13 ‫‪Speech Translation‬‬ ‫‪Interpreter:‬‬ ‫ﻣﺘﺮﺟﻢ ) ﺑﻠﺤﻈﺘﮭﺎ (‬ ‫‪Translator (compiler):‬‬ ‫اﻟﻤﺘﺮﺟﻢ )اﻟﻠﻲ ﯾﺴﺠﻞ اﻟﻜﻼم ﻛﺎاااﻣﻞ ﺛﻢ ﯾﺘﺮﺟﻤﮫ ﻛﺎﻣﻞ(‪:‬‬ ‫‪14‬‬ Interpreters  For program translation, the interpretation approach relies on a program known as an interpreter to translate and execute high-level statements: ‫ ﯾﻌﺘﻤﺪ ﻧﮭﺞ اﻟﺘﺮﺟﻤﺔ اﻟﺸﻔﻮﯾﺔ ﻋﻠﻰ ﺑﺮﻧﺎﻣﺞ ﯾﻌﺮف ﺑﺎﺳﻢ ﻣﺘﺮﺟﻢ‬،‫ﺑﺎﻟﻨﺴﺒﺔ ﻟﺘﺮﺟﻤﺔ اﻟﺒﺮاﻣﺞ‬ :‫ﻓﻮري ﻟﺘﺮﺟﻤﺔ وﺗﻨﻔﯿﺬ اﻟﺒﯿﺎﻧﺎت رﻓﯿﻌﺔ اﻟﻤﺴﺘﻮى‬  The interpreter reads one high-level statement at a time, immediately translating and executing the statement before processing the next one. ‫ وﯾﺘﺮﺟﻢ اﻟﺒﯿﺎن وﯾﻨﻔﺬه ﻋﻠﻰ‬،‫ﯾﻘﺮأ اﻟﻤﺘﺮﺟﻢ اﻟﻔﻮري ﺑﯿﺎﻧﺎ واﺣﺪا رﻓﯿﻊ اﻟﻤﺴﺘﻮى ﻓﻲ ﻛﻞ ﻣﺮة‬.‫اﻟﻔﻮر ﻗﺒﻞ ﻣﻌﺎﻟﺠﺔ اﻟﺒﯿﺎن اﻟﺘﺎﻟﻲ‬  JavaScript is an interpreted language. 15 Compilers  The compilation approach relies on a program known as a compiler to translate the entire high-level language program into its equivalent machine-language instructions:‫ﯾﻌﺘﻤﺪ ﻧﮭﺞ اﻟﺘﺠﻤﯿﻊ ﻋﻠﻰ ﺑﺮﻧﺎﻣﺞ ﯾﻌﺮف ﺑﺎﺳﻢ اﻟﻤﺘﺮﺟﻢ ﻟﺘﺮﺟﻤﺔ ﺑﺮﻧﺎﻣﺞ اﻟﻠﻐﺔ ﻋﺎﻟﻲ‬ :‫اﻟﻤﺴﺘﻮى ﺑﺄﻛﻤﻠﮫ إﻟﻰ ﺗﻌﻠﯿﻤﺎت اﻟﻠﻐﺔ اﻵﻟﯿﺔ اﻟﻤﻜﺎﻓﺌﺔ ﻟﮫ‬  The resulting machine-language program can be executed directly on the computer. ‫ﯾﻤﻜﻦ ﺗﻨﻔﯿﺬ ﺑﺮﻧﺎﻣﺞ اﻟﻠﻐﺔ اﻵﻟﯿﺔ اﻟﻨﺎﺗﺞ ﻣﺒﺎﺷﺮة ﻋﻠﻰ اﻟﻜﻤﺒﯿﻮﺗﺮ‬  Most languages used for the development of commercial software employ the compilation technique (C, C++)..(++ C، C) ‫ﺗﺴﺘﺨﺪم ﻣﻌﻈﻢ اﻟﻠﻐﺎت اﻟﻤﺴﺘﺨﺪﻣﺔ ﻟﺘﻄﻮﯾﺮ اﻟﺒﺮاﻣﺞ اﻟﺘﺠﺎرﯾﺔ ﺗﻘﻨﯿﺔ اﻟﺘﺠﻤﯿﻊ‬ 16 Compilation vs. Interpretation Compliers Interpreters Scans the entire program and Translates program one statement at a translates it as a whole into machine time..‫ﯾﺘﺮﺟﻢ اﻟﺒﺮﻧﺎﻣﺞ ﻋﺒﺎرة واﺣﺪة ﻓﻲ ﻛﻞ ﻣﺮة‬ code..‫ﯾﻘﻮم ﺑﻤﺴﺢ اﻟﺒﺮﻧﺎﻣﺞ ﺑﺄﻛﻤﻠﮫ وﺗﺮﺟﻤﺘﮫ ﻛﻜﻞ إﻟﻰ رﻣﺰ اﻟﺠﮭﺎز‬ ‫ﯾﺴﺘﻐﺮق ﺗﺤﻠﯿﻞ ﺷﻔﺮة‬ It takes large amount of time to analyze It takes less amount of time to analyze ‫ﯾﺴﺘﻐﺮق ﺗﺤﻠﯿﻞ ﺷﻔﺮة اﻟﻤﺼﺪر‬ ‫اﻟﻤﺼﺪر ﻗﺪرا ﻛﺒﯿﺮا ﻣﻦ‬ ‫ وﻟﻜﻦ وﻗﺖ اﻟﺘﻨﻔﯿﺬ‬،‫وﻗﺘﺎ أﻗﻞ‬ ‫ وﻟﻜﻦ وﻗﺖ اﻟﺘﻨﻔﯿﺬ‬،‫اﻟﻮﻗﺖ‬ the source code, but the overall the source code, but the overall.‫اﻹﺟﻤﺎﻟﻲ أﺑﻄﺄ‬.‫اﻹﺟﻤﺎﻟﻲ أﺳﺮع ﻧﺴﺒﯿﺎ‬ execution time is comparatively faster. execution time is slower. Generates intermediate object code which No intermediate object code is further requires linking, hence requires generated, hence are memory more memory. ‫ وﺑﺎﻟﺘﺎﻟﻲ‬،‫ﯾﻘﻮم ﺑﺈﻧﺸﺎء رﻣﺰ ﻛﺎﺋﻦ وﺳﯿﻂ ﯾﺘﻄﻠﺐ اﻟﻤﺰﯾﺪ ﻣﻦ اﻟﺮﺑﻂ‬ efficient. ‫ وﺑﺎﻟﺘﺎﻟﻲ ﻓﺈن اﻟﺬاﻛﺮة ﻓﻌﺎﻟﺔ‬،‫ﻻ ﯾﺘﻢ إﻧﺸﺎء رﻣﺰ ﻛﺎﺋﻦ وﺳﯿﻂ‬.‫ﯾﺘﻄﻠﺐ اﻟﻤﺰﯾﺪ ﻣﻦ اﻟﺬاﻛﺮة‬ ‫ﻻ ﯾﻮﻟﺪ رﺳﺎﻟﺔ اﻟﺨﻄﺄ إﻻ ﺑﻌﺪ‬ It generates the error message only after Continues translating the program until the ‫ﯾﺴﺘﻤﺮ ﻓﻲ ﺗﺮﺟﻤﺔ اﻟﺒﺮﻧﺎﻣﺞ ﺣﺘﻰ ﯾﺘﻢ اﺳﺘﯿﻔﺎء اﻟﺨﻄﺄ‬ ‫ وﺑﺎﻟﺘﺎﻟﻲ‬.‫ﻣﺴﺢ اﻟﺒﺮﻧﺎﻣﺞ ﺑﺄﻛﻤﻠﮫ‬ ‫ﻓﺈن ﺗﺼﺤﯿﺢ اﻷﺧﻄﺎء ﺻﻌﺐ‬ scanning the whole program. Hence first error is met, in which case it stops. ‫ وﺑﺎﻟﺘﺎﻟﻲ ﻓﺈن ﺗﺼﺤﯿﺢ‬.‫ وﻓﻲ ھﺬه اﻟﺤﺎﻟﺔ ﯾﺘﻮﻗﻒ‬،‫اﻷول‬.‫اﻷﺧﻄﺎء ﺳﮭﻞ‬.‫ﻧﺴﺒﯿﺎ‬ debugging is comparatively hard. Hence debugging is easy. Programming language like C, C++ use Programming language like Python, Ruby, compilers..‫ اﻟﻤﺘﺮﺟﻤﯿﻦ‬++C‫ و‬C ‫ﺗﺴﺘﺨﺪم ﻟﻐﺔ اﻟﺒﺮﻣﺠﺔ ﻣﺜﻞ‬ Java use interpreters. ‫ﺗﺴﺘﺨﺪم ﻟﻐﺔ اﻟﺒﺮﻣﺠﺔ ﻣﺜﻞ ﺑﺎﯾﺜﻮن وروﺑﻲ وﺟﺎﻓﺎ‬.‫اﻟﻤﺘﺮﺟﻤﯿﻦ اﻟﻔﻮرﯾﯿﻦ‬ 17 Compilation vs. Interpretation 18

Use Quizgecko on...
Browser
Browser