Presentation is loading. Please wait.

Presentation is loading. Please wait.

Friday, December 15, 2006 “When you come to a fork in the road, take it.” - Yogi Berra.

Similar presentations


Presentation on theme: "Friday, December 15, 2006 “When you come to a fork in the road, take it.” - Yogi Berra."— Presentation transcript:

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 l reported by the compiler §Linker errors l reported by the linker §Execution/Run-time errors l reported by the operating system §Logic errors l not reported

7 Relational Operators > >= < <= = != Logical Operators && | !

8 Implementing the Branch §if-else statement is used in C++ to perform a branch if (hours > 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 x=2, y=9, z=3; //Is y is between x and z? cout<<(x < y < z)<<endl; //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 x=2, y=9, z=3; //Is y is between x and z? cout<<(x < y < z)<<endl; //WRONG! Instead, you have to write: cout<< ((x < y) && (y < z)) <<endl;

12 Boolean expressions §Given x=3 and y=4. What's the value of !(( (x y-2) ) && ( x*2 < y ) )

13 Boolean expressions §Given x=3 and y=4. What's the value of cout y-2) ) && ( x*2 < y ) ); prints 1

14 Boolean expressions §What's wrong with these? (y > x) & (z > x) (z !>= y) (a < x < 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 << "Please enter the withdrawal amount: "; cin >> withdrawal; if(withdrawal <= balance) { cout << "Here is your money" << endl; } else { cout << "Sorry, insufficient balance" <<endl; }

17 If: selective execution §What is wrong here? int age; cout << "Please enter your age: "; cin >> age; if (age < 0) cout << "Wow, you are really young!"; cout << "Your age is negative."; cout << endl; cout << "Your age is " << age << endl;

18 If: selective execution §Pitfall: Indentation by itself is not enough. int age; cout << "Please enter your age: "; cin >> age; if (age < 0) cout << "Wow, you are really young!"; cout << "Your age is negative."; cout << endl; cout << "Your age is " << age << endl;

19 If: selective execution §What's wrong with this? if (x > y); x = y; cout << 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 < 0) { cout << "Come on, get real" << endl; } else if (num_credits < 12) { cout << "Part-time student" << endl; } else if (num_credits < 18) { cout << "Full-time student" << endl; } else { cout << "Glutton for punishment" << endl; }

23 Another way of writing this? if(num_credits < 0) { cout << "Come on, get real" << endl; } else if (num_credits < 12) { cout << "Part-time student" << endl; } else if (num_credits < 18) { cout << "Full-time student" << endl; } else { cout << "Glutton for punishment" << endl; }

24 If: selective execution int main () { int age; cout << "Please enter your age: "; cin >> age; if(age <= 0) { cout 0\n"; return 0; } cout << "Your age is " << age << endl; return 0; }

25 While loop int main () { int age; cout << "Please enter your age: "; cin >> age; while(age <= 0) { cout 0\n"; cout << "Please enter your age again: "; cin >> age; } cout << "Your age is " << age << endl; return 0; }

26 Nested if statements  The inside the braces can contain any valid C++ statements, including if statements! // … some other code here char answer; if (withdrawal > balance) { cout << "Insufficient funds." << endl; cout << "Do you want to see your balance? "; cin >> answer; if (answer == 'y') cout<< "Your balance is "<<balance<< endl; } else { cout << "Here is your money." << endl; } cout << "Good bye." << endl;

27 Nested if statements if (x>y){ if (x>z) statement1; if (x>p) statement2; else statement3; } else statement4; //bad style- no indentation (code with proper indentation on next slide)

28 Nested if statements if (x>y){ if (x>z) statement1; if (x>p) 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 x=10, y=2, z=12, p=13; if (x>y){ if (x>z) cout<<1<<endl;//statement1; if (x>p) cout<<2<<endl;//statement2; else cout<<3<<endl;//statement3; else cout<<4<<endl;//statement4; } else cout<<5<<endl;//statement5; return 0;}

30 Nested if statements // what is wrong here? int main(){ int x=10, y=2, z=12, p=13; if (x>y){ if (x>z) cout<<1<<endl;//statement1; if (x>p) cout<<2<<endl;//statement2; else cout<<3<<endl;//statement3; else //Error cout<<4<<endl;//statement4; } else cout<<5<<endl;//statement5; return 0;}

31 Nested if statements // what is output here? int main(){ int x=10, y=2, z=12, p=13; if (x>y){ if (x>z){ cout<<1<<endl;//statement1; if (x>p) cout<<2<<endl;//statement2; else cout<<3<<endl;//statement3; } else cout<<4<<endl;//statement4; } else cout<<5<<endl;//statement5; return 0; }

32 §Output is 4


Download ppt "Friday, December 15, 2006 “When you come to a fork in the road, take it.” - Yogi Berra."

Similar presentations


Ads by Google