Title: Wavelet transform And Its Applications to Image Processing
1Learning the C language 3. The Nuts and Bolts
of C (4) 10 September 2008
23.3 Expressions, Statements, and Blocks
3A Typical Program
include ltiostreamgt int abc (...) ...
return ... int cde (...) ... return
... int main() ... return 0
function
statements
function
function
4Statements
- A C program comprises a number of functions.
- A function comprises a number of statements.
- A statement can be as simple as
- Can be as complicated as
- A statement must end with a semicolon
- In a statement, space carries no information.
x a b
if (choice Sunday choice Saturday) cout
ltlt "\nYou're already off on weekends!\n"
x a b
?
x a b
5Expressions
- A statement comprises one or many expressions.
- Anything that returns a value is an expression.
- Examples of expressions
- 3.2 // Returns the value 3.2
- studentPerClass // Returns a constant, say 87
- x a b // Here, two expressions
- // Return a b and return the value of a
variable x - Since x a b is an expression (i.e. return a
value) , it can certainly be assigned to another
variable. - For example,
- y x a b // assign y to the value of x
which is - // equal to the sum of a and b
6Operators
- An expression often comprises one of more
operators. - The assignment operator '' assigns a value to a
variable or constant, e.g. x 39 - Several mathematical operators are provided by
C. - Let a be 3, b be 4 and x is declared as an
integer,
Operators Meaning Add - Subtract
Multiply / Divide modulus
Examples x a b // x 7 x a - b // x
-1 x a b // x 12 x a / b // x 0
(why?) x a b // x 3 (why?)
3 modulo 4 is 3
7Integer and Floating Point Divisions
include ltiostreamgt using namespace std void
intDiv(int x, int y) int z x/y cout ltlt "z
" ltlt z ltlt endl void floatDiv(int x, int
y) float a (float)x // old style float b
static_castltfloatgt(y) // ANSI style float c
a/b cout ltlt "c " ltlt c ltlt endl int
main() int x 5, y 3 intDiv(x,y) floatDiv
(x,y) return 0
x, y of main() are not x, y of intDiv(int, int)
Casting Ask the compiler to temporarily consider
x as a floating point no., i.e. 5.0 in this case.
Only give integer division result, i.e. 1
Give floating point division result, i.e. 1.66...
8Shorthand of Expressions
- For some commonly used expressions, there are
some shorthands. - c c 1 ? c 1 ? c ? c
//increment - c c - 1 ? c - 1 ? c-- ? --c
//decrement - c c 2 ? c 2
- c c - 3 ? c - 3
- c c 4 ? c 4 // there is no c or
c//, - c c / 5 ? c / 5 // since it is
meaningless - Let c be an integer with value 5. Note that
- a c // a 5, c 6 since a c first and
then c - a c // a 6, c 6 since c first and
then a c
9Precedence and Parentheses
- Mathematical operations are basically performed
from left to right. - It follows the normal mathematical precedence
that multiplication / division first and addition
/ subtraction next. - Hence
- To change the precedence, we can use parentheses.
- Parentheses can be nested as in normal
arithmetic.
x 5 3 8 // x 29 since we evaluate 3
8 first
x (5 3) 8 // x 64 since we evaluate 5
3 first
x 5 ((3 2) 3 2) // x 5 (5 6)
55
10Exercise 3.3a
include ltiostreamgt int main() int myAge
39 // initialize two integers int
yourAge 39 cout ltlt "I am " ltlt myAge ltlt "
years old.\n" cout ltlt "You are " ltlt yourAge
ltlt " years old\n" myAge //
postfix increment yourAge // prefix
increment cout ltlt "One year passes...\n"
cout ltlt "I am " ltlt myAge ltlt " years old.\n"
cout ltlt "You are " ltlt yourAge ltlt " years
old\n" cout ltlt "Another year passes\n"
cout ltlt "I am " ltlt myAge ltlt " years old.\n"
cout ltlt "You are " ltlt yourAge ltlt " years
old\n" cout ltlt "Let's print it again.\n"
cout ltlt "I am " ltlt myAge ltlt " years old.\n"
cout ltlt "You are " ltlt yourAge ltlt " years
old\n" return 0
11Relational Operators
- Besides arithmetic operations, we can perform
relational operations with C. - A number of relational operators are defined in
C. - Each relational operator returns a bool result
(true or false). - In C, zero is considered false, and all other
values are considered true. - True is usually represented by 1.
12Relational Operators
13Applications of Relational Operators
- Relational operators are often used with if
statement. - The if statement provides branching of the
program execution depending on the conditions. - int bigNumber 10, smallNumber 5
- bool bigger bigNumber gt smallNumber //bigger
true - if (bigger)
- doSomeThing()
-
Or you can simply write if (bigNumber gt
smallNumber) doSomeThing()
14Logical Operators
- Logical operators do logical operations for bool
variables. - Each logical operation returns a bool result
(true or false).
Operators Meaning AND OR ! NOT
Examples expression1 expression2 expression1
expression2 !expression
int bigNum 10, smallNum 5 if (bigNum lt 10
smallNum gt 0) doSomeThing()
Will doSomeThing() be called???
NO
15int bigNum 10, smallNum 5 if ((bigNum lt 10
smallNum gt 0) bigNum gt smallNum) doSomeT
hing()
F
T
Will doSomeThing() be called???
YES
int bigNum 10, smallNum 5 if (!(bigNum gt
smallNum)) doSomeThing()
Will doSomeThing() be called???
NO
16Exercise 3.3b
The following program asks user to enter 3
numbers. If the sum of the first two is equal to
the last one, a message is printed to the
console. a. Build the project and note the
result. Does it perform correctly? b. If not,
fix the problem and re-build the program.
include ltiostreamgt using namespace std int
main() int a, b, c cout ltlt "Enter 3
numbers \n" cin gtgt a cin gtgt b cin gtgt
c if (c (ab)) cout ltlt "c ab\n"
return 0
17Acknowledgment
- The slides are based on the set developed by Dr.
Frank Leung (EIE). - Teach Yourself C in 21 Days, Second Edition
(http//newdata.box.sk/bx/c/)
18Reading
- Read Hours 4-5 in 24 hours