Presentation is loading. Please wait.

Presentation is loading. Please wait.

While Loop Design ENGI 1020 Fall 2018.

Similar presentations


Presentation on theme: "While Loop Design ENGI 1020 Fall 2018."— Presentation transcript:

1 While Loop Design ENGI 1020 Fall 2018

2 Example to trace the loop
void foo(){ int i=7; while (i>0){ if( i%2 != 0 ) cout<<i<<endl; i--; } cout<<"goodbye!"; What is printed on the screen? Be very detailed! Hint: 15%2=1 (the modulo operator returns the remainder)

3 Solution void foo(){ int i=5; while (i>0){ if( i%2 != 0 ) cout<<i<<endl; i--; } cout<<"goodbye!"; i

4 While Loop Design The general case: The special cases:
What should be done in the body? The special cases: Under what condition should the iteration stop? How should the loop control condition be initialized? How should the loop control condition be updated? How should other variables be initialized? How should other variables be updated? What is the state when the loop exits? long factorial(int num){ long fact = 1; while (num > 1) { fact *= num; num--; } return fact; } General case

5 Design loops from the inside out (from the general to the specific).
Note the implication here is that first design the loop, then code the loop

6 Simple design example We want to print the numbers from 5 to 9 on the screen Design process: We start with number 5, then print number then we increment the previous printed number, then print number then increment previous printed number, then print number then increment previous printed number, and so on until we print 9 - Obviously this is a repeated task I got it – Let’s have a variable, make a loop and inside the loop: print number then we increment the previous printed number Integer i=5 While ( i<=9 ) { print the variable increment the variable } Define a variable While ( condition ) { print the variable increment the variable }

7 void printNumbers(){ int i=5; while (i<=9){ cout<<i; i++; }

8 Exercise Write a function that adds all the numbers from 5 to 9
Modify this code void printNumbers(){ int i=5; while (i<=9){ cout<<i; i++; } int addNumbers( ){ int sum=0; int i=5; while (i<=9){ sum+=i; i++; } return sum; To write this function int addNumbers( ){

9 Summary int addNumbers( ){ int sum=0; int i=5; while (i<=9){ sum+=i; i++; } return sum; Identify the principles we discussed on this function you just wrote: The general case: What should be done in the body? The special cases: Under what condition should the iteration stop? How should the loop control condition be initialized? How should the loop control condition be updated? How should other variables be initialized? How should other variables be updated?

10 Homework Repeat the same previous example but add only the odd numbers between 5 and 9 (sum = 5+7+9) Hint: how i should be updated?

11 Complicated Example Write a program in C++ to find the sum of the first 10 terms of the following series: sum = (1/1!) - (2/2!) + (4/4!) - (8/8!) …………. (n/n!) where number of terms=10 while loop condition i<10 define sum, term, i=0 while loop( ) { evaluate the current term Add the current term to sum increment i } Print the Sum 1, 2, 4, 8, … is equivalent to 2(i) (that’s why we start at i=0) Also notice the sign is changing - The sign can be done using (-1) (i) The general form of a term is then: (−2) 𝑖 (2) 𝑖 ! ½ of the lecture (25 minutes have passed)

12 Example 2 – Cont’d double term=0.0; double sum=0.0;
int i=0; We need a factorial function 𝑥! long factorial(int x) { long result=1; while (x>1){ result*= x; x-- ; } return result; define sum, term, i While ( i<10 ) { current term = (-2)(i) / factorial( (2)(i) ) Add current term to sum increment i } Print the Sum pow(-2,i)

13 Putting All Together long factorial (int x); main function {
double sum=0.0; double term=0.0; int i=0; while (i<10 ) current term = (-2)(i) / factorial( (2)(i) ) Add current term to sum increment i } Print the Sum long factorial(int x) { long result=1; while (x>1){ result*= x; x-- ; return result;

14 Writing the code #include <iostream> #include <math.h>
using namespace std; long factorial(int x); int main() { double sum=0.0; double term=0.0; int i=0; while (i<10) { term=pow(-2,i) / factorial(pow(2,i)); sum += term; i++; } cout << "The sum of 10 terms is: " << sum ; return 0; long factorial(int x) { long result=1; while (x>1){ result*= x; x-- ; return result; long factorial (int x); main function { double sum=0.0; double term=0.0; int i=0; while (i<10 ) current term = (-2)(i) / factorial( (2)(i) ) Add current term to sum increment i } Print the Sum long factorial(int x) { long result=1; while (x>1){ result*= x; x-- ; return result;


Download ppt "While Loop Design ENGI 1020 Fall 2018."

Similar presentations


Ads by Google