Title: Wavelet transform And Its Applications to Image Processing
1Learning the C language 3. The Nuts and Bolts
of C (5) 10 September 2008
23.4 Program Flow Control
3Control of Program Flow
- Normal program execution is performed from
top-to-down and statement-by-statement. - It is often that the program modifies the program
flow depending on some conditions set by the
programmer or user. - C provides many approaches to control program
flow.
if (cond) // if the cond doA() // is
true, // then do doA() else // else
do doB() doB()
if ... else
switch ... case ... break
switch (expression) case value1 doA()
break case value2 doB() break
4include using namespace std int
main() int firstNum, secondNum 10 cout
firstNum
cout secondNum)
if ((firstNum secondNum) 0) if
(firstNum secondNum) cout same!\n" else cout divisible!\n" else cout evenly divisible!\n" else cout The second no. is larger!\n" return 0
Nested if ... else
5Using braces with if else
Whats wrong with this program? How do you solve
the problem?
include using namespace std int
main() int x cout or x 100 " cin x cout 10) if (x 100) cout thanks!\n" else // not the else
intended! cout thanks!\n" return 0
6switch case break
include using namespace std int
main() unsigned short int number cout "Enter a number between 1 and 5 " cin
number switch (number) case 0 cout "Too small, sorry!" break case 3 cout "Excellent!\n" //falls through case 2 cout "Masterful!\n" //falls through case 1 cout "Incredible!\n" break default cout large!\n" break cout return 0
Only accept number or expression that returns a
number
See the differences between break and without
break
Do the default if all tests fail.
7Implement switch with if else
- switch case statement can be implemented by the
if else statement but with more complicated
structure.
if (num 0) cout sorry!" else if (num 3 num 2 num
1) cout (num 3 num 2) cout "Masterful!\n" if (num 3)
cout cout
- However, switch case can only be applied to
simple numerical tests.
8Looping
- Many programming problems are solved by
repeatedly acting on the same data. - The method for achieving repeated execution is by
looping. - C provides many approaches to implement looping
- if goto // old way, strongly NOT recommended
- while loop // use when the testing parameters are
// complicated - do-while loop // use when the repeated part is
expected // to be executed at least once - for loop // use when the testing parameters are
// simple
9while Loops
- while loops do the looping until the testing
condition fails.
include using namespace std int
main() int counter 0 //initialize
counter while (counterof the loop cout "\n" cout counter
Test condition (tested variable must change
inside the loop)
5
Repeat this part until counter 5
10More complicated while Loops
- while loops can be used with a more complicated
testing condition.
include using namespace std int
main() unsigned long small, large unsigned
short const MAXSMALL 65535 cinsmall
cinlarge while (small 0
small 0) cout 5000 small large - 2 return 0
3 tests for each loop
Testing parameters are updated in every loop
11do while
- while loops do the test first and then the loop.
- Sometimes we would like to have the loop to be
done at least once. In this case, do while can
be used.
include using namespace std int
main() int counter cout " cin counter do cout "Hello\n" counter-- while (counter
0) cout endl return 0
Hello will be shown at least once even if user
enters 0 for counter
12for loops
- In many loops, we initialize a testing parameter,
modify the value of the parameter and test the
parameter. - It can be done in a single line by a for loop.
include using namespace std int
main() int counter for (counter0
counter//counter then increment cout
Initialize
Modify
Test
5
13Different varieties of for loops
include using namespace std int
main() for (int i0, j0 ij) cout return 0
Multiple initialization and increments
include using namespace std int
main() int counter 0 for ( counter 5) counter cout cout return 0
Null initialization and increments
14Different varieties of for loops
include using namespace std int
main() int counter 0 // initialization
int max cout max for () // a for loop that doesn't
end if (counter increment else break //end for loop
return 0
Empty for loop
15continue and break
- Keywords continue and break allow one to change
the program flow during looping. - Should be used with caution since it will make
the program hard to understand owing to the
sudden change of direction.
Execution of continue will skip the following
part of the loop but not leaving the loop
include using namespace std int
main() int counter for (counter0
countercounter is " 1) //odd number continue cout "Counter is an even number.\n" return
0
16Nested for loops
include using namespace std int
main() int rows, columns char theChar cout
rows cout many columns? " cin columns cout characters? " cin theChar for (int i0
ij) cout "\n" return 0
More explanation on next page
Nested for loop
Will be executed (rows ? columns) times
17for (int i0 ij"\n"
Assumption rows 2 columns 3 theChar x
Value of i, j
i 0 j ?
i 1 j 3
i 0 j 0
i 1 j 0
Output on the screen
i 0 j 1
i 1 j 1
i 0 j 2
i 1 j 2
i 0 j 3
i 1 j 3
i 2 j 3
18- If a variable is defined in the initialization
part of the for loop, the variable will no longer
exist on leaving the for loop.
It is not an error for Visual Studio .NET 2003.
19Exercise 3.4
For the program on p. 16, a. Build the project
and note the result. b. Try to rewrite the
program using nested while loops instead of the
nested for loops. Which program is more
complicated?