Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2004-2013 Curt Hill Flow of Control A Quick Overview.

Similar presentations


Presentation on theme: "Copyright © 2004-2013 Curt Hill Flow of Control A Quick Overview."— Presentation transcript:

1 Copyright © 2004-2013 Curt Hill Flow of Control A Quick Overview

2 Copyright © 2004-2013 Curt Hill Up to Now… We can only write programs that execute each and every statement one time only No conditionality and no repetition –We might get repetition by calling a method multiple times Not much call for these types of programs Mostly –Formula evaluations –Simple pictures

3 Copyright © 2004-2013 Curt Hill Flow of control The idea of this is how execution moves through a program Most programming languages support three types of flow of control –Sequential –Decision –Iteration Java is no exception to this

4 Copyright © 2004-2013 Curt Hill Flowchart Representation Sequential DecisionIteration

5 Copyright © 2004-2013 Curt Hill The Three Types Sequential flow –Each statement is executed once and only once Decision –A test determines which path to take –Only one of the paths may be taken –Usually a boolean choosing one of two paths Iteration –A test determines if the path is to be executed zero or more times

6 Copyright © 2004-2013 Curt Hill A few more things Other possibilities do exist, but are not needed Bohm and Jacoppini proved in the 60s that these three are all that is needed to write any program Most languages developed since then have these three in variations All the statements covered so far are sequential

7 Copyright © 2004-2013 Curt Hill Java Flow of Control Has two decision statements –If –Switch Case Has four loops –for (two types) –while –do Also the break and continue statements

8 Copyright © 2004-2013 Curt Hill Flowcharts Again The rectangle represents any series of statements that a single entry point and a single exit point The decision and iteration are also single entry and single exit So in any flowchart a rectangle can be replaced by a decision or iteration This can be done to any depth

9 Copyright © 2004-2013 Curt Hill The Compound Statement The rectangle does have a corresponding construction in the C family of languages – 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

10 Copyright © 2004-2013 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

11 Copyright © 2004-2013 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

12 Copyright © 2004-2013 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

13 Copyright © 2004-2013 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

14 Copyright © 2004-2013 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?

15 Copyright © 2004-2013 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

16 Copyright © 2004-2013 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

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

18 Copyright © 2004-2013 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:

19 Copyright © 2004-2013 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 … }

20 Copyright © 2004-2013 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

21 Copyright © 2004-2013 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

22 Copyright © 2004-2013 Curt Hill A Little History The idea of scope was proposed in 1960 as part of the Algol language It was perceived to be so good that most languages since then have used it However, FORTRAN, COBOL, BASIC all were established before the idea caught on Most newer languages did adopt some variation: –C, C++, Java, Pascal, Ada

23 Copyright © 2004-2013 Curt Hill Why use it? Scope makes large scale programming possible When using libraries a program may have hundreds to millions of variables created by programmers who did not talk to each other Since the scope rules isolate these variables we do not have to worry if this variable name has been used before Java does not need the unlimited scope because most things are declared inside a class

24 Copyright © 2004-2013 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

25 Copyright © 2004-2013 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


Download ppt "Copyright © 2004-2013 Curt Hill Flow of Control A Quick Overview."

Similar presentations


Ads by Google