Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC 1306 COMPUTER SCIENCE AND PROGRAMMING

Similar presentations


Presentation on theme: "COSC 1306 COMPUTER SCIENCE AND PROGRAMMING"— Presentation transcript:

1 COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Jehan-François Pâris Fall 2016 1

2 THE ONLINE BOOK CHAPTER I GENERAL INTRODUCTION

3 Algorithms Problem solving is coming with an algorithm that can serve as a general solution

4 High-level languages Very few programs are written in machine language
Prefer to use a more human-friendly high-level language Cannot be directly executed by any computer Must be first translated Two approaches Compiled languages Interpreted languages

5 Compiled languages (I)
Programs written in old Fortran, C, C++ and so on must go through a program that translates them into directly executable code The compiler Doing g++ myprogram.cpp –o myprogram produces an executable file called myprogram that can run anytime No need to recompile

6 Compiled languages (II)
C program C compiler Executable

7 Advantages The executable can run on any computer with
Same CPU instruction set Same—or compatible—operating system We can sell copies of the executable without revealing the secrets of our program Reverse engineering is possible but very time-consuming

8 Interpreted languages (I)
Languages like Basic, Perl, Python and Ruby are not compiled Translated again and again into bytecode Each time we execute them Bytecode is interpreted by the language interpreter

9 Interpreted languages (II)
Python Program Byte code Python Interpreter: translates program into bytecode then executes it

10 Advantages Platform independence:
Bytecode can run on any platform for which there is an interpreter Dynamic typing: Same variable can refer to a string then an integer then … Smaller executable sizes

11 Disadvantages Portability limitations :
Bytecode will not run on any machine on which the interpreter was not installed. Speed: Bytecode is not optimized for a specific architecture Just-in-time compilation introduces delays Cannot sell a copies of a program without revealing its source code

12 A partial solution In many cases, speed is not an issue outside of loops than get executed thousand and thousands of times Loops inside other loops Can rewrite code for these inner loops in C and include this code into a Python program Use Python C API

13 Neither fish nor fowl Java is compiled Like Fortran, C, …
into bytecode Like Perl and Python Bytecode is executed by Java Virtual Machine

14 A comparison Compiled languages
Translate once the whole program into an executable Fast Can run on any machine having same architecture and same OS Interpreted languages Translate programs line by line each time they are executed Much slower Can run on any machine having the interpreter installed

15 My first program print(“My first program adds two numbers, 2 and 3”)
It’s “print” and not “Print” Cannot use smart quotes Semicolons are optional and nobody uses them

16 Types of instructions Input & output name = input(“Enter your name: ”)
Print(“Hello!”) Math and logic celsius = (fahrenheit - 32)* 5/9 Conditional execution if celsius > 100 : Repetition For all lines in file:

17 Debugging Finding what’s wrong in your program
Finding the roof of the problem Fixing the error

18 Types of error Syntax errors
print(“Hello!” “How are you:”) # missing comma Run-time errors Division by zero Semantic errors Your solution is wrong You expressed it poorly

19 Experimental debugging (I)
Modify your program until it works?

20 Experimental debugging (II)
Look at your program Try to explain it to yourself, to friends Do not let them copy your code! Add print statements after each step Remove them or better comment them out when they are not needed anymore.

21 Interactive Python Requires Javascript to be authorized for at least interactivepython.org

22 Natural and formal languages
Natural languages are complex, tolerate ambiguities, often have to be taken figuratively I am literally dying of thirst Could you bring me a glass of water? Formal languages are simpler, have much more strict syntax rules, and want to be unambiguous What they say will be taken literally

23 Natural language ambiguities
KIDS MAKE NUTRITIOUS SNACKS STOLEN PAINTING FOUND BY TREE QUEEN MARY HAVING BOTTOM SCRAPED MILK DRINKERS ARE TURNING TO POWDER SQUAD HELPS DOG BITE VICTIM

24 Commenting Main purpose To help human readers understand your programs
Becomes more important as program sizes grow To record who wrote the program, when it happened and so on

25 In-line comments Anything following a pound sign (“#”) is ignored by the Python interpreter print (“Hello World!”) print (“Hello World!”) # not very original r = d/2 # compute radius of circle #Begin main loop

26 Multi-line comments Anything between three double quotes—”””—is ignored by the Python interpreter “”” Tell what the program does Jehan-Francois Paris Assignment # 1 COSC 1306 MW 2:30-4:00 Fall “”” At the beginning of each program


Download ppt "COSC 1306 COMPUTER SCIENCE AND PROGRAMMING"

Similar presentations


Ads by Google