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.

Slides:



Advertisements
Similar presentations
Copyright 2006 by Pearson Education 1 reading: 4.1 Cumulative sum.
Advertisements

Classes, methods, and conditional statements We’re past the basics. These are the roots.
Moving To Code 3 More on the Problem-Solving Process §The final step in the problem-solving process is to evaluate and modify (if necessary) the program.
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
The switch statement: an N-way selection statement.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
Week 2 - Friday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
The for-statement. Different loop-statements in Java Java provides 3 types of loop-statements: 1. The for-statement 2. The while-statement 3. The do-while-statement.
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Boolean Expressions and Conditionals (If Statements) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.
 The pool rack example could be implemented using a for loop.  It is also possible to write recursive methods that accomplish things that you might.
Java TA Session 1. Software Java Runtime Environment (JRE) java.exe Java Development Kit (JDK) java.exe, javac.exe Version 1.6 = Version 6, Version 1.7.
The dangling-else ambiguity. Previously discussed The Java compiler (translator) consider white space characters (i.e., SPACE, TAB and New line) as insignificant.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Arrays and Array Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
Loops (While and For) CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Assignment statements using the same variable in LHS and RHS.
First Programs CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Introduction Chapter 1 8/31 & 9/1 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
The for loop.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Jeopardy $100 VariablesErrorsLoops Classes and Objects Program Structure $200 $300 $400 $500 $400 $300 $200 $100 $500 $400 $300 $200 $100 $500 $400 $300.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
The if-else statement. Introducing the if-else statement Programming problem: Re-write the a,b,c-formula program to solve for complex number solutions.
Arrays and Array Lists CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Variables, Types, Operations on Numbers CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Decisions (If Statements) And Boolean Expressions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington.
Common Mistakes with Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
Sophomore Scholars Java
Introduction to Java Import Scanner class to use in our program
Chapter 2 Clarifications
CSC111 Quick Revision.
Exercise 1- I/O Write a Java program to input a value for mile and convert it to kilogram. 1 mile = 1.6 kg. import java.util.Scanner; public class MileToKg.
Boolean Expressions and Conditionals (If Statements)
The Lifetime of a Variable
AKA the birth, life, and death of variables.
Chapter 2 Elementary Programming
Exceptions and User Input Validation
Maha AlSaif Maryam AlQattan
Introduction to Methods in java
Repetition-Counter control Loop
Data types, Expressions and assignment, Input from User
TK1114 Computer Programming
First Programs CSE 1310 – Introduction to Computers and Programming
Something about Java Introduction to Problem Solving and Programming 1.
Control Statement Examples
Common Mistakes with Functions
Maximization and Minimization Problems
While Statement.
Methods (Functions) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan and Vassilis Athitsos University of Texas at Arlington.
Programming 2 Decision-Making.
Java Fix a program that has if Online time for Monday’s Program
Self study.
AKA the birth, life, and death of variables.
Variables, Types, Operations on Numbers
Building Java Programs
Building Java Programs
Week 4 Lecture-2 Chapter 6 (Methods).
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
First Programs CSE 1310 – Introduction to Computers and Programming
Consider the following code:
More on iterations using
Scope Rules.
Chapter 6: Methods CS1: Java Programming Colorado State University
Presentation transcript:

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 a program that: – Asks the user to enter an integer. – If the integer is >= 0, it creates a variable result and sets it equal to the square of the integer. – If the integer is < 0, it creates a variable result and sets it equal to 10 * the integer. – It prints out the value of variable result. 2

Variable Scope This version will not run. Why? 3 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { result = number * 10; } System.out.printf("result = %d\n", result); }

Variable Scope This version will not run. Why? Java will complain that "it cannot find symbol" result, on the line shown in red. How do we fix this? 4 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { result = number * 10; } System.out.printf("result = %d\n", result); }

Variable Scope This version will also not run. Why? 5 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }

Variable Scope This version will also not run. Why? Java will complain that "it cannot find symbol" result, on the line shown in red. 6 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }

Lifetime of a Variable What is the reason for these errors? Each variable has a lifetime. – It is born in a line of code. – It dies at some point. To understand the lifetime of variable, we must understand the concept of a block of code. 7 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }

Blocks of Code and Braces A block of code is a chunk of code that: – Starts with an opening brace {. – Ends with the corresponding closing brace }. Example: – What block of code does the highlighted line belong to? 8 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }

Blocks of Code and Braces A block of code is a chunk of code that: – Starts with an opening brace {. – Ends with the corresponding closing brace }. Example: – What block of code does the highlighted line belong to? – The answer is shown in red. 9 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }

