Iterative Languages. Scoping

Similar documents
Programming Languages

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

*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

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.

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.

Loops. Repeat after me

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?

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

C Programming Language

3 The L oop Control Structure

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

Functions and Recursion

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

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?

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

Fundamental Concepts and Definitions

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

Recap: Assignment as an Operator CS 112 Introduction to Programming

LESSON 6 FLOW OF CONTROL

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 04 FUNCTIONS AND ARRAYS

Object-Oriented Programming in Java

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

Repetition CSC 121 Fall 2014 Howard Rosenthal

CS 112 Introduction to Programming

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

Programming Languages

CSE 230 Intermediate Programming in C and C++ Functions

Repetition Structures II

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

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

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

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

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

Problem Solving With Loops

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

142

Looping. Arizona State University 1

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

Kapil Sehgal PGT Computer. Science Ankleshwar Gujarat

Array. Prepared By - Rifat Shahriyar

LECTURE 5 Control Structures Part 2

Chapter 4: Control structures. Repetition

CA Compiler Construction

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

CS125 : Introduction to Computer Science. Lecture Notes #6 Compound Statements, Scope, and Advanced Conditionals

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

CSI33 Data Structures

ECE 30 Introduction to Computer Engineering

Compiling and running OpenMP programs. C/C++: cc fopenmp o prog prog.c -lomp CC fopenmp o prog prog.c -lomp. Programming with OpenMP*

Chapter 3 Structured Program Development

Computer Science & Information Technology (CS) Rank under AIR 100. Examination Oriented Theory, Practice Set Key concepts, Analysis & Summary

ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C

Chapter 4: Control structures

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

Repetition Structures

C++ Programming: From Problem Analysis to Program Design, Third Edition

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

UNIT 3 FUNCTIONS AND ARRAYS


Programming in C. main. Level 2. Level 2 Level 2. Level 3 Level 3

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Transcription:

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 executible 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 {}) 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 inner-scoped 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 : a. program that demonstrates statically scoped samplestaticscope.pl b. 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