Download presentation
Presentation is loading. Please wait.
Published byBerna Atalay Modified over 5 years ago
1
FOR statement a compact notation for a WHILE e.g. sumgrades = 0;
for (i = 1; i <= 25; i++) { cin >> grade; sumgrades = sumgrades + grade; } syntax: Initialization Test Update for ( InitStatement; Expression1; Expression2 ) statement;
2
Sum the first 100 integers WHILE DO WHILE FOR sum = 0; sum = 0;
i = 1; i = 1; while (i <= 100) do { { sum = sum + i; sum = sum + i; i++; i++; } } while(i <= 100); FOR sum = 0; for (i = 1; i <= 100; i++) sum = sum + i;
3
Nested FOR statement processes in 2 dimensions will print 1 12 123
for (lastnum = 1; lastnum <= 7; lastnum++) { for (number = 1; number <= lastnum; number++) cout << number; cout << endl; } will print 1 12 123 1234 12345 123456
4
Problem: Print out the integers 5, 10, 15…100 each on a separate line
for (i=5; i<100; i=i+5) { cout << i << endl; } i=5; while (i<=100) i=i+5; for (i=1; i<=20; i++) { cout << 5*i << 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 4. Statement 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 << << << << << << endl;
7
Setting Padding This will produce the output: Padding Output
Eliminated coverage of the functional form… the manipulator is more compact and less complicated syntactically. 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 = ; cout << " " << endl; cout << setw(10) << ID << endl; cout << setfill('0'); //pad with zeroes cout << setfill(' '); //reset padding to spaces This will produce the output: 413225
8
Scope The region of program code where it is legal to reference (use) an identifier. Example if (alpha > 3) { int n; cin >> n; beta = beta + n; } The scope of the identifier n is the body of the if statement.
9
Categories of Scope Local scope Global 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 < 0) { int y;
cin >> 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
11
Global Scope (Example)
double d; int main () { int y = 0; cin >> d; if (d < 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 << d; // 3.5 is printed }
13
Scope Rules Global identifier Local 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 () { while (…) int b; }
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
18
Lifetime != Scope int a; char c; int main () { while (…) int b; }
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.