The lifespan of a variable

Slides:



Advertisements
Similar presentations
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
Advertisements

Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Local Variables and Scope Benjamin Fein. Variable Scope A variable’s scope consists of all code blocks in which it is visible. A variable is considered.
Debugging Mohammad Zikky, M.T. 2 Debugging Introduction (1 of 2)  Debugging is methodical process for removing mistakes in a program  So important,
CS0004: Introduction to Programming Repetition – Do Loops.
Chapter 3.5 Debugging Games
Summary of the lecture We discussed –variable scope –instance variable declarations –variable lifetime.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
1 CS 106, Winter 2009 Class 10, Section 4 Slides by: Dr. Cynthia A. Brown, Instructor section 4: Dr. Herbert G. Mayer,
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
1 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
General Issues in Using Variables
Chapter 6 RAID. Chapter 6 — Storage and Other I/O Topics — 2 RAID Redundant Array of Inexpensive (Independent) Disks Use multiple smaller disks (c.f.
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
1 CSC 221: Introduction to Programming Fall 2012 Functions & Modules  standard modules: math, random  Python documentation, help  user-defined functions,
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
“Education is a Treasure that follows you everywhere.” – Chines Proverb Methods and Functions.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Coding Conventions  Coding conventions are a set of guidelines for a specific software project that recommend programming style, practices and methods.
Nested for loops.
+ Storage Classes and Linkage. + Introduction Scope describe the region or regions of a program that can access and identifier Variables can be shared.
ATS Programming Short Course I INTRODUCTORY CONCEPTS Tuesday, Feb 10th, 2009 Introduction to Programming.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Variable Scope. When you declare a variable, that name and value is only “alive” for some parts of the program  We must declare variables before we use.
I/O Errors 1 Computer Organization II © McQuain RAID Redundant Array of Inexpensive (Independent) Disks – Use multiple smaller disks (c.f.
CS0004: Introduction to Programming
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Conditionals, Block Statements, Style
Chapter 9: Value-Returning Functions
Java Programming Fifth Edition
Fundamentals of PL/SQL part 2 (Basics)
Encapsulation, Data Hiding and Static Data Members
Functions Review.
Chapter 5 Conclusion CIS 61.
CNIT 133 Interactive Web Pags – JavaScript and AJAX
Accumulators in Programming
Topic 5 for Loops -Arthur Schopenhauer
Elements of the PHP Programming Environment
Functions and Procedures
Lesson 16: Functions with Return Values
CS 190 Lecture Notes: Wrapup
Lesson 5: Building an App: Clicker Game
3 Control Statements:.
Writing Functions.
Loop Strategies Repetition Playbook.
Namespaces How Shall I Name Thee?.
Let’s all Repeat Together
Welcome back to Software Development!
Week 4 Lecture-2 Chapter 6 (Methods).
CMPE212 – Reminders The other four assignments are now posted.
Building Java Programs
Scope J. Michael Moore.
Welcome back to Software Development!
Base.
Templates Generic Programming.
Designing Software.
Scope Scope is the space within which a variable label exists and can be used. Python talks of names being bound to the code in an area and that determining.
If-Statements and If/Else Statements
The Three Attributes of an Identifier
3.2 Working with Data Scope of variables 29/07/2019.
How to allow the program to know when to stop a loop.
while Loops Looping Control Statement
Algebra 1 Glencoe McGraw-Hill Malinda Young
Parameters and Arguments
The for Loop © 2018 Kris Jordan.
Presentation transcript:

The lifespan of a variable Scope The lifespan of a variable

Scope Not every variable is available everywhere Principle: Avoids: Name collisions (15 things all called x) Needless complexity Principle: Make variables available only where needed

Scope Scope : where a variable is available Scope defined by the block { } a variable is in x available anywhere in this block disappears at line 16

Scope Scope : where a variable is available Scope defined by the block a variable is in y only available in the if statement block disappears at line 12 Line 14 error!!

Scope Can use variable in surrounding scope(s)

Scope Variables in different scopes can have same name BAD IDEA

Scope Problem Might need value from calculated in a narrower scope:

Bad Scope Solution Bad idea: do all other work inside if/else Repeating code is a sign of design failure!!

Better Scope Solution Declare variable in scope you want it Modify it in if/else

Design Principle 1 General Principle: Don't declare things until you need them Use maximally narrow scope Yuck Too much info up front

Maximally Narrow Scope Better Variables appear when needed

Design Principle 2 Don't repeat same work in multiple places Yuck Same work in two places

Do Not Repeat Work If it always happens, keep it out of the if Better Factor our common code to before or after if