Looping c الفصل الثانى

download Looping c الفصل الثانى

of 13

Transcript of Looping c الفصل الثانى

  • 8/10/2019 Looping c

    1/13

    1

    Starting Out with C++, 3rdEdition

    1

    The Increment and Decrement Operators

    ++ and -- are operators that add and subtract

    one from their operands.

    num = num + 1;

    num += 1;

    num++;

    Starting Out with C++, 3rdEdition

    2

    Program 5-1// This program demonstrates the increment and decrement

    // operators.

    #include

    void main(void)

    {

    int bigVal = 10, smallVal = 1;

    cout

  • 8/10/2019 Looping c

    2/13

    2

    Starting Out with C++, 3rdEdition

    5

    Program 5-2

    //This program demonstrates the prefix and postfix modes of the

    // increment and decrement operators.

    #include

    void main(void)

    {

    int bigVal = 10, smallVal = 1;

    cout

  • 8/10/2019 Looping c

    3/13

    3

    Starting Out with C++, 3rdEdition

    9

    5.2 Introduction to Loops - The while

    Loop

    A loop is part of a program that repeats.

    A while loop is a pre test loop - the

    expression is tested before the loop is

    executed

    while (expression)

    statement;

    Starting Out with C++, 3rdEdition

    10

    Program 5-3// This program demonstrates a simple while loop.

    #include

    void main(void)

    {

    int number = 0;

    cout number;}

    Starting Out with C++, 3rdEdition

    11

    Program Output with Example Input

    This program will let you enter number after number. Enter

    99 when you want to quit the program.

    1 [Enter]

    2 [Enter]

    30 [Enter]

    75 [Enter]

    99 [Enter]

    Starting Out with C++, 3rdEdition

    12

    Terminating a Loop

    A loop that does not have a way of stopping is called

    an infinite loop

    int test = 0;

    while (test < 10)

    cout

  • 8/10/2019 Looping c

    4/13

    4

    Starting Out with C++, 3rdEdition

    13

    Programming Style and the while Loop

    If there is only one statement repeated by

    the loop, it should appear on the line after

    the while statement and be indented one

    additional level

    If the loop repeats a block, the block shouldbegin on the line after the while statement

    and each line inside the braces should beindented

    Starting Out with C++, 3rdEdition

    14

    5.3 Counters

    A counter is a variable that is incremented

    or decremented each time a loop iterates.

    Starting Out with C++, 3rdEdition

    15

    Program 5-4

    // This program displays the numbers 1 through 10 and

    // their squares.

    #include

    void main(void)

    {

    int num = 1; // Initialize counter

    cout

  • 8/10/2019 Looping c

    5/13

    5

    Starting Out with C++, 3rdEdition

    17

    Program 5-5

    // This program displays the numbers 1 through 10

    and

    // their squares.

    #include

    void main(void)

    {

    int num = 0;

    cout

  • 8/10/2019 Looping c

    6/13

    6

    Starting Out with C++, 3rdEdition

    21

    Program continued from previous slide.

    while (count++ < numStudents)

    {

    int score1, score2, score3;

    float average;

    cout > score2 >> score3;

    average = (score1 + score2 + score3) / 3.0;

    cout

  • 8/10/2019 Looping c

    7/13

    7

    Starting Out with C++, 3rdEdition

    25

    Program continues

    while (count++ < days)

    {

    float sales;

    cout

  • 8/10/2019 Looping c

    8/13

    8

    Starting Out with C++, 3rdEdition

    29

    Program continued from previous slide.

    while (points != -1)

    {

    count++;

    cout > score3;

    average = (score1 + score2 + score3) / 3.0;

    cout

  • 8/10/2019 Looping c

    9/13

    9

    Starting Out with C++, 3rdEdition

    33

    Program Output w ith Example Input

    Enter 3 scores and I will average them: 80 90 70 [Enter]

    The average is 80.

    Do you want to average another set? (Y/N) y [Enter]

    Enter 3 scores and I will average them: 60 75 88 [Enter]

    The average is 74.333336.

    Do you want to average another set? (Y/N) n [Enter]

    Starting Out with C++, 3rdEdition

    34

    The for Loop

    Ideal for situations that require a counter

    because it has built-in expressions that

    initialize and update variables.

    for (initialization; test; update)

    statement;

    Starting Out with C++, 3rdEdition

    35

    Program 5-11// This program displays the numbers 1 through 10 and

    // their squares.

    #include

    void main(void)

    {

    int num;

    cout

  • 8/10/2019 Looping c

    10/13

    10

    Starting Out with C++, 3rdEdition

    37

    Omitting the for Loops Expressions

    int num = 1;

    for ( ; num

  • 8/10/2019 Looping c

    11/13

    11

    Starting Out with C++, 3rdEdition

    41

    Program Output with Example Input

    Enter the sales for day 1: 489.32 [Enter]

    Enter the sales for day 2: 421.65 [Enter]

    Enter the sales for day 3: 497.89 [Enter]

    Enter the sales for day 4: 532.37 [Enter]

    Enter the sales for day 5: 506.92 [Enter]

    Enter the sales for day 6: 489.01 [Enter]

    Enter the sales for day 7: 476.55 [Enter]

    The total sales are $3413.71

    Starting Out with C++, 3rdEdition

    42

    5.8 Other forms of the update

    expression

    Incrementing the counter by something besides 1:for(number = 2; number

  • 8/10/2019 Looping c

    12/13

    12

    Starting Out with C++, 3rdEdition

    45

    Program 5-13

    // This program averages test scores. It asks the user for the

    // number of students and the number of test scores per student.

    #include

    void main(void)

    {

    int numStudents, numTests, total;

    float average;

    cout numStudents;

    cout > numTests;

    Program continues

    Starting Out with C++, 3rdEdition

    46

    Program continued from previous slide.

    for (int count1 = 1; count1

  • 8/10/2019 Looping c

    13/13

    13

    Starting Out with C++, 3rdEdition

    49

    Program 5-14// This program raises the user's number to the powers

    // of 0 through 10.

    #include

    #include

    void main(void)

    {

    int value;

    char choice;

    cout > value;

    cout