Controlling execution iteration a lecture for the c++ Course


Download 0.75 Mb.
bet2/4
Sana20.12.2022
Hajmi0.75 Mb.
#1034535
1   2   3   4
Bog'liq
iteration7

}

A summing program

  • We wish to write a program that will calculate the sum of the integers between 1 and 20.
  • // A Summing Program using a while/do Loop

    #include

    using namespace std;

    int main(){

    int sum, count ;

    sum = 0;

    count = 1;

    while (count <=20){

    sum = sum + count;

    count = count + 1;

    }

    cout << "The sum of the integers from 1 through 20 is ";

    cout << sum << endl;

    return 0;

    }


The variables sum and count are accumulators.

A summing program

  • This program produces the same output. Which is better?
  • // A Summing Program using a for loop

    #include

    using namespace std;

    int main(){

    int sum, count ;

    sum = 0;

    for (count=1; count<=20; count++)

    sum = sum + count;

    cout << "The sum of the integers from 1 through 20 is ";

    cout << sum << endl;

    return 0;

    }

Just an average program

  • We have already seen a program that calculates the average of 3 particular numbers. That’s not very useful because it is not general enough.
  • We can do better now that we know how to loop. Here are some runs to show how the output from this program should look.

Just an average program

  • This program uses a for loop:
  • #include

    int main(){

    int n, count;

    float x, sum, avg;

    sum = 0;

    cout << "How many numbers? ";

    cin >> n;

    for (count=1; count<=n; count++){

    cout << "? ";

    cin >> x;

    sum = sum + x;

    }

    avg = sum / n;

    cout << "The average is " << avg << endl;

    return 0

    }


Do we really need to ask the user to count? Counting is something computers do really well!

Using a sentinel value

#include

int main(){

int count;

float x, sum, avg;

const float EOD = -999; //end of data indicator

count = 0;

sum = 0;

cout << “First number?”; cin >> x;

while (x != EOD) {

sum += x;

count++;

cout << "? "; cin >> x;

}

avg = sum / count;

cout << "The average is " << avg << endl;

return 0;

}


Run this program yourself to see how it works.

Choosing a loop structure

  • When to use the while structure? When to use for?
  • How about do/while?

Branching – emergency use only

  • Discuss conditional vs. unconditional branching statements. The goto statement. Branching instructions should be used sparingly, only when absolutely necessary.
  • We will never use the unconditional goto statement, so we won’t even cover it here.
  • Conditional branching statements:
    • break
    • continue

break

  • The break statement is used to break out of a loop. Control is transferred to the first statement after the end of the loop.
  • Example. What is output?
  • // modified from the Deitel book

    // Using the break statement in a for structure

    #include

    using namespace std;

    int main(){

    int x;

    for (x=1; x<=10; x++) {

    if (x==5)

    break; //break loop only if x is 5

    cout << x << " ";

    } //end for

    cout << "\nBroke out of loop at x = " << x << endl;

    cout<< endl << endl << endl;

    return 0;

    }

Example using break

Continue

  • The continue statement skips the remainder of the body of the loop, and continues the loop. Transfers control to “loop again.”
  • // This program is modified from the Deitel book,


    Download 0.75 Mb.

    Do'stlaringiz bilan baham:
1   2   3   4




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling