Scope of variables class scopeofvars {

Slides:



Advertisements
Similar presentations
CS110 Programming Language I
Advertisements

Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
CSCI S-1 Section 5. Deadlines for Problem Set 3 Part A – Friday, July 10, 17:00 EST Parts B – Tuesday, July 14, 17:00 EST Getting the code examples from.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of 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.
Hello, world! Dissect HelloWorld.java Compile it Run it.
L EC. 02: D ATA T YPES AND O PERATORS (1/2) Fall Java Programming.
1 Methods Instructor: Mainak Chaudhuri
Introduction to Objects A way to create our own types.
Control Structures if else do while continue break switch case return for.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
More arrays Primitive vs. reference parameters. Arrays as parameters to functions.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
1 Variables. 2 Receipt example What's bad about the following code? public class Receipt { public static void main(String[] args) { // Calculate total.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
August 6, 2009 Data Types, Variables, and Arrays.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.
Indentation & Readability. What does this program do? public class Hello { public static void main ( String[] args ) { //display initial message System.out.println(
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Building java programs, chapter 3 Parameters, Methods and Objects.
Review :chapters What is an algorithm? A step by step description of how to accomplish a task. For example, Adding 3 numbers N1, N2 and N3 Add.
The for loop.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Staples are our staple Building upon our solution.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
CS 160 – Summer 16 Exam 1 Prep.
Chapter 2 Clarifications
Suppose we want to print out the word MISSISSIPPI in big letters.
CS0007: Introduction to Computer Programming
The Lifetime of a Variable
AKA the birth, life, and death of variables.
Department of Computer Science
Building Java Programs
Primitive Data, Variables, Loops (Maybe)
Building Java Programs
Something about Java Introduction to Problem Solving and Programming 1.
Control Statement Examples
Computing Adjusted Quiz Total Score
Lecture 11 C Parameters Richard Gesick.
TO COMPLETE THE FOLLOWING:
An Introduction to Java – Part I, language basics
160 Exam 2 Prep.
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Code Animation Examples
Introduction to Programming
CS2011 Introduction to Programming I Methods (II)
Sampath Kumar S Assistant Professor, SECE
AKA the birth, life, and death of variables.
Lecture 5: For Loops Building Java Programs: A Back to Basics Approach
class PrintOnetoTen { public static void main(String args[]) {
Factoring if/else code
Building Java Programs
Building Java Programs
Arrays in Java.
CSE 142 Lecture Notes Global Constants, Parameters, Return Values
Building Java Programs
Scope scope: The part of a program where a variable exists. From its declaration to the end of the { } braces A variable declared in a for loop exists.
Suggested self-checks:
Drawing complex figures
Building Java Programs
Building Java Programs
Methods (a.k.a functions)
CIS 110: Introduction to Computer Programming
Presentation transcript:

Scope of variables class scopeofvars { public static void main(String args[]) { int x; // known to all code within main x = 10; if(x == 10) { // start new scope int y = 20; // known only to this block // x and y both known here. System.out.println("x and y: " + x + " " + y); x = y * 2; } // y = 100; // Error! y not known here // x is still known here. System.out.println("x is " + x);

Scope of variables contd class lifetime1 { public static void main(String args[]) { int x; for(x = 0; x < 3; x++) { int y = -1; // y is initialized each time block is entered System.out.println("y is: " + y); // this always prints -1 y = 100; System.out.println("y is now: " + y); }

Scope of variables contd class lifetime2 { public static void main(String args[]) { int count; for(count = 0; count < 10; count = count+1) { System.out.println("This is count: " + count); int count; // illegal!!! for(count = 0; count < 2; count++) System.out.println("This program is in error!"); }

Methods in problem solving Often we think of solving a large problem in parts Computing the number of digits in n! involves two major steps: computing k=n! and computing the number of digits in k So I can have two “procedures”, one computes n! and the other computes the number of digits in it Such procedures are called methods These are just like functions A method may or may not produce a value The type of the produced value determines the “return type” of a method

Return and parameter type Can be int, float, String, double, char, or void We have seen one method so far: main It does not produce any value: void public static void PrintMyName () { System.out.println(“Tintin”); } Every method must be a part of a class

PrintMyName class anExample { public static void main (String arg[]) { PrintMyName(); // Method call } public static void PrintMyName () { // Method declaration System.out.println(“Tintin”);

Method parameters Methods can have parameters also class anExample { Just like functions class anExample { public static void main (String arg[]) { String myName = “Tintin”; PrintMyName(myName); } public static void PrintMyName (String s) { System.out.println(s);