Top Banner

of 20

Lecture 12 - Control Structures Rep Pt 1.pdf

Jun 04, 2018

Download

Documents

sunnyopg
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/14/2019 Lecture 12 - Control Structures Rep Pt 1.pdf

    1/20

    2/13/201

    1992-2012 by Pearson Education, Inc. & John Wiley & SonsSome portions are adopted from C++ f or Everyone by Horstmann

    ENGR 1200U Introduction to Programming

    Lecture 12

    Control Structures: Repetition (Chapter 4)

    Dr. Eyhab Al-Masri

    ENGR 1200UWinter 2013 - UOIT

    In Chapter 3, we developed algorithms that usedselection control structures Provide alternative paths during program execution

    In this chapter, we develop algorithms and writeC++ programs that use repetition structures

    Whatisarepetitionstructure?Repetition structure allows us to repeat (or loop

    through) a set of steps as long as the condition is true

  • 8/14/2019 Lecture 12 - Control Structures Rep Pt 1.pdf

    2/20

    2/13/201

    ENGR 1200UWinter 2013 - UOIT

    Example We might want to compute the average of midterm

    exam grades.

    Howtoentermidtermgrades?Use a repetition control structure to request the user

    to enter the midterm grades until a condition becomesfalse (i.e. no more students). Through every loop (i.e.

    every new grade entered), calculate the sum anddivide by number of students (i.e. iterations)

    ENGR 1200UWinter 2013 - UOIT

    Algorithms usingrepetition structurescan be describedwith pseudocode orflowcharts

    is count

  • 8/14/2019 Lecture 12 - Control Structures Rep Pt 1.pdf

    3/20

    2/13/201

    ENGR 1200UWinter 2013 - UOIT

    Loops are used to implement repetitionstructures

    C++ contains three different loop structures

    C++ also supports t two additional statementswith loops to modify their performance:

    while for do/while

    break continue

    ENGR 1200UWinter 2013 - UOIT

    Syntax:while (boolean_expression)statement; while (boolean_expression) {statement1;statement_n;}Exampleswhile (!isspace(ch))

    cin.get(ch); // statement repeated until a// space character is read

    while (x>y) { //statement block executed//while x>0

    ++count;--x;

    }

  • 8/14/2019 Lecture 12 - Control Structures Rep Pt 1.pdf

    4/20

    2/13/201

    ENGR 1200UWinter 2013 - UOIT

    An investment Problem: Savings account start balance: $10,000 Task: How many years until we have at least $20,000?1. Set a year value to 0, set a balance value to $10,000

    2. Repeat the following steps while balance is less than$20,000

    Increment the year value by 1

    Compute the interest by multiplying balance value by 0.05(5% interest)

    Add interest to balance

    3. Report final year value as the answer

    Yes!

    Algorithm:

    C++ for Everyone by Cay Horstman

    ENGR 1200UWinter 2013 - UOIT

    The statements to be controlled are:

    Incrementing the year variable

    Computing the interest variable using interest 5%

    Updating the balance variable by adding the interest

    year++;

    double interest = balance * (5/100);balance = balance + interest;

    Yes!

    code snippet:

    C++ for Everyone by Cay Horstman

  • 8/14/2019 Lecture 12 - Control Structures Rep Pt 1.pdf

    5/20

    2/13/201

    ENGR 1200UWinter 2013 - UOIT

    The condition which indicates whento stop executing the statements isthis test

    (balance < TARGET)

    while (balance < TARGET)

    {

    year++;

    double interest = balance * RATE / 100;

    balance = balance + interest;}

    Yes!

    complete while statement

    C++ for Everyone by Cay Horstman

    ENGR 1200UWinter 2013 - UOIT

    Notice that interest is defined insidethe loop and that year and balanceare defined outside the loop

    while (balance < TARGET)

    {

    year++;

    double interest = balance * RATE / 100;

    balance = balance + interest;

    }

    Yes!

    complete while statement

    A new interest variable to be created in each iteration.year and balance are used for all iterations.

    C++ for Everyone by Cay Horstman

  • 8/14/2019 Lecture 12 - Control Structures Rep Pt 1.pdf

    6/20

    2/13/201

    ENGR 1200UWinter 2013 - UOIT C++ for Everyone by Cay Horstman

    ENGR 1200UWinter 2013 - UOIT

    int main(){

    const double RATE=5;

    const double INITIAL_BALANCE=10000;

    const double TARGET=2*INITIAL_BALANCE;

    double balance=INITIAL_BALANCE;

    int year=0;

    while (balance

  • 8/14/2019 Lecture 12 - Control Structures Rep Pt 1.pdf

    7/20

    2/13/201

    ENGR 1200UWinter 2013 - UOIT C++ for Everyone by Cay Horstman

    10000

    0

    ENGR 1200UWinter 2013 - UOIT C++ for Everyone by Cay Horstman

    10000

    0

    10000

    0

    ?

  • 8/14/2019 Lecture 12 - Control Structures Rep Pt 1.pdf

    8/20

    2/13/201

    ENGR 1200UWinter 2013 - UOIT C++ for Everyone by Cay Horstman

    10000

    1

    10000

    1

    ?

    ENGR 1200UWinter 2013 - UOIT C++ for Everyone by Cay Horstman

    10000

    1

    10000

    1

    500

  • 8/14/2019 Lecture 12 - Control Structures Rep Pt 1.pdf

    9/20

    2/13/201

    ENGR 1200UWinter 2013 - UOIT C++ for Everyone by Cay Horstman

    10500

    1

    10500

    1

    500

    ENGR 1200UWinter 2013 - UOIT C++ for Everyone by Cay Horstman

    10500

    1

    10500

    1

    500

  • 8/14/2019 Lecture 12 - Control Structures Rep Pt 1.pdf

    10/20

    2/13/201

    1

    ENGR 1200UWinter 2013 - UOIT C++ for Everyone by Cay Horstman

    10500

    1

    ENGR 1200UWinter 2013 - UOIT C++ for Everyone by Cay Horstman

    10500

    1

    10500

    1

    ?

  • 8/14/2019 Lecture 12 - Control Structures Rep Pt 1.pdf

    11/20

    2/13/201

    1

    ENGR 1200UWinter 2013 - UOIT C++ for Everyone by Cay Horstman

    10500

    2

    10500

    2

    ?

    ENGR 1200UWinter 2013 - UOIT C++ for Everyone by Cay Horstman

    10500

    2

    10500

    2

    525

  • 8/14/2019 Lecture 12 - Control Structures Rep Pt 1.pdf

    12/20

    2/13/201

    1

    ENGR 1200UWinter 2013 - UOIT C++ for Everyone by Cay Horstman

    11025

    1

    11025

    525

    2

    ENGR 1200UWinter 2013 - UOIT C++ for Everyone by Cay Horstman

    10500

    1

    11025

    525

    2

  • 8/14/2019 Lecture 12 - Control Structures Rep Pt 1.pdf

    13/20

    2/13/201

    1

    ENGR 1200UWinter 2013 - UOIT C++ for Everyone by Cay Horstman

    11025

    2

    ENGR 1200UWinter 2013 - UOIT C++ for Everyone by Cay Horstman

    this process goes onfor 15 iterations

    before entering while's

    b od y at t he en d o f w hi le' s bo dy

    balance year interest balance year

    10000.00 0 500.00 10500.00 1

    10500.00 1 525.00 11025.00 2

    11025.00 2 551.25 11576.25 3

    11576.25 3 578.81 12155.06 4

    12155.06 4 607.75 12762.82 5

    12762.82 5 638.14 13400.96 6

    13400.96 6 670.05 14071.00 7

    14071.00 7 703.55 14774.55 8

    14774.55 8 738.73 15513.28 9

    15513.28 9 775.66 16288.95 10

    16288.95 10 814.45 17103.39 1 1

    17103.39 11 855.17 17958.56 1 2

    17958.56 12 897.93 18856.49 1 3

    18856.49 13 942.82 19799.32 1 4

    19799.32 14 989.97 20789.28 1 5

    20789.28 15 w hi le s tat em en t i s o ver

  • 8/14/2019 Lecture 12 - Control Structures Rep Pt 1.pdf

    14/20

    2/13/201

    1

    ENGR 1200UWinter 2013 - UOIT C++ for Everyone by Cay Horstman

    this process goes onfor 15 iterations

    before entering while's

    b od y at t he en d o f w hi le' s bo dy

    balance year interest balance year

    10000.00 0 500.00 10500.00 1

    10500.00 1 525.00 11025.00 2

    11025.00 2 551.25 11576.25 3

    11576.25 3 578.81 12155.06 4

    12155.06 4 607.75 12762.82 5

    12762.82 5 638.14 13400.96 6

    13400.96 6 670.05 14071.00 7

    14071.00 7 703.55 14774.55 8

    14774.55 8 738.73 15513.28 9

    15513.28 9 775.66 16288.95 10

    16288.95 10 814.45 17103.39 1 1

    17103.39 11 855.17 17958.56 1 2

    17958.56 12 897.93 18856.49 1 3

    18856.49 13 942.82 19799.32 1 4

    19799.32 14 989.97 20789.28 1 5

    20789.28 15 w hi le s tat em en t i s o ver

    ENGR 1200UWinter 2013 - UOIT C++ for Everyone by Cay Horstman

    this process goes onfor 15 iterations

    before entering while's

    b od y at t he en d o f w hi le' s bo dy

    balance year interest balance year

    10000.00 0 500.00 10500.00 1

    10500.00 1 525.00 11025.00 2

    11025.00 2 551.25 11576.25 3

    11576.25 3 578.81 12155.06 4

    12155.06 4 607.75 12762.82 5

    12762.82 5 638.14 13400.96 6

    13400.96 6 670.05 14071.00 7

    14071.00 7 703.55 14774.55 8

    14774.55 8 738.73 15513.28 9

    15513.28 9 775.66 16288.95 10

    16288.95 10 814.45 17103.39 1 1

    17103.39 11 855.17 17958.56 1 2

    17958.56 12 897.93 18856.49 1 3

    18856.49 13 942.82 19799.32 1 4

    19799.32 14 989.97 20789.28 1 5

    20789.28 15 w hi le s tat em en t i s o ver

  • 8/14/2019 Lecture 12 - Control Structures Rep Pt 1.pdf

    15/20

    2/13/201

    1

    ENGR 1200UWinter 2013 - UOIT C++ for Everyone by Cay Horstman

    this process goes onfor 15 iterations

    before entering while's

    b od y at t he en d o f w hi le' s bo dy

    balance year interest balance year

    10000.00 0 500.00 10500.00 1

    10500.00 1 525.00 11025.00 2

    11025.00 2 551.25 11576.25 3

    11576.25 3 578.81 12155.06 4

    12155.06 4 607.75 12762.82 5

    12762.82 5 638.14 13400.96 6

    13400.96 6 670.05 14071.00 7

    14071.00 7 703.55 14774.55 8

    14774.55 8 738.73 15513.28 9

    15513.28 9 775.66 16288.95 10

    16288.95 10 814.45 17103.39 1 1

    17103.39 11 855.17 17958.56 1 2

    17958.56 12 897.93 18856.49 1 3

    18856.49 13 942.82 19799.32 1 4

    19799.32 14 989.97 20789.28 1 5

    20789.28 15 w hi le s tat em en t i s o ver

    ENGR 1200UWinter 2013 - UOIT C++ for Everyone by Cay Horstman

    this process goes onfor 15 iterations

    before entering while's

    b od y at t he en d o f w hi le' s bo dy

    balance year interest balance year

    10000.00 0 500.00 10500.00 1

    10500.00 1 525.00 11025.00 2

    11025.00 2 551.25 11576.25 3

    11576.25 3 578.81 12155.06 4

    12155.06 4 607.75 12762.82 5

    12762.82 5 638.14 13400.96 6

    13400.96 6 670.05 14071.00 7

    14071.00 7 703.55 14774.55 8

    14774.55 8 738.73 15513.28 9

    15513.28 9 775.66 16288.95 10

    16288.95 10 814.45 17103.39 1 1

    17103.39 11 855.17 17958.56 1 2

    17958.56 12 897.93 18856.49 1 3

    18856.49 13 942.82 19799.32 1 4

    19799.32 14 989.97 20789.28 1 5

    20789.28 15 w hi le s tat em en t i s o ver

  • 8/14/2019 Lecture 12 - Control Structures Rep Pt 1.pdf

    16/20

    2/13/201

    1

    ENGR 1200UWinter 2013 - UOIT C++ for Everyone by Cay Horstman

    this process goes onfor 15 iterations

    until thebalance is

    finally(!) over $20,000 andthe test becomes false.

    before entering while's

    b od y at t he en d o f w hi le' s bo dy

    balance year interest balance year

    10000.00 0 500.00 10500.00 1

    10500.00 1 525.00 11025.00 2

    11025.00 2 551.25 11576.25 3

    11576.25 3 578.81 12155.06 4

    12155.06 4 607.75 12762.82 5

    12762.82 5 638.14 13400.96 6

    13400.96 6 670.05 14071.00 7

    14071.00 7 703.55 14774.55 8

    14774.55 8 738.73 15513.28 9

    15513.28 9 775.66 16288.95 10

    16288.95 10 814.45 17103.39 1 1

    17103.39 11 855.17 17958.56 1 2

    17958.56 12 897.93 18856.49 1 3

    18856.49 13 942.82 19799.32 1 4

    19799.32 14 989.97 20789.28 1 5

    20789.28 15 w hi le s tat em en t i s o ver

    ENGR 1200UWinter 2013 - UOIT C++ for Everyone by Cay Horstman

    15

  • 8/14/2019 Lecture 12 - Control Structures Rep Pt 1.pdf

    17/20

    2/13/201

    1

    ENGR 1200UWinter 2013 - UOIT C++ for Everyone by Cay Horstman

    ENGR 1200UWinter 2013 - UOIT C++ for Everyone by Cay Horstman

  • 8/14/2019 Lecture 12 - Control Structures Rep Pt 1.pdf

    18/20

    2/13/201

    1

    ENGR 1200UWinter 2013 - UOIT C++ for Everyone by Cay Horstman

    ENGR 1200UWinter 2013 - UOIT

    What is the output of thefollowing while loop?

    i =5;

    while (i >0)

    {

    cout

  • 8/14/2019 Lecture 12 - Control Structures Rep Pt 1.pdf

    19/20

    2/13/201

    1

    ENGR 1200UWinter 2013 - UOIT

    What is the output of thefollowing while loop?

    i =5;

    while (i >0)

    {

    cout

  • 8/14/2019 Lecture 12 - Control Structures Rep Pt 1.pdf

    20/20

    2/13/201

    ENGR 1200UWinter 2013 - UOIT

    What is the output of thefollowing while loop?

    i =5;

    while (i