Compound Statements A Quick Overview

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
CS305j Introduction to ComputingNested For Loops 1 Topic 6 Nested for Loops "Complexity has and will maintain a strong fascination for many people. It.
Copyright © Texas Education Agency, Computer Programming For Loops.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
 2005 Pearson Education, Inc. All rights reserved. 1 Methods Called functions or procedures in other languages Modularize programs by separating its tasks.
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:
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Copyright Curt Hill A Quick Introduction to Looping Breadth not Depth.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
Copyright © Curt Hill The IF Revisited If part 4 Style and Testing.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
Copyright © Curt Hill The Compound Statement C-Family Languages and Scope.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
CMSC 202 Java Primer 1. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Java-02 Basic Concepts Review concepts and examine how java handles them.
Copyright © Curt Hill Flow of Control A Quick Overview.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
Copyright 2006 by Pearson Education 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.
The for Statement A most versatile loop
Copyright © 2016 Curt Hill Static Code Analysis What it is and does.
The Object-Oriented Thought Process Chapter 03
Chapter 9: Value-Returning Functions
Flow of Control An Overview
The Lifetime of a Variable
Side Effect Operators Changing the value of variables
Lecture 5: Some more Java!
Java Primer 1: Types, Classes and Operators
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Namespaces, Scopes, Access privileges
Passing information to and from a function
Unit 2 Programming.
Doing things more than once
Concepts From Alice Switching to Java Copyright © Curt Hill.
Scope, Visibility, and Lifetime
More On Enumeration Types
Java Programming Arrays
Arrays in Java What, why and how Copyright Curt Hill.
CHAPTER 6 GENERAL-PURPOSE METHODS
CMSC 202 Java Primer 2.
Scope Rules and Storage Types
Copyright © – Curt Hill Bash Flow of Control Copyright © – Curt Hill.
Tonga Institute of Higher Education
The Java switch Statement
CSE 142 Lecture Notes Global Constants, Parameters, Return Values
Suggested self-checks:
Drawing complex figures
Classes, Objects and Methods
Introduction to Programming
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.
Loops and Iteration CS 21a: Introduction to Computing I
The IF Revisited A few more things Copyright © Curt Hill.
CMSC 202 Exceptions 2nd Lecture.
The IF Revisited A few more things Copyright © Curt Hill.
The beginning of media computation Followed by a demo
Looping and Repetition
Methods Scope How are names handled?
Scope Rules.
Methods Coding in Java Copyright © Curt Hill.
While and Do While Syntax, semantics and examples
Presentation transcript:

Compound Statements A Quick Overview Copyright © 2004-2017 Curt Hill

The Compound Statement The compound statement is just a pair of braces { } and any number of statements in between them It is also the executable portion of a method It allows us to wrap many statements and treat them as just one Copyright © 2004-2017 Curt Hill

Compound statements Start with a { and end with a } No semicolon follows the closing brace Any place that one statement can be a compound statement can also be Primitive variables declared in a compound statement last until end of statement Objects may survive, but not their handles Copyright © 2004-2017 Curt Hill

Review Scope That area where a variable is known The scope of a primitive starts with its declaration It ends with the enclosing brace Object handles work the same way, but the object itself may survive the compound statement Java has more complicated scope than some other languages Copyright © 2004-2017 Curt Hill

Duplicate Names Some languages like FORTRAN and many versions of BASIC only allow one instance of a name Thus code like this: int a=2; … double a = 3.4; would be illegal regardless of where in the program these lines occur Java has a rather more complicated rule Copyright © 2004-2017 Curt Hill

A Scope Block A Scope Block is the area of a compound statement Excluding any nested compound statements Blocks may nested Within one scope block all names declared must be unique However, the same name may be used in a different block, provided it does not conflict with another available Copyright © 2004-2017 Curt Hill

Scope Example Consider the following code: { int a=5; // first a int b = a * 2; { // new brace : new scope int c = a; System.out.print(c); System.out.println(“ “); } System.out.print(c); What will happen? Copyright © 2004-2017 Curt Hill

Results Syntax error The c was declared inside a compound statement There was an attempt to use it after the statement The c variable only exists inside the compound statement However, it may access things declared in surrounding compound statements Copyright © 2004-2017 Curt Hill

Finding names How does compiler connect a name with declaration? Here is what the compiler does: Look in the current block (compound statement) for the name If found stop looking If not then go the next enclosing block Keep doing this until there are no further blocks or the name is found Copyright © 2004-2017 Curt Hill

Another Scope Example How will names be found? int x,z; … { int w,y; … { int v; … v = x * y; } … } Three scope blocks Copyright © 2004-2017 Curt Hill

Duplicate Names Unlike its predecessor C++, Java only allows duplicate names where they do not overlap Duplicates may exist, however no code has access to both Consider next screen: Copyright © 2004-2017 Curt Hill

Scope Example Consider the following code: { int a=5; // first a int b = a * 2; { // new brace : new scope int c = a; // one c … } { // new brace : new scope int c = b; // second c … } Copyright © 2004-2017 Curt Hill

Discussion If you are in a scope block, you may look outside to enclosing blocks You may not look inside a scope block The variables do not exist unless execution is inside the block These two c variables did not overlap in range Copyright © 2004-2017 Curt Hill

Another Thought The above examples use compound statements in an unusual way The import statement adds new items to the global scope Perhaps a great many new items A name not found by the time of finishing the global scope is undefined Copyright © 2004-2017 Curt Hill

Classes and Scope We have already seen Math.PI This is a constant defined inside the Math object Classes act like scope blocks as well with an important difference We can look inside at public items This is done by giving first the class name, a dot, then the interior name Copyright © 2004-2017 Curt Hill

Final Thoughts It is always safe to keep your names unique within a program Upon entering a new compound statement Any name not available to you currently may be used It may be attached to any type as well Always use meaningful names One letter names are usually reserved for formulas and control variables in loops Copyright © 2004-2017 Curt Hill