Presentation is loading. Please wait.

Presentation is loading. Please wait.

Flow of Control An Overview

Similar presentations


Presentation on theme: "Flow of Control An Overview"— Presentation transcript:

1 Flow of Control An Overview
Copyright © Curt Hill

2 Up to Now… We can only write programs that execute each and every statement one time only No conditionality Repetition only from clicking buttons multiple times Not much call for these types of programs Mostly simple formula evaluations Copyright © Curt Hill

3 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 C/C++/Java is no exception to this Copyright © Curt Hill

4 Flowchart Representation
Decision Iteration Sequential Copyright © Curt Hill

5 The Three Types Sequential flow Decision Iteration
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 Copyright © Curt Hill

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

7 The C family of languages
Includes C/C++/Java Has two decision statements If Switch Case Has three loops for while do Also the break and continue statements Copyright © Curt Hill

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

9 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 Most of the flow of control statements only allow one statement inside This is most often a compound statement Copyright © Curt Hill

10 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 Variables declared in a compound statement last until the end of the compound statement Copyright © Curt Hill

11 Syntax of Compound Statements
{ } Statement This is an example of a syntax graph Copyright © Curt Hill

12 Scope That area where a variable is known
The scope of a variable starts with its declaration It ends with the enclosing brace The three languages have different but similar scope rules A block or compound statement may look at variables in blocks that enclose but may not look into a block Copyright © Curt Hill

13 Scope Rules C only allows declarations at the beginning of a function or globally – only two levels of scope blocks C++ allows declarations anywhere – each compound statement is a scope block Java is similar to C++ but does not allow overlapping declarations Copyright © Curt Hill

14 The Block ALGOL 60 was the first block structured language
Once a block is opened up it becomes a little microcosm It may contain code and variables The variables are limited by the enclosing block Copyright © Curt Hill

15 Compound statement as block
C++ is more block structured than C or Java Variables declared in a compound statement last until end of the compound statement Duplicate variable names are allowed, if they are declared within different blocks A block may use variables that are outside of it Copyright © Curt Hill

16 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 C++ has a rather more enlightened rule Copyright © Curt Hill

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

18 Scope Example Consider the following code: { int a; // first a cin >> a; int b = a * 2; { // new brace : new scope int b = a; // different b cout << b << “ “; } cout << b << “\n”; What will happen? Copyright © Curt Hill

19 Results There were actually two integer variables named b
Defined in different blocks One was defined in the outer braces and the other in the inner If a was equal to 5 then the following would have been displayed: 5 10 5 is inner b and 10 the outer Copyright © Curt Hill

20 Scope Example Consider the following code: { int a; // first a cin >> a; int b = a * 2; { // new brace : new scope int c = a; … } cout << c << “\n”; What will happen? Syntax error: c is undefined Copyright © Curt Hill

21 Finding names If there are two variables with name b, which one will be used? Here is what the compiler does: Look in the current block (compound statement) for the name If found stop looking If not then step out to the next enclosing block Keep doing this until there are no further blocks or the name is found Copyright © Curt Hill

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

23 Scope Blocks int x,z; int y,z; int y; … y = x * z;
Copyright © Curt Hill

24 Notes The above example uses compound statements in an unusual way
Unusual = poor for readability Often true with examples The rule for naming variables is to choose a name which will suggest what it does to the reader A program may have two count variables when the counts are of different things and the context is clear Single letter names are usually only suitable for formulas and temporary variables Copyright © Curt Hill

25 Includes The include statement adds new items to the global scope
Global scope is outside of every function or method This may be a great many new items A name not found by the time of finishing the global scope is undefined Produces a syntax error Copyright © Curt Hill

26 A Little History The idea of scope was proposed in 1960 as part of the Algol language The first block structured 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: C, C++, Java, Pascal, Ada Copyright © Curt Hill

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

28 More Of course there is more to learn We need to understand both:
Scope Resolution Operator Namespaces Copyright © Curt Hill

29 Scope Resolution Operator
The Scope Resolution Operator is the two adjacent colons :: These must have no space between They allow us to look inside a scope block in certain cases Used for looking into named scope blocks Often used for public static methods/properties Copyright © Curt Hill

30 Includes Again Using an include introduces new names into the outermost namespace of your program There must be no duplications Problem: more than h files in the DevC++ include library All others have many as well The reverse problem is insignificant Handled by the block structure Open any { } gives a new empty namespace Thus no name clashes Copyright © Curt Hill

31 Include Problem The problem is that the global block can get cluttered with thousands of things from includes The solution is called a namespace It allows us to structure the global block Some IDEs makes much use of namespaces We use them more in console programs Copyright © Curt Hill

32 Solution is the namespace
The idea is to create and use namespaces (scope blocks) that contain names to be used by programs With the exception of the class all previous namespaces were anonymous Like classes we need to use them now and figure out how to create them later Copyright © Curt Hill

33 Using namespaces Some includes drop their names into the global block of names Others use namespaces Usually if the include has a .h then it drops things into the global namespace If namespace exists, there are two ways to access things in it: Scope resolution operator Using command Copyright © Curt Hill

34 iostream Some systems provides two iostream includes
#include <iostream.h> #include <iostream> The latter uses the namespace std Dev only has the latter This requires either the scope resolution operator or namespaces Copyright © Curt Hill

35 Example #include <iostream> …
int _tmain(int argc, _TCHAR* argv[]) { std::cout << “Enter value\n"; #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { cout << “Enter value\n"; Copyright © Curt Hill

36 Explanation By prefixing cout with std:: we ask the compiler to look inside the std namespace to find cout By the inclusion of: using namespace std; We ask the compiler to search the namespace std after all the local blocks but before the global namespace Copyright © Curt Hill

37 Using The using command inserts the namespace into the scope search path before the global Form: using namespace junk; using is also reserved Copyright © Curt Hill

38 Name search path Consider the fragment:
using namespace x; using namespace y; … {… // A block {… // B block z = u; } } How will the search proceed when looking up u? Copyright © Curt Hill

39 Name search path Consider the fragment:
using namespace x; using namespace y; … {… // A block {… // B block z = u; } } First search local block Copyright © Curt Hill

40 Name search path Consider the fragment:
using namespace x; using namespace y; … {… // A block {… // B block z = u; } } Second search enclosing block Copyright © Curt Hill

41 Name search path Consider the fragment:
using namespace x; using namespace y; … {… // A block {… // B block z = u; } } Third search namespace y Copyright © Curt Hill

42 Name search path Consider the fragment:
using namespace x; using namespace y; … {… // A block {… // B block z = u; } } Fourth search namespace x Copyright © Curt Hill

43 Name search path Consider the fragment:
using namespace x; using namespace y; … {… // A block {… // B block z = u; } } Fifth search global namespace Copyright © Curt Hill

44 Still need :: Suppose namespace x and y both have a name z
We will always find y’s z because it is searched before x If we want the other use scope resolution operator: x::z This changes the search path Copyright © Curt Hill

45 Some Thoughts It is always safe to keep your names unique within a program Upon entering a new compound statement You have a clean slate: any name 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

46 Final thoughts Most of the usings that we use will be generated for us
At the top of many programs written in other IDEs you find namespaces and scope resolution operators You also find scope resolution operators in method definitions Now you have some understanding of why they are there Copyright © Curt Hill


Download ppt "Flow of Control An Overview"

Similar presentations


Ads by Google