Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compound Statements A Quick Overview

Similar presentations


Presentation on theme: "Compound Statements A Quick Overview"— Presentation transcript:

1 Compound Statements A Quick Overview
Copyright © Curt Hill

2 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 © Curt Hill

3 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 © Curt Hill

4 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 © Curt Hill

5 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 © Curt Hill

6 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 © Curt Hill

7 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 © Curt Hill

8 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 © Curt Hill

9 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 © Curt Hill

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

11 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 © Curt Hill

12 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 © Curt Hill

13 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 © Curt Hill

14 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 © Curt Hill

15 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 © Curt Hill

16 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 © Curt Hill


Download ppt "Compound Statements A Quick Overview"

Similar presentations


Ads by Google