+ All Categories
Home > Documents > break and continue - Service Lectures at D-INFKbreak;} break: continue;} continue:

break and continue - Service Lectures at D-INFKbreak;} break: continue;} continue:

Date post: 15-Feb-2021
Category:
Upload: others
View: 8 times
Download: 0 times
Share this document with a friend
45
break
Transcript
  • break

  • break – Explanation

    •Goal:• Stop loop immediately…• … and continue from after the loop.

    2

  • Example – break

    3

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    break;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – break

    4

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    break;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – break

    5

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    break;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – break

    6

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    break;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – break

    7

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    break;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – break

    8

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    break;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – break

    9

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    break;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – break

    10

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    break;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – break

    11

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    break;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – break

    12

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    break;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – break

    13

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    break;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – break

    14

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    break;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – break

    15

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    break;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – break

    16

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    break;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – break

    17

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    break;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – break

    18

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    break;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • continue

  • continue - Explanation

    •Goal:• Skip to the next iteration right away.

    20

  • Example – continue

    21

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    continue;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – continue

    22

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    continue;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – continue

    23

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    continue;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – continue

    24

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    continue;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – continue

    25

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    continue;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – continue

    26

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    continue;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – continue

    27

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    continue;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – continue

    28

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    continue;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – continue

    29

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    continue;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – continue

    30

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    continue;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – continue

    31

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    continue;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – continue

    32

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    continue;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – continue

    33

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    continue;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – continue

    34

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    continue;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – continue

    35

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    continue;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – continue

    36

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    continue;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – continue

    37

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    continue;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – continue

    38

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    continue;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – continue

    39

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    continue;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Example – continue

    40

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    continue;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • break VS continue

  • Contrast

    42

    int a = 18;

    int n = 0;

    // How many inputs (out of 5) are divisors of a?

    for (int i = 1; i > input;

    if (input == 0)

    break;

    else if (a % input == 0)

    ++n;

    }

    // Output

    std::cout

  • Remark

    •continue makes more sense here.

    43

  • Remark

    •continue makes more sense here.

    •Reason:• break-version skips later inputs

    44

  • Remark

    •continue makes more sense here.

    •Reason:• break-version skips later inputs

    • But output is still:

    Number of divisors: ...

    as if nothing went wrong.

    45


Recommended