1 / 12

Looping I ( while statement)

Looping I ( while statement). Outline. Looping/repetition construct while statement (section 5.1). Repetition. Repetition or looping provides for the repeated execution of part of the algorithm. Exercise 1. Problem

Télécharger la présentation

Looping I ( while statement)

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. Looping I(while statement)

  2. Outline • Looping/repetition construct • while statement (section 5.1) CSCE 106

  3. Repetition • Repetition or looping provides for the repeated execution of part of the algorithm. CSCE 106

  4. Exercise 1 • Problem Analyse, and design, using a flowchart, an algorithm that outputs the word “Hello” 10 times each on a separate line. • Analysis • Input None • Output “Hello” 10 times • Intermediate variables i: counter used to indicate the current iteration number. CSCE 106

  5. #include <iostream> using namespace std; void main () { int i; i = 1; while (i <= 10) { cout << “Hello”<<endl; i = i +1; } } Exercise (cont’d) START • Design i = 1 False STOP i <= 10 ? True OUTPUT “Hello”, ‘\n’ i = i + 1 CSCE 106

  6. while Statement • Syntax: while (loop repetition condition) statement; • E.g. i = 1; while(i <= 10) { cout << “*”; i = i + 1; } Intialising loop control variable Testing loop control variable Updating loop control variable CSCE 106

  7. Listing 5.1while loop segment example CSCE 106

  8. Exercise 2 • Problem Analyse, design, and implement an algorithm that calculates and outputs the following sum: sum = 1 + 2 + 3 + ……. + n up to any number (n) input by the user. • Analysis • Input n: upper limit • Output sum: sum of series • Intermediate variables i: the current iteration number CSCE 106

  9. #include <iostream> using namespace std; void main () { int n, i, sum; cin >> n; i = 1; sum = 0; while (i <= n) { sum = sum + i; i = i +1; } cout << “The sum is “<<sum; } START • Design INPUT n i = 1 sum = 0 OUTPUT sum False i <= n ? True STOP sum = sum + i i = i + 1 CSCE 106

  10. Exercise 3 Analyse, design, and implement an algorithm that calculates the factorial of a given number (n), input by the user. Factorial is calculated using the following equation: factorial = 1 * 2 * 3 * … * n CSCE 106

  11. #include <iostream> • using namespace std; • void main() • { • int i, n, factorial = 1; • cout << “Please enter a whole number:“; • cin >> n; • i = 2; • while (i <= n) • { • factorial = factorial * i; • i = i + 1; • } • cout << “The factorial is:“ << factorial << endl; • } CSCE 106

  12. Next lecture we will continue Looping control construct in C++ CSCE 106

More Related