Looping I while statement CSCE 106 Outline Loopingrepetition

  • Slides: 12
Download presentation
Looping I (while statement) CSCE 106

Looping I (while statement) CSCE 106

Outline § Looping/repetition construct § while statement (section 5. 1) CSCE 106 2

Outline § Looping/repetition construct § while statement (section 5. 1) CSCE 106 2

Repetition § Repetition or looping provides for the repeated execution of part of the

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

Exercise 1 § Problem Analyse, and design, using a flowchart, an algorithm that outputs

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

Exercise (cont’d) START § Design #include <iostream> using namespace std; void main () {

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

while Statement § Syntax: while (loop repetition condition) statement; § E. g. Intialising loop

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

Listing 5. 1 CSCE 106 while loop segment example 7

Listing 5. 1 CSCE 106 while loop segment example 7

Exercise 2 § Problem Analyse, design, and implement an algorithm that calculates and outputs

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 u Input n: upper limit u Output sum: sum of series u Intermediate variables i: the current iteration number CSCE 106 8

#include <iostream> using namespace std; § Design START INPUT n void main () {

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

Exercise 3 Analyse, design, and implement an algorithm that calculates the factorial of a

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 10

#include <iostream> using namespace std; void main() { int i, n, factorial = 1;

#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 11

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

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