Programming Languages: Part 1. Robert M. Dondero, Ph.D. Princeton University

Similar documents
Programming Languages Part 1. Copyright 2017 by Robert M. Dondero, Ph.D. Princeton University

Programming Language Basics

EL2310 Scientific Programming

EL2310 Scientific Programming

PROGRAMMAZIONE I A.A. 2017/2018

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

Lecture 1. basic Python programs, defining functions

Software Project. Lecturers: Ran Caneti, Gideon Dror Teaching assistants: Nathan Manor, Ben Riva

Lecture 1: Overview of Java

Table of Contents EVALUATION COPY

27-Sep CSCI 2132 Software Development Lecture 10: Formatted Input and Output. Faculty of Computer Science, Dalhousie University. Lecture 10 p.

CSE 421 Course Overview and Introduction to Java

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 1

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

Spring 2018 NENG 202 Introduction to Computer Programming

The C Programming Language

Senthil Kumaran S

Chris Simpkins (Georgia Tech) CS 2316 Data Manipulation for Engineers Python Overview 1 / 9

EE 209: Programming Structures for Electrical Engineering

C++ Spring Break Packet 11 The Java Programming Language

CISC 124: Introduction To Computing Science II

CS 3813/718 Fall Python Programming. Professor Liang Huang.

Python Workshop. January 18, Chaitanya Talnikar. Saket Choudhary

Programming in C. What is C?... What is C?

Programming in C UVic SEng 265

Programming in C. What is C?... What is C?

Crash Dive into Python

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

The current topic: Python. Announcements. Python. Python

Is it ever useful to be confident that a problem is hard?

Compilation I. Hwansoo Han

Topic 6: A Quick Intro To C

Python for Earth Scientists

Chapter 1: Introduction

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 2: SEP. 8TH INSTRUCTOR: JIAYIN WANG

Python. Jae-Gil Lee Based on the slides by K. Naik, M. Raju, and S. Bhatkar. December 28, Outline

Index. Course Outline. Grading Policy. Lab Time Distribution. Important Instructions

CSCI 2132 Software Development. Lecture 8: Introduction to C

Chapter 1 Introduction to Computers, Programs, and Java

CpSc 1111 Lab 6 Conditional Statements, Loops, the Math Library, and Random Numbers What s the Point?

Programming: detailed instructions which tell the computer hardware what to do aka software Computer Science: the study NOT of computers, but of what

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Goals. Java - An Introduction. Java is Compiled and Interpreted. Architecture Neutral & Portable. Compiled Languages. Introduction to Java

EE 209: Programming Structures for Electrical Engineering. (Many Slides Borrowed from Princeton COS 217)

assembler Machine Code Object Files linker Executable File

CSI33 Data Structures

by Pearson Education, Inc. All Rights Reserved.

Object Oriented Concepts and Programming (CSC244) By Dr. Tabbasum Naz

M2 Instruction Set Architecture

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

Java is a high-level programming language originally developed by Sun Microsystems and released in Java runs on a variety of

What s next. Computer Systems A Programmer s Perspective

ENCE Computer Organization and Architecture. Chapter 1. Software Perspective

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

CSCI 2132: Software Development. Norbert Zeh. Faculty of Computer Science Dalhousie University. Introduction to C. Winter 2019

2 Introduction to Java. Introduction to Programming 1 1

Introduction to OOP Using Java Pearson Education, Inc. All rights reserved.

COS 217: Introduction to Programming Systems!

Introduction to Python Part 1. Brian Gregor Research Computing Services Information Services & Technology

COMP1917: Computing 1 1. Introduction

CS 113: Introduction to

CS11 Java. Fall Lecture 1

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

Lecture 1. Introduction to course, Welcome to Engineering, What is Programming and Why is this the first thing being covered in Engineering?

Lec 3. Compilers, Debugging, Hello World, and Variables

CS240: Programming in C

C Introduction. Comparison w/ Java, Memory Model, and Pointers

CS133 C Programming. Instructor: Jialiang Lu Office: Information Center 703

Outline. Compiling process Linking libraries Common compiling op2ons Automa2ng the process

1. Introduction. Course Web Site. COMP1917: Computing 1. Textbook. Occupational Health and Safety (OHS)

History of Java. Java was originally developed by Sun Microsystems star:ng in This language was ini:ally called Oak Renamed Java in 1995

COS 217: Introduction to Programming Systems!

COS 217: Introduction to Programming Systems

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

A software view. Computer Systems. The Compilation system. How it works. 1. Preprocesser. 1. Preprocessor (cpp)

CSE 303: Concepts and Tools for Software Development

EL2310 Scientific Programming

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Java Bytecode (binary file)

Programming and Data Structure

Informática Ingeniería en Electrónica y Automática Industrial

EC 413 Computer Organization

