1 / 33

Fundamental Programming

Fundamental Programming. More on Repetition. Repetition statements. we have been using one C++ repetition statement – while repetition statements allow us to perform a task more than once – the number of times depends on the input data

aduane
Télécharger la présentation

Fundamental Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Fundamental Programming More on Repetition

  2. Repetition statements we have been using one C++ repetition statement – while repetition statements allow us to perform a task more than once – the number of times depends on the input data in this class, we introduce another repetition statement – the do-while statement

  3. while – a Review • while ( <condition> ) • { • < while statements > • } • NbrTimesTold = 0; • while (NbrTimesTold < NbrTimesToTell) • { cout << “No new taxes!“ << endl; NbrTimesTold = NbrTimesTold + 1; }

  4. while – a Review commonlooping errors are: • loops that fail to stop (continue forever) • loops that stop one repetition too early • loops that perform one repetition too many • loops that fail to start normal while-loop structure is…

  5. while – a Review • < initialise variables in while-condition > • while ( <condition> ) • { • < while statements > • < updatevariables in while-condition > • } • NbrTimesTold = 0; • while (NbrTimesTold < NbrTimesToTell) • { cout << “No new taxes!“ << endl; NbrTimesTold = NbrTimesTold + 1; }

  6. Activity what will the following code output? Number = 5; while (Number > 0) { Sum = Sum + Number ; Number = Number - 1 ; } cout <<“The sum is “<<Sum <<endl;

  7. Activity Break

  8. Activity Feedback output depends on initial value of Sum if Sum is zero at start: “The sum is 15” must always initialise a sum to zero! Number = 5; while (Number > 0) { Sum = Sum + Number ; Number = Number - 1 ; } cout <<“The sum is “<<Sum <<endl;

  9. Activity what will the following code output? Sum = 0 ; while (Number > 0) { Sum = Sum + Number ; Number = Number - 1; } cout <<“The sum is “<<Sum <<endl;

  10. Activity Break

  11. Activity Feedback output depends on initial value of Number must initialise variables in while condition! Sum = 0 ; while (Number > 0) { Sum = Sum + Number ; Number = Number - 1; } cout <<“The sum is “<<Sum <<endl;

  12. More C++ operators C++ has operators to increment or decrement loop control variables Number = Number - 1; NbrTimesTold = NbrTimesTold + 1; can be replaced by: Number-- ; NbrTimesTold++ ;

  13. Activity What will the following code output? Sum = 0 ; Number = 5; while (Number > 0) { Sum = Sum + Number ; Number++; } cout <<“The sum is “<<Sum <<endl;

  14. Activity Break

  15. Activity Feedback loop will never end – an infinite loop this is a logic error! Always check loop conditions carefully. Sum = 0 ; Number = 5; while (Number > 0) { Sum = Sum + Number ; Number++; } cout <<“The sum is “<<Sum <<endl;

  16. The whileStatement while is the only repetition statement you will every need however, most programming languages include at least two other repetition statements we introduce one of these statements in this class, the other one will be covered later

  17. while vs do-while the while statement tests a condition atthe start of the loop the do-while statement tests a condition atthe endof the loop

  18. while vs do-while the while statement tests a condition atthe startof the loop cout << “Select a direction – N,S,E or W ==> “; cin >> Char; while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) { cout << “Invalid direction!” << endl; cout << “Select a direction – N,S,E or W ==> “; cin >> Char; }

  19. do-while loops if a task must be performed at least once, we can perform the test at the end of the loop using do-while do { cout << “Select a direction – N,S,E or W ==> “; cin >> Char; if ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) cout << “Invalid direction!” << endl; }while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’));

  20. Activity what are advantages and disadvantages of this design (compared to using while)? do { cout << “Select a direction – N,S,E or W ==> “; cin >> Char; if ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) cout << “Invalid direction!” << endl; }while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)); can you suggest an improvement?

  21. Activity Break

  22. Activity Feedback one advantage of do-while is that there is only one copy of prompt and input lines cout << “Select a direction – N,S,E or W ==> “; cin >> Char; while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) { cout << “Invalid direction!” << endl; cout << “Select a direction – N,S,E or W ==> “; cin >> Char; }

  23. Activity Feedback one advantage of do-while is that there is only one copy of prompt and input lines do { cout << “Select a direction – N,S,E or W ==> “; cin >> Char; if ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) cout << “Invalid direction!” << endl; }while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’));

  24. Activity Feedback one disadvantage of do-while is that the loop condition appears twice do { cout << “Select a direction – N,S,E or W ==> “; cin >> Char; if ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) cout << “Invalid direction!” << endl; }while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’));

  25. Activity Feedback one disadvantage of do-while is that the loop condition appears twice cout << “Select a direction – N,S,E or W ==> “; cin >> Char; while ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) { cout << “Invalid direction!” << endl; cout << “Select a direction – N,S,E or W ==> “; cin >> Char; }

  26. Activity Feedback repetition of complex loop conditions can be avoided using a Boolean variable… WaitingForDirection = true; do { cout << “Select a direction – N,S,E or W ==> “; cin >> Char; if ((Char != ‘N’) && (Char != ‘S’) && (Char != ‘E’) && (Char != ‘W’)) cout << “Invalid direction!” << endl; else WaitingForDirection = false; }while ( WaitingForDirection );

  27. Activity • is the following logic OK? • Sum = 0 ; • do • { • cout << “Enter a number (-9 to quit) ==> "; • cin >> Number; • Sum = Sum + Number; • } while (Number != -9); • cout << “Sum = “ << Sum; • if not, fix it.

  28. Activity Break

  29. Activity Feedback • the problem with the logic is that it will include –9 in the sum – it should be: • Sum = 0 ; • do • { • cout << “Enter a number (-9 to quit) ==> "; • cin >> Number; • if (Number != -9) • Sum = Sum + Number; • } while (Number != -9); • cout << “Sum = “ << Sum; note: you will often see loop conditions repeated in do-while statements

  30. Activity Code the following as a while loop: Number = 5 ; Sum = 0 ; do { Sum += Number ; Number-- ; }while (Number > 0) ;

  31. Activity Break

  32. Activity Feedback note: loop condition remains the same... loop condition controls value of variable during the finaltrip through the loop – so, it should be the same Number = 5 ; Sum = 0 ; while (Number > 0) { Sum += Number ; Number-- ; }

  33. while vs do-while difference between while and do-while is that while allows for the possibility that the task in the loop is performed 0 times with do-while, the task in the loop is performed at least once hence, while is more flexible - that’s why we started with it

More Related