Title: Control Statements (Iteration-II)
1Control Statements (Iteration-II)
DEPARTMENT OF COMPUTER SCIENCE FACULTY OF SCIENCE
AND TECHNOLOGY ASSUMPTION UNIVERSITY
2Agenda
- Understanding scope of variables
- Nested loops
3Variables
- 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
4Blocks
- A block is a section of code enclosed in curly
braces ( ) - e.g.
- int main()
-
- //something here
- return 0
Block
5Local Variables
- Local Variables are variables that are declared
in a block
int main() int n //n is a local
variable return 0
6Local 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!
-
7Local 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!
8Local 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
9Global 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
10Global Variables (cont.)
11Global 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
12Global Variables (cont.)
- Observe count in the next example which one is
the global, another a local variable
13Global Variables (cont.)
- Global Variables are always permanent in storage
- They are created and initialized before the
program starts and remain until it terminates
14Static 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)
16Nested 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).
17Design 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.
18Nested 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.
19Nested while loops
- //initialize outer loop
- while ( outer loop condition )
-
- //outer loop statement
- . . .
- //initialize inner loop
- while ( inner loop condition )
-
- //inner loop statement
-
- . . .
-
20Nested 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
21Nested 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.
22Nested for loops
- //initialize outer loop
- for(LCV value test update)
-
- for(LCV value test update) //inner loop
-
- //inner loop statement(s)
-
-
23Nested 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
24Class 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
25Class work
- Printout the following patterns (a) to (d), by
asking the user to key in the number of rows
(columns) -
-
-
-
-
- (a) (b) (c) (d)
26Problem 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).
27Class Exercise
- Write a program to print out all prime numbers
including 2-1000.
28Loop 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