Title: Friday, December 15, 2006
1Friday, December 15, 2006
- When you come to a fork in the road, take it.
- - Yogi Berra.
2- Class on Saturday (Tomorrow)
3- How do we define a block in C?
4- A block in C is created by placing a sequence
of statements between curly braces.
5Good programming practices
- Meaningful Variable names
- Code indentation
- Comments
- There will be marks for style in
homeworks/assignments.
6 Terminology
- Syntax errors
- reported by the compiler
- Linker errors
- reported by the linker
- Execution/Run-time errors
- reported by the operating system
- Logic errors
- not reported
7- Relational Operators
- gt
- gt
- lt
- lt
-
- !
- Logical Operators
-
-
- !
-
8Implementing the Branch
- if-else statement is used in C to perform a
branch - if (hours gt 40)
- gross_pay rate 40 1.5 rate
(hours-40) -
- else
-
- gross_pay rate hours
-
9Boolean expressions
Logical AND is represented by Example.
10Boolean expressions
- Pitfall Do not use strings of inequalities. They
may be legal C code but will not do what you
expect them to do. - int x2, y9, z3
- //Is y is between x and z?
- coutltlt(x lt y lt z)ltltendl //WRONG!
11Boolean expressions
- Pitfall Do not use strings of inequalities. They
may be legal C code but will not do what you
(for now) expect them to do. - int x2, y9, z3
- //Is y is between x and z?
- coutltlt(x lt y lt z)ltltendl //WRONG!
- Instead, you have to write
- coutltlt ((x lt y) (y lt z)) ltltendl
12Boolean expressions
- Given x3 and y4. What's the value of
- !(( (x lt y) (x gt y-2) ) ( x2 lt y ) )
13Boolean expressions
- Given x3 and y4. What's the value of
- coutltlt !(( (x lt y) (x gt y-2) ) ( x2 lt y )
) - prints 1
14Boolean expressions
- What's wrong with these?
- (y gt x) (z gt x)
- (z !gt y)
- (a lt x lt b)
- !(x 3)
15Boolean expressions
I want to put some balls in a box if the number
of balls in not 3 or 5. How do we write the
statement in C?
16If selective execution
Example Bank withdrawal checker int
balance // some other code here int
withdrawal cout ltlt "Please enter the withdrawal
amount " cin gtgt withdrawal if(withdrawal lt
balance) cout ltlt "Here is your money" ltlt
endl else cout ltlt "Sorry, insufficient
balance" ltltendl
17If selective execution
- What is wrong here?
-
- int age
- cout ltlt "Please enter your age "
- cin gtgt age
- if (age lt 0)
- cout ltlt "Wow, you are really young!"
- cout ltlt "Your age is negative."
- cout ltlt endl
- cout ltlt "Your age is " ltlt age ltlt endl
18If selective execution
- Pitfall Indentation by itself is not enough.
-
- int age
- cout ltlt "Please enter your age "
- cin gtgt age
- if (age lt 0)
- cout ltlt "Wow, you are really young!"
- cout ltlt "Your age is negative."
- cout ltlt endl
- cout ltlt "Your age is " ltlt age ltlt endl
19If selective execution
- What's wrong with this?
- if (x gt y)
- x y
- cout ltlt x
-
20If selective execution
Pitfall There is no semi-colon after the
if-condition!
21Stringing if-then-elses together
- If-then-else statements can be strung together.
- Exactly one of the alternatives will be executed.
22Stringing if-then-elses together
if(num_credits lt 0) cout ltlt "Come on,
get real" ltlt endl else if (num_credits lt
12) cout ltlt "Part-time student" ltlt
endl else if (num_credits lt 18)
cout ltlt "Full-time student" ltlt endl else
cout ltlt "Glutton for punishment" ltlt
endl
23Another way of writing this?
if(num_credits lt 0) cout ltlt "Come on,
get real" ltlt endl else if (num_credits lt
12) cout ltlt "Part-time student" ltlt
endl else if (num_credits lt 18)
cout ltlt "Full-time student" ltlt endl else
cout ltlt "Glutton for punishment" ltlt
endl
24If selective execution
- int main ()
- int age
- cout ltlt "Please enter your age "
- cin gtgt age
- if(age lt 0)
- cout ltlt "Age must be gt 0\n"
- return 0
-
- cout ltlt "Your age is " ltlt age ltlt endl
- return 0
-
25While loop
- int main ()
- int age
- cout ltlt "Please enter your age "
- cin gtgt age
- while(age lt 0)
- cout ltlt "Age must be gt 0\n"
- cout ltlt "Please enter your age again "
- cin gtgt age
-
- cout ltlt "Your age is " ltlt age ltlt endl
- return 0
-
26Nested if statements
- The ltstatementsgt inside the braces can contain
any valid C statements, including if
statements! - // some other code here
- char answer
- if (withdrawal gt balance)
-
- cout ltlt "Insufficient funds." ltlt endl
- cout ltlt "Do you want to see your balance?
" -
- cin gtgt answer
- if (answer 'y')
- coutltlt "Your balance is "ltltbalanceltlt
endl -
- else
-
- cout ltlt "Here is your money." ltlt endl
-
- cout ltlt "Good bye." ltlt endl
27Nested if statements
if (xgty) if (xgtz) statement1 if
(xgtp) statement2 else statement3 else statemen
t4 //bad style- no indentation (code with proper
indentation on next slide)
28Nested if statements
if (xgty) if (xgtz) statement1 if
(xgtp) statement2 else
statement3 else statement4 /else statement
always refers to nearest if statement that is
within same block as else and not already
associated with another else/
29Nested if statements // what is wrong here?
int main() int x10, y2, z12, p13 if
(xgty) if (xgtz) coutltlt1ltltendl//statement1
if (xgtp) coutltlt2ltltendl//statement2 els
e coutltlt3ltltendl//statement3 else
coutltlt4ltltendl//statement4 else
coutltlt5ltltendl//statement5 return 0
30Nested if statements // what is wrong here?
int main() int x10, y2, z12, p13 if
(xgty) if (xgtz) coutltlt1ltltendl//statement1
if (xgtp) coutltlt2ltltendl//statement2 els
e coutltlt3ltltendl//statement3 else
//Error coutltlt4ltltendl//statement4 else
coutltlt5ltltendl//statement5 return 0
31Nested if statements // what is output here?
int main() int x10, y2, z12, p13 if
(xgty) if (xgtz) coutltlt1ltltendl//statement1
if (xgtp) coutltlt2ltltendl//statement2 els
e coutltlt3ltltendl//statement3 els
e coutltlt4ltltendl//statement4 else
coutltlt5ltltendl//statement5 return 0
32