for (lastnum = 1; lastnum = 7; lastnum ) for (number = 1; ... The scope of x is nonlocal to the body. of the while loop. Global Scope (Example) double d; ... – PowerPoint PPT presentation
4 Problem Print out the integers 5, 10, 15100 each on a separate line
for (i5 ilt100 ii5)
cout ltlt i ltlt endl
i5
while (ilt100)
cout ltlt i ltlt endl
ii5
for (i1 ilt20 i)
cout ltlt 5i ltlt end
5
DOCUMENTATION
Each program should contain
1. Comment Section
PROGRAM name of program
AUTHOR your name and SSN
SYSTEM compiler and operating system
DATE date
PURPOSE short description of problem which program solves
2. Header Part
include (all header files)
6
DOCUMENTATION
3. Declaration Part
Use realistic names for constants
Use realistic names for variables (and explain their use)
4. Statement Part
Include about 10 comments on subtasks to be performed
Include blank lines for readability
Use indentation ( 4 spaces)
Include prompts for keyboard input
Line up long cin and cout
e.g. cout ltlt . . . . ltlt . . . . ltlt . . . .
ltlt . . . . ltlt . . . . ltlt endl
7 Setting Padding
Padding Output
- Padding refers to the character used to fill in the unused space in an output field.
- By default the pad character for justified output is the space (blank) character.
- This can be changed by using the setfill() manipulator
int ID 413225 cout ltlt "0123456789" ltlt endl cout ltlt setw(10) ltlt ID ltlt endl cout ltlt setfill('0') //pad with zeroes cout ltlt setw(10) ltlt ID ltlt endl cout ltlt setfill(' ') //reset padding to spaces This will produce the output 0123456789 413225 0000413225 8 Scope
The region of program code where it is legal to reference (use) an identifier.
Example
if (alpha gt 3)
int n
cin gtgt n
beta beta n
The scope of the identifier n is the body of the if statement. 9 Categories of Scope
Local scope
The scope of an identifier declared inside a block extends from the point of declaration to the end of that block.
Global scope
The scope of an identifier declared outside of all blocks extends from the point of declaration to the end of the entire file.
10 Local Scope (Example)
int x
while (x lt 0)
int y
cin gtgt y
x x y
The scope of y is local to the body of the while loop. The scope of x is nonlocal to the body of the while loop. 11 Global Scope (Example)
double d
int main ()
int y 0
cin gtgt d
if (d lt 0)
d y
return 0
The identifier d has a global scope. It can be referenced anywhere in the program below where it is declared, including inside main and in the body of the if statement. What if a second variable named d was declared in main? 12 Name Precedence
When a block declares a local identifier with the same name as a global identifier, the local identifier takes precedence.
Example
double d 1.2 int main() double d 3.5 cout ltlt d // 3.5 is printed 13 Scope Rules
Global identifier
Declaration to the end of the file.
Local identifier
Declaration to the end of the block.
Does not include any nested block that contains a locally declared identifier with the same name.
14 Scope Examples
int a
char c
int main ()
int a
while ()
int b
char c
return 0
15 Variables
Recall that variables in C are made up of four parts
Name
Type
Memory location
Value
16 Lifetime
The period of time during program execution when an identifier has memory allocated to it.
A local variable's lifetime
Starts when entering the block
Ends when the block is exited.
A global variable's lifetime
Duration of the entire program.
17 Lifetime Example
// c is neither in scope, nor has
// memory allocated to it
while ()
// c is allocated memory
char c // c is in scope and has memory
// c's memory is deallocated
// c is neither in scope, nor has
// memory allocated to it
18 Lifetime ! Scope
int a
char c
int main ()
int a
while ()
int b
char c
return 0
The lifetime of global identifiers a and c extends for the duration of the entire program. However, the global variable a is not in scope in main. It is hidden by the local variable a declared in main. Likewise in the while loop, the global variable c has memory allocated to it but is not in scope.
PowerShow.com is a leading presentation sharing website. It has millions of presentations already uploaded and available with 1,000s more being uploaded by its users every day. Whatever your area of interest, here you’ll be able to find and view presentations you’ll love and possibly download. And, best of all, it is completely free and easy to use.
You might even have a presentation you’d like to share with others. If so, just upload it to PowerShow.com. We’ll convert it to an HTML5 slideshow that includes all the media types you’ve already added: audio, video, music, pictures, animations and transition effects. Then you can share it with your target audience as well as PowerShow.com’s millions of monthly visitors. And, again, it’s all free.
About the Developers
PowerShow.com is brought to you by CrystalGraphics, the award-winning developer and market-leading publisher of rich-media enhancement products for presentations. Our product offerings include millions of PowerPoint templates, diagrams, animated 3D characters and more.