Friday, December 15, 2006 - PowerPoint PPT Presentation

1 / 32
About This Presentation
Title:

Friday, December 15, 2006

Description:

Friday, December 15, 2006 'When you come to a fork in the road, take it.' - Yogi Berra. ... A block in C is created by placing a sequence of statements ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 33
Provided by: Erud
Category:
Tags: berra | december | friday | yogi

less

Transcript and Presenter's Notes

Title: Friday, December 15, 2006


1
Friday, 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.

5
Good 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
  • !

8
Implementing 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

9
Boolean expressions
Logical AND is represented by Example.
10
Boolean 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!

11
Boolean 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

12
Boolean expressions
  • Given x3 and y4. What's the value of
  • !(( (x lt y) (x gt y-2) ) ( x2 lt y ) )

13
Boolean expressions
  • Given x3 and y4. What's the value of
  • coutltlt !(( (x lt y) (x gt y-2) ) ( x2 lt y )
    )
  • prints 1

14
Boolean expressions
  • What's wrong with these?
  • (y gt x) (z gt x)
  • (z !gt y)
  • (a lt x lt b)
  • !(x 3)

15
Boolean 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?
16
If 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
17
If 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

18
If 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

19
If selective execution
  • What's wrong with this?
  • if (x gt y)
  • x y
  • cout ltlt x

20
If selective execution
Pitfall There is no semi-colon after the
if-condition!
21
Stringing if-then-elses together
  • If-then-else statements can be strung together.
  • Exactly one of the alternatives will be executed.

22
Stringing 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
23
Another 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
24
If 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

25
While 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

26
Nested 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

27
Nested 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)
28
Nested 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/
29
Nested 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
30
Nested 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
31
Nested 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
  • Output is 4
Write a Comment
User Comments (0)
About PowerShow.com