Block Scope By Greg Butler Purpose The purpose of this presentation is to familiarize the student with the concept of variable scope, as it relates to.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
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.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
Mock test review Revision of Activity Diagrams for Loops,
Unit 08 & 091 Nested Classes Introduction Inner Classes Local Classes Anonymous Classes Exercises.
Access to Names Namespaces, Scopes, Access privileges.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Chapter Eight: Arrays 1.Terms and what they mean 2.Types of arrays -One Dimensional arrays -Two Dimensional arrays -ragged arrays -Parallel arrays 3. Methods.
CS305j Introduction to ComputingNested For Loops 1 Topic 6 Nested for Loops "Complexity has and will maintain a strong fascination for many people. It.
Writing methods. Calling Methods nameOfMethod(parameters) if method returns something, use that value Math.pow(2, 3) returns 8 System.out.println(Math.pow(2,
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Introduction to Methods
Lecture From Chapter 6 & /8/10 1 Method of Classes.
More Arrays Length, constants, and arrays of arrays By Greg Butler.
COMP More About Classes Yi Hong May 22, 2015.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Chapter 5 Loops.
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
1 Scope Scope describes the region where an identifier is known, and semantic rules for this.
The if StatementtMyn1 The if Statement The basic if statement allows your program to execute a single statement, or a block of statements enclosed between.
 The if statement and the switch statement are types of conditional/decision controls that allow your program.  Java also provides three different looping.
Odds and Ends. CS 21a 09/18/05 L14: Odds & Ends Slide 2 Copyright © 2005, by the authors of these slides, and Ateneo de Manila University. All rights.
CS170 ygao JAVA, C4Slide 1. CS170 ygao JAVA, C4Slide 2.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
Printing with for Loops To print a character multiple times, use a for loop. for (int j = 1; j
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Building java programs, chapter 3 Parameters, Methods and Objects.
Nested Classes CompSci 230 S Software Construction.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Catie Welsh February 14,  Program 2 Due Tonight by 11:59pm  Program 3 Assigned 2.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
ITM 3521 ITM 352 Functions. ITM 3522 Functions  A function is a named block of code (i.e. within {}'s) that performs a specific set of statements  It.
Methods. Methods are groups of statements placed together under a single name. All Java applications have a class which includes a main method class MyClass.
Variable scope. Variable Scope Variables do not live forever. Failing to take that into account leads to problems. Let's look at an example. Let's write.
Suppose we want to print out the word MISSISSIPPI in big letters.
The Lifetime of a Variable
AKA the birth, life, and death of variables.
CSCI 161 – Introduction to Programming I William Killian
CompSci 230 S Programming Techniques
Lecture 4 D&D Chapter 5 Methods including scope and overloading Date.
Decision statements. - They can use logic to arrive at desired results
Introduction to Computer Programming Counting Loops 2
Group Status Project Status.
CHAPTER 6 GENERAL-PURPOSE METHODS
Programming 2 Decision-Making.
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
class PrintOnetoTen { public static void main(String args[]) {
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Scope of variables class scopeofvars {
Object Oriented Programming
Method of Classes Chapter 7, page 155 Lecture /4/6.
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Methods/Functions.
Methods (a.k.a functions)
Inner Classes.
Scope Rules.
Presentation transcript:

Block Scope By Greg Butler

Purpose The purpose of this presentation is to familiarize the student with the concept of variable scope, as it relates to blocks.

Objectives The student, given a short answer test, multiple choice test, or programming problem should be able to demonstrate : An understanding of the definition of the term block scope. The ability to determine a variable's scope, based on the principle of block scope.

Definition Block Scope A block is a chunk of code that begins with a { and ends with a } A nested block is also known as an inner block. Surprisingly, inner blocks are inside outer blocks The term local variable applies to those variables declared within a method { } Outer block Any variable delcared here can be used in any associated inner blocks Inner block Any variable delcared here can be used in any associated inner blocks, but not in the outer block A variable or identifier declared within a block "can only be used within that block or in blocks nested within that block

import javax.swing.*; public class DemoBlockScope { static final int CONST_DEMO_SIZE = 6; static int arrayConstants[ ] = new int [CONST_DEMO_SIZE ]; static void ReadOutArray () { for (int index2 = 0; index2 < CONST_DEMO_SIZE; ++index2) { int tempVar = arrayConstants [index2]; System.out.println ("point 2: " + index2); System.out.println (" Element "+ index2 + " is: " + tempVar ) ; } // end for } // end Read Out Array public static void main(String args[]) { int index = CONST_DEMO_SIZE -1; do { arrayConstants [index] = Integer.parseInt (JOptionPane. showInputDialog( "What do you want in the next array element?")); --index; } while (index >=0); // end do-while ReadOutArray(); } //end main } //end class ArrayBasicsOne Example-Block Scope Both of these variables have class scope since they are not declared inside any of the methods within the class. They can be used in any of the blocks nested within the class (e.g. ReadOutArray( ) and main( ). index2 is declared in the arguments section of a "for" statement. Its scope is therefore limited to use within that "for" statement and any blocks that nested in the for statement You can't use "index2" or "tempVar " here, since you are outside the block in which they were declared. You can use "index" here. Index was declared in main()-- the outer block. The block associated with the "do", wherin this statement appears, is nested within the main()- - a inner block. You can't use "index" here, you are outside the main() block, where it was declared. It is local to the main() method

Block Scope Summary If the declaration of a variable occurs outside any methods or other class inner blocks, it has class scope. This means it can be used by any of the methods inside the class. If the declaration of a variable occurs in a method, it is a local variable of that method. It can be used in any blocks embedded in the method (its inner blocks), but not outside the method in which it was declared. In general, you can use a variable in any blocks nested inside the block in which the variable was declared. You cannot use in any block outside the block in which it was declared.