Welcome to... CS113: Introduction to C

Introduction. A. Bellaachia Page: 1

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 61C: Great Ideas in Computer Architecture Introduction to C

Python Programming, bridging course 2011

1/14/2014. Introduction to CSE 1325 Object Oriented Programming (Using Java) Introduction (Cont.) Introduction

introduction week 1 Ritsumeikan University College of Information Science and Engineering Ian Piumarta 1 / 20 imperative programming about the course

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

COS 217: Introduction to Programming Systems. Goals for Todayʼs Class. Introductions

1DL321: Kompilatorteknik I (Compiler Design 1) Introduction to Programming Language Design and to Compilation

ANSI C. Data Analysis in Geophysics Demián D. Gómez November 2013

Oregon State University School of Electrical Engineering and Computer Science. CS 261 Recitation 1. Spring 2011

Introduction. CS 2210 Compiler Design Wonsun Ahn

Fundamentals of Programming (Python) Basic Concepts. Ali Taheri Sharif University of Technology Spring 2018

Language Design COMS W4115. Prof. Stephen A. Edwards Fall 2006 Columbia University Department of Computer Science

Transcription:

Programming Languages: Part 1 Robert M. Dondero, Ph.D. Princeton University 1

Objectives You will learn/review: Subsets of C, Java, and Python... That are appropriate for COS 333... Through example programs Example 1 in C, Java, Python Example 2 in C, Java, Python 2

Objectives Note: Comprehensive coverage is impossible! Please supplement with reading 3

Objectives Specifically: Overview Program structure Building and running Multiple functions/methods Using library code 4

C Overview Who: AT&T Bell Labs Dennis Ritchie When: 1972 Why: Build Unix 5

C Overview Characteristics: Low level Strongly typed Minimal standard library Two-word description: Fast: Close to the OS and hardware Eccentric: Unless you know the underlying assembly/machine lang 6

Why Study C? Why study C? The world's most popular/influential lang Illustrates some of the course's material Provides strong contrast with other langs I know it You know it from COS 217 We'll do a quick review 7

Java Overview Who: Sun Microsystems Team led by... James Gosling 8

Java Overview When: 1990 Why: Embedded systems ("Oak"), and then... "The language of the Web", and then... Robust portable server-side software 9

Java Overview Characteristics: Medium level Strongly typed Large standard library Two-word description: Rich: Large language and standard library Verbose: I/O is especially so 10

Why Study Java? Why study Java? Elegant and popular Illustrates most of the course's material I know it You know it from COS 126/226 But maybe not some of the advanced features We'll do a quick overview 11

Python Overview Who: Guido Van Rossum 12

Separated at Birth? 13

Python Overview When: ~1990 Why: "I was looking for a 'hobby' programming project that would keep me occupied during the week around Christmas. My office would be closed, but I had a home computer, and not much else on my hands. I decided to write an interpreter for the new scripting language I had been thinking about lately: a descendant of ABC that would appeal to Unix/C hackers. I chose Python as a working title for the project, being in a slightly irreverent mood (and a big fan of Monty Python's Flying Circus)." Van Rossum 14

Python Overview Characteristics: High level Weakly typed No variable declaration statements "Scripting language" Rich standard library But inconsistent programming conventions 15

Python Overview Two-word description: Expressive: Can accomplish much work with little code Immature: Still evolving; ill-documented 16

Why Study Python? Why study Python? Reasonably elegant and getting more popular Esp. vs. Perl Illustrates most of the course's material Python/Django is popular I'm new to it; I want to learn it You also are new to it??? Thus the first part of the course won't be entirely review??? 17

Why Study that Combination? Among high level languages: Good for computers C Java Python Good for pgmmers The three languages cover the spectrum well 18

Our Approach Approach: sequence of example programs For each example: Examine/run code Note important features Generalize Incidentally... No function/method comments Bad style but saves paper 19

"Hello World" Programs Illustrate Program structure Building and running 20

"Hello World" in C See hello.c Overall structure Comments #include preprocessor directive Definition of main() function printf() function String literal (" ") 21

"Hello World" in C Generalizations: A C program consists of functions Exactly one must be named "main" 22

"Hello World" in C Building and executing: hello.c C preprocessor/compiler/ assembler/linker At run-time OS hello (executable binary code) Source code is portable (to some extent) Executable binary code is not portable 23

"Hello World" in C Long procedure: File: hello.c File: hello.i Preprocess $ gcc -Wall -ansi -pedantic -E hello.c > hello.i Compile $ gcc -Wall -ansi -pedantic -S hello.i 24

"Hello World" in C File: hello.s File: hello.o Assemble $ gcc -Wall -ansi -pedantic -c hello.s File: hello Link $ gcc -Wall -ansi -pedantic hello.o -o hello Run $ hello 25

