Top Banner
Saturday, December 16, 2006 “The whole of the development and operation of analysis are now capable of being executed by machinery… As soon as an Analytical Engine exists, it will necessarily guide the future course of science.” - Charles Babbage (1792 - 1871)
36
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Nested Loop

Saturday, December 16, 2006

“The whole of the development and operation of analysis are now capable of being executed by

machinery… As soon as an Analytical Engine exists, it will necessarily guide the

future course of science.”

- Charles Babbage (1792 - 1871)

Page 2: Nested Loop

Office hours today

Page 3: Nested Loop

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

Page 4: Nested Loop

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;

Page 5: Nested Loop

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)

Page 6: Nested Loop

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

Page 7: Nested Loop

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

Page 8: Nested Loop

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

Page 9: Nested Loop

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;

}

Page 10: Nested Loop

Output is 4

Page 11: Nested Loop

SELF TEST // 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;

cout<<4<<endl;//statement4; }else

cout<<5<<endl;//statement5;

return 0;}

Page 12: Nested Loop

SELF TEST // 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;cout<<4<<endl;//statement4; /*this statement is not in the above else block, it will always be executed whenever we enter if(x>y) block. Try this code for x=10, y=2, z=3, p=13 and also for x=10, y=2, z=13, p=3 */

}else

cout<<5<<endl;//statement5;return 0;}

Page 13: Nested Loop

char alarm;int fuel_quantity, temperature, pressure;cout<<"Enter alarm fuel_quantity temperature pressure\n";cin>> alarm >> fuel_quantity >> temperature>> pressure;if (alarm=='y'){

if(fuel_quantity <10){cout<<"Add more fuel\n";

}else{

if (temperature >90){if (pressure >100)

cout<<"RED ALERT! Shut down motor\n";else

cout<<"Turn on pressure valve\n";}else {

cout<<"Check temperature again after 10 minutes\n";}

}

}else {

cout<<"No problem with motor\n";}

Page 14: Nested Loop

Loops of various sorts are used to repeat a set of statements some number of times.

Page 15: Nested Loop

Print numbers from 0 to 1000

Page 16: Nested Loop

int count_down=3;

while (count_down > 0)

{ cout << "Hello "; count_down -= 1;

}

Page 17: Nested Loop

int count_down=3;

while (count_down > 0)

{ cout << "Hello "; //count_down -= 1;

}

What happens now?

Page 18: Nested Loop

int x = 10;

while ( x > 0)

{ cout << x << endl; x = x – 3;

}

Page 19: Nested Loop

int x = 10;

while (x > 0)

{ cout << x << endl; x = x – 3;

}Output using the comparison x < 0 instead of x > 0?

Page 20: Nested Loop

What happens here?

int x = 1;

while (x != 12)

{ cout << x << endl;

x = x + 2;

}

Page 21: Nested Loop

Print the odd numbers less than 12

int x = 1;while (x != 12){ cout << x << endl;

x = x + 2;}How to fix it?

Page 22: Nested Loop

While loopsWhat's wrong with this?

int x = 10; while ( x > 0 ); { cout << x << endl; x--; }

Page 23: Nested Loop

While-loop and for-loop

Page 24: Nested Loop

int x = 1;

while (x < 12)

{

cout<<x<<endl;

x = x + 2;

}

Page 25: Nested Loop

The for loopfor (initialization; expression; increment){

//statements here}

Example: flowchart

Page 26: Nested Loop

int x = 1;

while (x < 12)

{

cout<<x<<endl;

x = x + 2;

}

Page 27: Nested Loop

int x = 1;

while (x < 12)

{

cout<<x<<endl;

x = x + 2;

}

for (x=1; x<12; x=x+2)

{

cout<<x<<endl;

}

Page 28: Nested Loop

int i ;for( i=0; i<10; ++i ){

cout << i << " ";}

Page 29: Nested Loop

int i;for( i=0; i<10; ++i ){

cout << i << " ";}

int i;for (i=0; i<10; i++){

cout <<i << " ";}

Page 30: Nested Loop

int x, n=100;for (x=0; x<n; x++){

x=x+1;}cout<<"x after end of loop is "<<x<<endl;

Page 31: Nested Loop

int x, n=100;for (x=0; x<n; x++){

x=x+1;}cout<<"x after end of loop is "<<x<<endl;

x is the smallest even number >= n

Page 32: Nested Loop

SELF TEST

int x, n=100;

for (x=0; x<n; x++){

cout<<x<<endl;

x=x+1;

}cout<<"x after end of loop is "<<x<<endl;

Page 33: Nested Loop

SELF TEST

int x, n=100;

for (x=0; x<n; x++){

x=x+1;

cout<<x<<endl;

}cout<<"x after end of loop is "<<x<<endl;

Page 34: Nested Loop

The For Loop

int i ;for( i=23; i>=30; ++i ){

cout << i << " ";}

for (i=13; i>=10; --i){

cout <<i <<" ";}

Page 35: Nested Loop

The For Loop

What is wrong here?int i, j, k ;for( i=0; i<10; i-- ){

cout<<i;}

for (i=0; i<10; ){

j=i+30;k=j+30;

}

Page 36: Nested Loop

Fibonacci numbers

F1=1

F2=2

Fn=Fn-1 + Fn-2

1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 …