Programming Languages

Similar documents
Iterative Languages. Scoping

PL Recitation 9/21/2010

Fundamentals of Programming Session 13

Introduction. C provides two styles of flow control:

A Fast Review of C Essentials Part II

Lab 09: Advanced SQL

CSE 2421: Systems I Low-Level Programming and Computer Organization. Functions. Presentation C. Predefined Functions

Loops. Repeat after me

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Chapter 5: Prefix vs. Postfix 8/19/2018. The Increment and Decrement Operators. Increment and Decrement Operators in Program 5-1

Technical Questions. Q 1) What are the key features in C programming language?

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5-1

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

Subroutines. Subroutine. Subroutine design. Control abstraction. If a subroutine does not fit on the screen, it is too long

Chapter 5: Loops and Files

CS110D: PROGRAMMING LANGUAGE I

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Fundamentals of Programming Session 12

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. Looping. ++ is the increment operator.

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

Chapter 2: Functions and Control Structures

Function Call Stack and Activation Records

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

Object-Oriented Programming in Java

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

Recap: Assignment as an Operator CS 112 Introduction to Programming

C Programming Language

Repetition Structures II

3 The L oop Control Structure

Functions and Recursion

C Functions. Object created and destroyed within its block auto: default for local variables

CS 112 Introduction to Programming

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

V3 1/3/2015. Programming in C. Example 1. Example Ch 05 A 1. What if we want to process three different pairs of integers?

CS2223: Algorithms D- Term, Homework I. Teams: To be done individually. Due date: 03/27/2015 (1:50 PM) Submission: Electronic submission only

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Fundamental Concepts and Definitions

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

LESSON 6 FLOW OF CONTROL

Looping. Arizona State University 1

G Programming Languages - Fall 2012

Day08 A. Young W. Lim Mon. Young W. Lim Day08 A Mon 1 / 27

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE

The for Loop. Lesson 11

Functions and Recursion

Lecture 10. Daily Puzzle

Lecture 04 FUNCTIONS AND ARRAYS

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

142

Repetition CSC 121 Fall 2014 Howard Rosenthal

Array. Prepared By - Rifat Shahriyar

Functions. Lecture 6 COP 3014 Spring February 11, 2018

C: How to Program. Week /Apr/16

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Scopes Global and Block

Programming Languages

CA Compiler Construction

CSE 230 Intermediate Programming in C and C++ Functions

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Structured Programming. Dr. Mohamed Khedr Lecture 9

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

(Refer Slide Time: 00:26)

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Multiple-Subscripted Arrays

LECTURE 14. Names, Scopes, and Bindings: Scopes

Repetition and Loop Statements Chapter 5

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

For Loop. Variations on Format & Specific Examples

JUMP, LOOP AND CALL INSTRUCTIONS

Unit 3 Decision making, Looping and Arrays

Programming for Engineers Iteration

