Nested Loop

1,130 views
848 views

Published on

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,130
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
17
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Nested Loop

  1. 1. 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)
  2. 2. Office hours today
  3. 3. 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;}
  4. 4. Nested if statements• The <statements> inside the braces can contain any valid C++ statements, including if statements!// … some other code herechar 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;
  5. 5. Nested if statementsif (x>y){if (x>z)statement1;if (x>p)statement2;elsestatement3;}elsestatement4;//bad style- no indentation (code with proper indentation on next slide)
  6. 6. Nested if statementsif (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*/
  7. 7. 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;}
  8. 8. 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;}
  9. 9. 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;}
  10. 10. Output is 4
  11. 11. 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;}
  12. 12. 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;}
  13. 13. char alarm;int fuel_quantity, temperature, pressure;cout<<"Enter alarm fuel_quantity temperature pressuren";cin>> alarm >> fuel_quantity >> temperature>> pressure;if (alarm==y){ if(fuel_quantity <10){ cout<<"Add more fueln"; } else{ if (temperature >90){ if (pressure >100) cout<<"RED ALERT! Shut down motorn"; else cout<<"Turn on pressure valven"; } else { cout<<"Check temperature again after 10 minutesn"; } }}else { cout<<"No problem with motorn";}
  14. 14. Loops of various sorts are used torepeat a set of statements somenumber of times.
  15. 15. Print numbers from 0 to 1000
  16. 16. int count_down=3;while (count_down > 0) { cout << "Hello "; count_down -= 1; }
  17. 17. int count_down=3;while (count_down > 0) { cout << "Hello "; //count_down -= 1; }What happens now?
  18. 18. int x = 10;while ( x > 0){ cout << x << endl; x = x – 3; }
  19. 19. int x = 10;while (x > 0){ cout << x << endl; x = x – 3;}Output using the comparison x < 0 instead of x > 0?
  20. 20. What happens here?int x = 1;while (x != 12){ cout << x << endl; x = x + 2;}
  21. 21. Print the odd numbers less than 12int x = 1;while (x != 12){ cout << x << endl; x = x + 2;}How to fix it?
  22. 22. While loopsWhats wrong with this? int x = 10; while ( x > 0 ); { cout << x << endl; x--; }
  23. 23. While-loop and for-loop
  24. 24. int x = 1;while (x < 12){ cout<<x<<endl; x = x + 2;}
  25. 25. The for loopfor (initialization; expression; increment){ //statements here}Example: flowchart
  26. 26. int x = 1;while (x < 12){ cout<<x<<endl; x = x + 2;}
  27. 27. int x = 1; for (x=1; x<12; x=x+2)while (x < 12) {{ cout<<x<<endl; cout<<x<<endl; } x = x + 2;}
  28. 28. int i ;for( i=0; i<10; ++i ){ cout << i << " ";}
  29. 29. int i;for( i=0; i<10; ++i ){ cout << i << " ";}int i;for (i=0; i<10; i++){ cout <<i << " ";}
  30. 30. int x, n=100;for (x=0; x<n; x++){ x=x+1;}cout<<"x after end of loop is "<<x<<endl;
  31. 31. 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
  32. 32. SELF TESTint x, n=100;for (x=0; x<n; x++){ cout<<x<<endl; x=x+1;}cout<<"x after end of loop is "<<x<<endl;
  33. 33. SELF TESTint x, n=100;for (x=0; x<n; x++){ x=x+1; cout<<x<<endl;}cout<<"x after end of loop is "<<x<<endl;
  34. 34. The For Loopint i ;for( i=23; i>=30; ++i ){ cout << i << " ";}for (i=13; i>=10; --i){ cout <<i <<" ";}
  35. 35. The For LoopWhat 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;}
  36. 36. Fibonacci numbersF1=1F2=2Fn=Fn-1 + Fn-21 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 …

×