Control Statements (Iteration-II) - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Control Statements (Iteration-II)

Description:

... is initialized once, and only one copy is created even ... Class work. Write program that print out the table of multiplication by using nested loop like below. ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 29
Provided by: Too94
Category:

less

Transcript and Presenter's Notes

Title: Control Statements (Iteration-II)


1
Control Statements (Iteration-II)
  • Lesson 7

DEPARTMENT OF COMPUTER SCIENCE FACULTY OF SCIENCE
AND TECHNOLOGY ASSUMPTION UNIVERSITY
2
Agenda
  • Understanding scope of variables
  • Nested loops

3
Variables
  • All variables have two important attributes which
    are scope and storage
  • The scope of a variable is the area of the
    program where the variable is valid
  • The storage of the variable explains how long
    does the variable will stay in the memory

4
Blocks
  • A block is a section of code enclosed in curly
    braces ( )
  • e.g.
  • int main()
  • //something here
  • return 0

Block
5
Local Variables
  • Local Variables are variables that are declared
    in a block

int main() int n //n is a local
variable return 0
6
Local Variables (cont.)
  • A local variables scope is limited to the block
    where it is declared and cannot be accessed
    outside the block
  • int main()
  • int no //no is a local variable to this block
  • no //LEGAL!
  • no //ILLEGAL!
  • no //ILLEGAL!

7
Local Variables (cont.)
  • Notice that in for loops, local variables can be
    created and used, and you have to be careful
    about its scope

for (int count 0 count lt MAX count)
sum count // count is out of scope from
here on. cout ltlt count //ILLEGAL!
8
Local Variables (cont.)
  • Local Variables that are not static are
    considered as temporary variables
  • Each time the block is entered, the temporary
    variables are initialized if required
  • Temporary variables are allocated from a section
    of memory called the stack at the beginning of
    the block
  • The space used by the temporary variables is
    returned to the stack at the end of the block

9
Global Variables
  • Global Variables are special variables that are
    valid from declaration until the end of the
    program
  • int global //global variable
  • int main()
  • global //can be used everywhere

10
Global Variables (cont.)
11
Global Variables (cont.)
  • It is possible to declare a local variable with
    the same name as a global variable
  • This is called naming clash
  • However the order which C/C understands the
    name is to pick names from the most local space
    first and gets back until it reaches global

12
Global Variables (cont.)
  • Observe count in the next example which one is
    the global, another a local variable

13
Global Variables (cont.)
  • Global Variables are always permanent in storage
  • They are created and initialized before the
    program starts and remain until it terminates

14
Static Variables (Optional)
  • By adding an optional static keyword in front of
    your variable will cause an additional define
    about how your variable will behave in your
    program.
  • The table in the next slide will explain how it
    would behave in different situations?

15
(No Transcript)
16
Nested loops
  • When one loop structure is completely contained
    in the body of another, the loops are said to be
    nested.
  • That is, in the body statement of a loop can be
    another loop.
  • A nested loop compose by outer loop and inner
    loop (s).

17
Design nested loops
  • Begin with an outer loop
  • In the body of the outer loop, where the inner
    loop appears, make it as a separate module.

18
Nested while loops
  • In nested while loops, the entire loop body is
    executed for each of the values of the outer loop
    control variable.
  • Thus for each value of the outer loop control
    variable, the inner while loop will run through
    all of its value.

19
Nested while loops
  • //initialize outer loop
  • while ( outer loop condition )
  • //outer loop statement
  • . . .
  • //initialize inner loop
  • while ( inner loop condition )
  • //inner loop statement
  • . . .

20
Nested while loops Example
include ltiostreamgt using namespace std int
main() int n cout ltlt "Input
times" while(cin gtgt n) while(n gt 1)
cout ltlt "Hello\n" --n
cout ltlt "Input times"
exit(1) system("PAUSE") return 0
Outer loop
Inner loop
End of Inner loop
End of outer loop
21
Nested for loops
  • In nested for loops, the entire loop body is
    executed for each of the values of the outer loop
    control variable.
  • Thus for each value of the outer loop control
    variable, the inner for loop will run through all
    of its value.

22
Nested for loops
  • //initialize outer loop
  • for(LCV value test update)
  • for(LCV value test update) //inner loop
  • //inner loop statement(s)

23
Nested for loops Example
include ltiostreamgt using namespace std int
main() for ( int k 1 k lt 5 k)
for ( int j 1 j lt 3 j) cout ltlt k
j ltlt endl system ("PAUSE") return
0
24
Class work
  • Write program that print out the table of
    multiplication by using nested loop like below .
  • 1 2 3 4 5
  • 2 4 6 8 10
  • 3 6 9 12 15
  • 4 8 12 16 20
  • 5 10 15 20 25

25
Class work
  • Printout the following patterns (a) to (d), by
    asking the user to key in the number of rows
    (columns)
  • (a) (b) (c) (d)

26
Problem solving using loops and conditionals
  • Display both biggest and smallest numbers from a
    set of user inputs until -1 by the user (-1 is
    omitted from the final result).

27
Class Exercise
  • Write a program to print out all prime numbers
    including 2-1000.

28
Loop Testing and Debugging
  • Test data should test all sections of the
    program.
  • Beware of infinite loops -- program doesnt stop
  • Check loop termination condition, and watch for
    off-by-1 problem.
  • Use get function for loops controlled by
    detection of \n character
  • Use algorithm walk-through to verify pre- and
    post conditions.
  • Trace execution of loop by hand with code
    walk-through
  • Use a debugger to run program in slow motion or
    use debug output statements
Write a Comment
User Comments (0)
About PowerShow.com