Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

40
Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope

description

Copyright © Curt Hill The Compound Statement The rectangle has 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

Transcript of Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Page 1: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Copyright © 2004-2012 Curt Hill

The Compound Statement

C-Family Languages and Scope

Page 2: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Copyright © 2004-2012 Curt Hill

Recall FlowchartsSequential Decisio

nIteration

Page 3: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Copyright © 2004-2012 Curt Hill

The Compound Statement• The rectangle has 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

Page 4: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Copyright © 2004-2012 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

• Variables declared in a compound statement last until the end of the compound statement

Page 5: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Copyright © 2004-2012 Curt Hill

Syntax of Compound Statements

Compound statement

{ Statement }

This is an example of a syntax graph

Page 6: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Copyright © 2004-2012 Curt Hill

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

Page 7: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

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

Page 8: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Copyright © 2004-2012 Curt Hill

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

Page 9: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Copyright © 2004-2012 Curt Hill

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

Page 10: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Copyright © 2004-2012 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

• C++ has a rather more enlightened rule

Page 11: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Copyright © 2004-2012 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

Page 12: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

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

Page 13: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Copyright © 2004-2012 Curt Hill

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

Page 14: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Copyright © 2004-2012 Curt Hill

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

Page 15: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Copyright © 2004-2012 Curt Hill

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

Page 16: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Copyright © 2004-2012 Curt Hill

Another Scope Example • How will names be found? int x,z;…{ int y,z; … { int y; … y = x * z; } …}

• Three scope blocks

Page 17: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Copyright © 2004-2012 Curt Hill

Scope Blocks

int x,z;

int y,z;int y;…

y = x * z;

Page 18: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Copyright © 2004-2012 Curt Hill

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

Page 19: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Copyright © 2004-2012 Curt Hill

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

Page 20: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Copyright © 2004-2012 Curt Hill

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

Page 21: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Copyright © 2004-2012 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

Page 22: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

More• Of course there is more to learn• We need to understand both:

– Scope Resolution Operator– Namespaces

Copyright © 2004-2012 Curt Hill

Page 23: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

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

Page 24: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Includes Again• Using an include introduces new names

into the outermost namespace of your program

• There must be no duplications• Problem: more than 1000 .h files in the C+

+Builder 2011 include library• Visual Studio has 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 © 2004-2012 Curt Hill

Page 25: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

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• Newer C++ compilers make much

use of namespaces

Copyright © 2004-2012 Curt Hill

Page 26: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

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

Page 27: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

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

Page 28: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

iostream• There have been two iostream

includes in previous compilers– #include <iostream.h>– #include <iostream>

• The latter uses the namespace std• DevC++ only has the latter• This requires either the scope

resolution operator or namespaces

Copyright © 2009 – Curt Hill

Page 29: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Example

Copyright © 2009 – Curt Hill

#include <iostream> …int main(int argc, char* argv[]) {…std::cout << “Enter value\n";

#include <iostream> using namespace std;…int main(int argc, char* argv[]) {… cout << “Enter value\n";

Page 30: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

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

Page 31: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Using• The using command inserts the

namespace into the scope search path before the global

• Form:using namespace junk;

• using is also reserved

Copyright © 2009 – Curt Hill

Page 32: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Name search path

Copyright © 2009 – Curt Hill

Consider the fragment:using namespace x;using namespace y;…{… // A block {… // B block z = u; }}How will the search proceed when looking up u?

Page 33: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Name search path

Copyright © 2009 – Curt Hill

Consider the fragment:using namespace x;using namespace y;…{… // A block {… // B block z = u; }}

First search local block

Page 34: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Name search path

Copyright © 2009 – Curt Hill

Consider the fragment:using namespace x;using namespace y;…{… // A block {… // B block z = u; }}

Second search enclosing block

Page 35: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Name search path

Copyright © 2009 – Curt Hill

Consider the fragment:using namespace x;using namespace y;…{… // A block {… // B block z = u; }}

Third search namespace y

Page 36: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Name search path

Copyright © 2009 – Curt Hill

Consider the fragment:using namespace x;using namespace y;…{… // A block {… // B block z = u; }}

Fourth search namespace x

Page 37: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Name search path

Copyright © 2009 – Curt Hill

Consider the fragment:using namespace x;using namespace y;…{… // A block {… // B block z = u; }}

Fifth search global namespace

Page 38: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

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

Page 39: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Copyright © 2004-2012 Curt Hill

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

Page 40: Copyright © 2004-2012 Curt Hill The Compound Statement C-Family Languages and Scope.

Final thoughts• Most of the usings that we use will

be generated for us• At the top of many Visual Studio

programs you find namespaces and scope resolution operatorsYou also find scope resolution operators in method definitions

• Now you have some understanding of why they are there

Copyright © 2004-2012 Curt Hill