int j = 0, sum = 0; for (j = 3; j <= 79; j++) { sum = sum + j; System.out.println(sum); //Show the progress as we iterate thru the loop.

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Chapter 3 Structured Program Development

Problem Solving With Loops

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

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

Run-time Environment

L o o p s. for(initializing expression; control expression; step expression) { one or more statements }

Assignment: 1. (Unit-1 Flowchart and Algorithm)

Run-time Environments - 2

Loops / Repetition Statements

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

COMP519 Web Programming Lecture 27: PHP (Part 3) Handouts

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

Syntax Errors; Static Semantics

LECTURE 5 Control Structures Part 2

The Big Picture. Linker. Module 1. Module 2. One Module. Module 3. Module 4

Chapter 4: Control structures. Repetition

(2) Accidentally using the wrong instance of a variable (sometimes very hard one to find).

CS 321 Homework 4 due 1:30pm, Thursday, March 15, 2012 This homework specification is copyright by Andrew Tolmach. All rights reserved.

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

Transcription:

Programming Languages Recitation Summer 2014

Recitation Leader Joanna Gilberti Email: jlg204@cs.nyu.edu Office: WWH, Room 328 Web site: http://cims.nyu.edu/~jlg204/courses/pl/index.html

Homework Submission Guidelines Submit all homework to me via email (at jlg204@cs.nyu.edu only) on the due date. Email subject: PL homework # (EXAMPLE: PL Homework 1 ) Use file naming convention on all attachments: firstname_lastname_hw_# (example: "joanna_gilberti_hw_1") In the case multiple files are being submitted, package all files into a ZIP file, and name the ZIP file using the naming convention above.

What s Covered in Recitation Homework Solutions Questions on the homeworks For additional questions on assignments, it is best to contact me via email. I will hold office hours (time will be posted on the recitation Web site) Run sample programs that demonstrate concepts covered in class

Iterative Languages Scoping Sample Languages C: static-scoping Perl: static and dynamic-scoping (use to be only dynamic scoping) Both gcc (to run C programs), and perl (to run Perl programs) are installed on the cims servers Must compile the C program with gcc, and then run the generated executable Must include the path to the perl executable in the perl script

Basic Scope Concepts in C block scope (nested scope) run scope_levels.c (sl) ** block scope: set of statements enclosed in braces {}, and variables declared within a block has block scope and is active and accessible from its declaration to the end of the block Analysis: Variable, i, is visible within outer block (within the main function) Variables i, and j are visible only within the inner block (enclosed by {})

Analysis (continued): Variable i is defined in two places, but since they are declared in two different scopes the two variables cannot be accessed at the same time Demonstrated in output: i prints 23 before and after the loop gets executed (loop is in the inner scope) The for-loop loops through ten different values for i while the inner-scoped portion of the code is being executed After the for-loop (inner scope block) completes executing, the variables within the innerscoped block are no longer accessible The value for i in the outer-scoped block is now accessible and gets printed

Basic Scope Concepts in C program scope run program_block_scope.c (pbs) program scope: variables declared outside of a function have program scope

Analysis: Variables x and y are declared globally with program scope The declared function, function_1() prints x and y the values of the global variables x and y are printed (no local declarations) The function function_1() is called twice from two nested blocks: (1) from the outer block in the main() function, and (2) from the inner block in the main() function. The first function_1() call (outer block) prints the global values for both x and y called from the outer block The second function_1() call (inner, nested block) prints the global values for both x and y again called from the inner nested block The print statement in the main() function in the outer block prints the value of x that was locally declared in the main() function, but the global value of y Finally, the print statement in main(), but in the inner, nested block prints the values for the locally declared variables x and y

Basic Scope Concepts in C the static specifier run static_specifier.c (ss) the static specifier indicates that the memory location of a variable is temporary, and so a variable s reserved space in memory can be erased or relocated when the variable is out of its scope.

Analysis: The function add_two() is declared, and is called several times from the main() function Two arguments are passed to the add_two() function, which are added when the function executes, and the sum is returned A counter is set to keep track of how many times the function is called A variable is declared locally with the static specifier in the add_two() function that has function scope this is the counter that keeps track on how many times the add_two() function is called The values stored by the counter are retained because the duration of the variable is permanent although the scope of the counter is within the block of the add_two() function, the memory location of the counter and value saved in the location are not changed after the add_two() function is called and the execution control is returned back to the main() function. The counter variable is used as a counter to keep the number of calls received by the add_two() function. The printf() function in the add_two() function prints out the value saved by the counter variable each time the add_two() function is called. In addition, counter is incremented by one each time after the printf() function is executed The for-loop in the main() function calls the add_two() function five times The values for i and j are passed, and their sum is returned and printed Evident from the output, the value for the counter is saved and incremented each time the add_two() function is called, and is retained after the function exits because the counter variable is declared with static

Basic Scope Concepts in C more on static run more_static_specifier.c (mss) When a variable is declared with static when the variable goes out of scope (the block it was local to is finished) the variable stays in memory, retaining its value. The variable stays in memory until the program ends. The behavior resembles global variables, but static variables still obey scope rules and therefore cannot be accessed outside of their scope

Analysis: The j variable is accessible by both the up() and down() functions and retains its value it is declared with static but globally The k variable also retain their values but are two different variables in each of their own scopes In the up() function, both k and j are incremented, and in the down() function both k and j are decremented. The difference is: k has separate scopes in each function, and even though their values retain for each function, they start from a different initial value because they have separate scopes. j has one scope (program scope) even the value of j is retained, and across both functions

Static vs. Dynamic Scoping two programs that do exactly the same thing: one written in C (static scope) one written in Perl (dynamic scope) run staticscope_cversion.c (sscv) dynamicscope_pversion.pl Analysis: (see diagrams)

Static vs. Dynamic Scoping

Perl With Static and Dynamic Scope Two perl programs that do exactly the same thing: program that demonstrates statically scoped samplestaticscope.pl program that demonstrates dynamically scoped sampledynamicscope.pl

Analysis: Static scoping: active bindings are determined using the text of the program at compile time - Current binding - the matching declaration whose block most closely surrounds the point in the program where the name is mentioned. Dynamic scoping: active bindings are determined by the flow of execution at run time Current binding - the one encountered the most recently during the execution and that has not yet been destroyed. my makes a variable statically scoped Only available to the subroutine (scope) in which it is declared Not available to subroutines you call Not available to subroutines that called you Destroyed when the execution exits the block it s in local makes a variable dynamically scoped A temporary global Available to subroutines you call Not available to a subroutine that called you Can shadow and protect an existing global Destroyed when execution exits the block it s in