FOR statement - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

FOR statement

Description:

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

Number of Views:10
Avg rating:3.0/5.0
Slides: 19
Provided by: peterdep
Category:

less

Transcript and Presenter's Notes

Title: FOR statement


1
  • FOR statement
  • a compact notation for a WHILE
  • e.g. sumgrades 0
  • for (i 1 i lt 25 i)
  • cin gtgt grade
  • sumgrades sumgrades grade
  • syntax
  • Initialization Test
    Update
  • for ( InitStatement Expression1 Expression2
    )
  • statement

2
  • Sum the first 100 integers
  • WHILE DO WHILE
  • sum 0 sum 0
  • i 1 i 1
  • while (i lt 100) do
  • sum sum i sum sum i
  • i i
  • while(i lt 100)
  • FOR
  • sum 0
  • for (i 1 i lt 100 i)
  • sum sum i

3
  • Nested FOR statement
  • processes in 2 dimensions
  • for (lastnum 1 lastnum lt 7 lastnum)
  • for (number 1 number lt lastnum number)
  • cout ltlt number
  • cout ltlt endl
  • will print
  • 1
  • 12
  • 123
  • 1234
  • 12345
  • 123456

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.
Write a Comment
User Comments (0)
About PowerShow.com