"Hello World" in C Short procedure: File: hello.c File: hello Preprocess, compile, assemble, link $ gcc -Wall -ansi -pedantic hello.c -o hello Run $ hello 26

"Hello World" in Java See Hello.java Overall structure Comments Class definition Definition of main() method "static" => class level, not object level System.out.println() method String literal (" ") 27

"Hello World" in Java Generalizations: A program consists of classes Define one public class per file Can define other non-public classes in same file A class consists of methods (and fields) 28

"Hello World" in Java Building and executing: Hello.java Java compiler At run-time OS Java interpreter (JVM) Hello.class (bytecode) Source code is portable Bytecode is portable 29

"Hello World" in Java Procedure: File: Hello.java File: Hello.class Compile $ javac Hello.java Run $ java Hello 30

"Hello World" in Python See hello1.py Overall structure (minimal!) #!/usr/bin/env python Comments print statement String literal (' ') No semicolons!!! Commands OS to... Use the Python interpreter Search the PATH env var to find it 31

"Hello World" in Python Generalizations: Program consists of statements (and other things; stay tuned) String literal: ' ' or " " 32

"Hello World" in Python Building and executing: OS Python interpreter At run-time hello1.py hello1.pyc (bytecode) Python compiler Source code is portable Bytecode is portable 33

"Hello World" in Python Procedure 1 (works with/without #! line) $ python hello1.py Procedure 2 (works only with #! line) $ chmod 700 hello1.py $ hello.py Procedure 3 (works with/without #! line) $ python >>> import hello1 Procedure 3 is valuable for learning 34

"Hello World" in Python See hello2.py Function definitions and calls name == ' main ' True iff this module is the "main" module True iff this module is compiled/interpreted directly from the command-line False iff this module is loaded into interpreter by another module Indentation matters!!! 35

"Hello World" in Python Generalizations: Program consists of statements and functions Indentation indicates nesting Better to avoid global code Global code can hide errors in local code 36

"Hello World" in Python Procedure 1 (works with/without #! line) python hello2.py Procedure 2 (works only with #! line) chmod 700 hello2.py hello2.py 37

"Hello World" in Python Procedure 3 (works with/without #! line) $ python >>> import hello2 Makes all names defined in the hello2 module accessible within this (the main) module >>> hello2.main() Can use those names using "hello2." prefix 38

"Hello World" in Python Procedure 4 (works with/without #! line) $ python >>> from hello2 import main >>> main() Imports "main" from the hello2 module into this (the main) module Can use "main" as if it were defined in this module 39

"Square" Programs Illustrate: Multi-function/method programs Function parameters 40

"Square" in C See square1.c One source code file defines multiple functions Parameters are passed by value Compiler does one pass only Compiler must see function definition (or declaration) before function call 41

"Square" in C See square2.c Function declaration Compiler must see function definition or declaration before function call 42

"Square" in Java See Square.java One source code file (one class) defines multiple methods Parameters are passed by value Compiler does two passes Can define methods in any order 43

"Square" in Python See square1.py One source code file defines multiple functions Weakly typed Parameters are typeless Compiler/interpreter does one pass only Compiler/interpreter must see function def before function call 44

"Square" in Python However... Python does not compile/interpret code until necessary 45

"Square" in Python See square2.py OK to define sqr() after main() See square3bad.py Compiler/interpreter attempts to interpret call of sqr() before seeing def of sqr() Error

"Square" in Python See square4bad.py Compiler/interpreter does not attempt to interpret call of non-existing function No error detected! Dangerous!!!

"SquareRoot" Programs Illustrate: Using library code 48

"SquareRoot" in C See squareroot.c #include <math.h> Provides compiler with declaration of sqrt() Allows compiler to check calls of sqrt() 49

"SquareRoot" in C To build: $ gcc -Wall -ansi -pedantic squareroot.c -o squareroot -lm -lm commands linker to search libm.a for function defs Experiment: Required on many/most systems, but not on ours Remove #include directive What happens? 50

"SquareRoot" in Java See SquareRoot1.java Uses full class name in code Fine, but sometimes awkward See SquareRoot2.java import java.lang.math; Commands compiler to resolve "Math" to "java.lang.math" Actually, not necessary: "java.lang.*" is imported by default 51

"SquareRoot" in Python See squareroot1.py import math Makes all names defined in the math module accessible within this (the main) module Can use those names using "math." prefix Bad: Sometimes awkward to use prefix Good: No conflicts with names in this module 52

"SquareRoot" in Python See squareroot2.py from math import sqrt Imports "sqrt" from the math module into this module Can use "sqrt" as if it were defined in this module Good: No need to specify prefixes Bad: Potential name conflicts New def overwrites old def 53

Summary We have covered: In Java, C, and Python... Program structure Building and running Multiple functions/methods Using library code 54