Blocks of Code and Braces A block of code is a chunk of code that: – Starts with an opening brace {. – Ends with the corresponding closing brace }. Example: – What block of code does the highlighted line belong to? 10 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }

Blocks of Code and Braces A block of code is a chunk of code that: – Starts with an opening brace {. – Ends with the corresponding closing brace }. Example: – What block of code does the highlighted line belong to? – The answer is shown in red. 11 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }

Birth of a Variable Each variable has a lifetime. – It is born in a line of code. – It dies at some point. Where is a variable born? – At the line where it is declared. 12 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }

Death of a Variable Where is a variable born? – At the line where it is declared. Where does a variable die? – Find the innermost block containing the variable declaration. – The variable dies at the end of that block. 13 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }

Scope of a Variable Where is a variable born? – At the line where it is declared. Where does a variable die? – Find the innermost block containing the variable declaration. – The variable dies at the end of that block. The lines of code where a variable is alive are called the scope of that variable. 14 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }

Variable Scope Where is variable number born? 15 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }

Variable Scope Where is variable number born? – At the line where it is declared. 16 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }

Variable Scope Where is variable number born? – At the line where it is declared. Where does variable number die? 17 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }

Variable Scope Where is variable number born? – At the line where it is declared. Where does variable number die? – The innermost block where containing the declaration of number is shown in red. 18 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }

Variable Scope Where is variable number born? – At the line where it is declared. Where does variable number die? – The innermost block where containing the declaration of number is shown in red. – So, number dies when we get outside that block. – In short, number dies at the end of the program. 19 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }

Variable Scope So, what is the scope of variable number? 20 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }

Variable Scope So, what is the scope of variable number? It is the lines between its declaration and its death. – Shown in red. It cannot be used before or after those lines. This is fine, the scope of number is what it should be. 21 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }

Variable Scope What is the scope of variable result? 22 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }

Variable Scope What is the scope of variable result? In this code, there are two independent variables called result. – The first one has its scope shown in red. – The second one has its scope shown in green. Obviously, none of them is alive at the printf line. – That is why Java complaints. 23 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { int result = number * 10; } System.out.printf("result = %d\n", result); }

Variable Scope What is the scope of variable result in this example? 24 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { result = number * 10; } System.out.printf("result = %d\n", result); }

Variable Scope What is the scope of variable result in this example? It is shown in red on this slide. Obviously, in this example, result is not alive either at the else part or at the printf line at the end. – That is why Java complains. 25 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { result = number * 10; } System.out.printf("result = %d\n", result); }

Variable Scope How do we make this code correct? 26 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { result = number * 10; } System.out.printf("result = %d\n", result); }

Variable Scope How do we make this code correct? We need to make sure that we have a single variable, called result, that is alive: – at the if part. – at the else part. – at the printf statement at the end. 27 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); if (number >= 0) { int result = number * number; } else { result = number * 10; } System.out.printf("result = %d\n", result); }

Variable Scope The solution is to declare result before the if part. What is the scope of result now? 28 import java.util.Scanner; // correct code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); int result; if (number >= 0) { result = number * number; } else { result = number * 10; } System.out.printf("result = %d\n", result); }

Variable Scope The solution is to declare result before the if part. What is the scope of result now? It is shown in red on this slide. 29 import java.util.Scanner; // correct code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); int result; if (number >= 0) { result = number * number; } else { result = number * 10; } System.out.printf("result = %d\n", result); }

Variable Scope We do not have to assign a value to result when we declare it. Java is sure that, when it is time to print the value at the end, result has received a value. How can it be sure? 30 import java.util.Scanner; // correct code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); int result; if (number >= 0) { result = number * number; } else { result = number * 10; } System.out.printf("result = %d\n", result); }

Variable Scope We do not have to assign a value to result when we declare it. Java is sure that, when it is time to print the value at the end, result has received a value. How can it be sure? – Because result is assigned a value both by the if part and by the else part. 31 import java.util.Scanner; // correct code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); int result; if (number >= 0) { result = number * number; } else { result = number * 10; } System.out.printf("result = %d\n", result); }

Variable Scope What is going to happen here? – We removed the else part. 32 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); int result; if (number >= 0) { result = number * number; } System.out.printf("result = %d\n", result); }

Variable Scope What is going to happen here? – We removed the else part. Java will refuse to run this code. Reason: if number < 0, then result never receives a value. Before running a program, Java must prove to itself that all variables receive values before they are used. 33 import java.util.Scanner; // incorrect code public class example1 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.printf("Please enter an integer: "); int number = in.nextInt(); int result; if (number >= 0) { result = number * number; } System.out.printf("result = %d\n", result); }