week 5 part 2
DESCRIPTION
Week 5 Part 2. Kyle Dewey. Overview. Scope Lifetime Testing Exam #1 overview. What’s with the { ... } ?. Recall. Function definitions look like this:. void foo() { ... }. Conditionals ( if ) look like this:. if ( condition ) { ... }. while loops look like this:. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/1.jpg)
Week 5 Part 2Kyle Dewey
![Page 2: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/2.jpg)
Overview
•Scope
•Lifetime
•Testing
•Exam #1 overview
![Page 3: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/3.jpg)
What’s with the { ... }?
![Page 4: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/4.jpg)
Recall•Function definitions look like this:
void foo() { ... }
•Conditionals (if) look like this:
if ( condition ) { ... }
•while loops look like this:
while ( condition ) { ... }
![Page 5: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/5.jpg)
Brackets
•The { ... } part is significant
•This is called a block
•Blocks have special meaning to C (and to the vast majority of languages)
![Page 6: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/6.jpg)
Blocks•As we’ve already seen, blocks can
be nested:
void foo() { int x; for ( x = 0; x < 10; x++ ) { if ( x % 2 == 0 ) { printf( “Even: %i\n”, x ); continue; } printf( “Odd: %i\n”, x ); }}
![Page 7: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/7.jpg)
Blocks
•Importance of this lies in variable declaration
•A block nested at level N has access to variables defined at nesting levels 0 .. N - 1, but not the other way around
![Page 8: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/8.jpg)
Example
void foo() { int x = 10; if ( x > 5 ) { int y = x * 4; // this block can access x } // ...but this block can’t access y}
![Page 9: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/9.jpg)
So what?
•This may seem obvious and/or insignificant
•This mechanism means that you don’t have to worry about what was defined in inner blocks, because they are inaccessible anyway
![Page 10: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/10.jpg)
Variable Name Reusage
•Blocks help to prevent variable names from clashing
•A variable foo defined in a given block is distinct from all other variables named foo defined in other blocks
![Page 11: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/11.jpg)
Example
int x = ...;
if ( x < 10 ) { int y = 20;} else { int y = 30;}
Distinct variables
![Page 12: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/12.jpg)
Variable Name Reusage
•Consider the following code:
int x = ...;
if ( x < 10 ) { int x = 20;} else { int x = 30;}
Name (x) Reused
![Page 13: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/13.jpg)
Variable Name Reusage•The original variable x does not
change
•The old definition is shadowed by the new one, not overwritten
int x = ...;
if ( x < 10 ) { int x = 20;} else { int x = 30;}
![Page 14: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/14.jpg)
Question
•What does this code print?
int x = 10;if ( x == 10 ) { int x = 5; printf( “%i\n”, x );}printf( “%i\n”, x );
![Page 15: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/15.jpg)
Block Advantage
•Focus only on one block at a time, not on previous blocks
•Variables defined in previous blocks are shadowed
![Page 16: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/16.jpg)
Scope•Scope defines which variables can
be accessed at any given point in the code
•Blocks manipulate the scope
if ( 1 < 2 ) { int x = 5; // x is now in scope}// x is no longer in scope
![Page 17: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/17.jpg)
Scope Exampleint x = 10;// x is now in scope
if ( 1 < 2 ) { int x = 5; // x is in scope, but it // refers to the x = 5 definition}
// x is in scope, but it refers to// the x = 10 definition
![Page 18: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/18.jpg)
Lifetime
•How long a variable exists in your program is the variable’s lifetime
•Scope is not the same as lifetime
•Scope: when you can access a variable
•Lifetime: whether or not a variable is there
![Page 19: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/19.jpg)
Scope vs. Lifetime
•A variable in scope is necessarily alive
•A variable that’s alive is not necessarily in scope
![Page 20: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/20.jpg)
Example #1
int x = ...; // alive and in scopeif ( x < 10 ) { int y = 5; // alive and in scope ...}// y is not in scope and not alive
![Page 21: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/21.jpg)
Example #2int x = ...; // alive and in scopeif ( x < 10 ) { int y = 5; // x and y are alive
// and in scope if ( x < y + 5 ) { int z = 20; // x, y, z alive and in scope } // x, y alive and in scope}// x alive and in scope
![Page 22: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/22.jpg)
Example #3int x = ...; // alive and in scopeif ( x < 10 ) { int x = 5; // x = ... is alive
// but not in scope // x = 5 alive in scope
if ( x < y + 5 ) { int x = 20; // x = ... and x = 5 alive // only x = 20 is in scope } // x = ... and x = 5 alive // only x = 5 in scope}// x = ... alive and in scope
![Page 23: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/23.jpg)
Example #4void bar() { // y is alive but not in scope int z = 5;}
void foo() { int y = 10; bar();}
void main() { foo();}
![Page 24: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/24.jpg)
Global Variables•Consider the following code:
int x = 10;
void foobar() { printf( “%i\n”, x );}
void barfoo() { x++;}
![Page 25: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/25.jpg)
Global Variables•x is a global variable
•Always in scope (unless shadowed)
•Always aliveint x = 10;
void foobar() { printf( “%i\n”, x );}
void barfoo() { x++;}
![Page 26: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/26.jpg)
Thought Question
•Global variables are seen as bad practice, and are usually avoided
•Why?
![Page 27: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/27.jpg)
Answer
•Always in scope and always alive means everything in the file probably heavily relies on it
•Another variable to keep track of for everything in the file
•Can be error prone
•Interdependent code
![Page 28: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/28.jpg)
Aside: “In the File”
•Technically a “compilation unit”
•In this class, a file is a compilation unit
•However, it’s possible to have multiple files in the same compilation unit
![Page 29: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/29.jpg)
Testing
![Page 30: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/30.jpg)
Recall...
•Testing is an important step in software development
•Builds confidence that code works correctly
•Modern software development heavily relies on testing
![Page 31: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/31.jpg)
Testing
•Testing can confirm a bug exists
•...but it cannot confirm that bugs do not exist
•May not be testing for it
•May need additional tests
![Page 32: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/32.jpg)
Testing Weaknessint badMax( int x, int y ) { if ( x == 513 ) { return x; } else if ( x > y ) { return x; } else { return y; }}
![Page 33: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/33.jpg)
Testing Strength
•Code is not usually written like that
•The goal is not to mess up the tests
•Simple (compared to verification, which attempts to prove that there are no bugs)
![Page 34: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/34.jpg)
Additional Terminology
•White box testing: you can see the whole code, as with:
// get the max of x and yint max( int x, int y ) { if ( x > y ) { return x; } else { return y; }}
![Page 35: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/35.jpg)
Additional Terminology
•Black box testing: you can see only the interfaces and what they do, as with:
// get the max of x and yint max( int x, int y );
![Page 36: Week 5 Part 2](https://reader036.vdocuments.site/reader036/viewer/2022062408/56813d07550346895da6aeca/html5/thumbnails/36.jpg)
Exam #1 Overview