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

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

  • View
    213

  • Download
    0

Embed Size (px)

Citation preview

Friday, December 15, 2006

“When you come to a fork in the road, take it.”

- Yogi Berra.

Class on Saturday (Tomorrow)

How do we define a block in C++?

A block in C++ is created by placing a sequence of statements between curly braces.

Good programming practices

Meaningful Variable namesCode indentationComments

There will be marks for style in homeworks/assignments.

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

Relational Operators

>

>=

<

<=

= =

!=

Logical Operators

&&

| |

!

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;}

Boolean expressions

Logical AND is represented by &&

Example.

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!

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;

Boolean expressions

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

Boolean expressions

Given x=3 and y=4. What's the value of cout<< !(( (x < y) && (x > y-2) ) && ( x*2 < y ) );

prints 1

Boolean expressions

What's wrong with these?

(y > x) & (z > x)

(z !>= y)

(a < x < b)

!(x = 3)

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++?

If: selective executionExample: Bank withdrawal checker int balance;//… some other code hereint withdrawal;cout << "Please enter the withdrawal amount: ";cin >> withdrawal;

if(withdrawal <= balance) { cout << "Here is your money" << endl; }else { cout << "Sorry, insufficient balance" <<endl;}

If: selective executionWhat 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;

If: selective executionPitfall: 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;

If: selective executionWhat's wrong with this? if (x > y); x = y; cout << x;

If: selective execution

Pitfall:

There is no semi-colon after the if-condition!

Stringing if-then-elses together

If-then-else statements can be strung together.

Exactly one of the alternatives will be executed.

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; }

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; }

If: selective executionint main () {

int age;

cout << "Please enter your age: ";cin >> age;if(age <= 0) { cout << "Age must be > 0\n"; return 0;}cout << "Your age is " << age << endl;return 0;

}

While loopint main () {

int age;

cout << "Please enter your age: ";

cin >> age;

while(age <= 0) {

cout << "Age must be > 0\n";

cout << "Please enter your age again: ";

cin >> age;

}

cout << "Your age is " << age << endl;return 0;

}

Nested if statements The <statements> 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;

Nested if statements if (x>y){if (x>z)statement1;if (x>p)statement2;elsestatement3;}elsestatement4;//bad style- no indentation (code with proper indentation on next

slide)

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*/

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;}

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;}

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;

}

Output is 4