What statement causes a loop to skip the remaining statements in the current iteration?

Problem

Write a while loop that lets the user enter a num…

Question:

Problem 29 Easy Difficulty


Video Answer:

Get the answer to your homework problem.

Try Numerade free for 7 days

Input your name and email to request the answer

Numerade Educator

Numerade Educator

Like

Report

Problem 1 Problem 2 Problem 3 Problem 4 Problem 5 Problem 6 Problem 7 Problem 8 Problem 9 Problem 10 Problem 11 Problem 12 Problem 13 Problem 14 Problem 15 Problem 16 Problem 17 Problem 18 Problem 19 Problem 20 Problem 21 Problem 22 Problem 23 Problem 24 Problem 25 Problem 26 Problem 27 Problem 28 Problem 29 Problem 30 Problem 31 Problem 32 Problem 33 Problem 34 Problem 35 Problem 36 Problem 37 Problem 38 Problem 39 Problem 40 Problem 41 Problem 42 Problem 43 Problem 44 Problem 45 Problem 46 Problem 47 Problem 48 Problem 49 Problem 50 Problem 51 Problem 52 Problem 53 Problem 54 Problem 55 Problem 56 Problem 57 Problem 58 Problem 59 Problem 60 Problem 61

Answer

Related Courses

AP CS

What statement causes a loop to skip the remaining statements in the current iteration?

Starting Out With C++: From Control Structures through Objects

Chapter 5

Looping

Related Topics

Computer Science Overview

Loops

Discussion

You must be signed in to discuss.

Top AP CS Educators

AP CS Courses

Lectures

What statement causes a loop to skip the remaining statements in the current iteration?

0:00

Errors 00 Intro

What statement causes a loop to skip the remaining statements in the current iteration?

0:00

Number System And Algorithms

Join Course

Recommended Videos

Watch More Solved Questions in Chapter 5

Problem 1

Problem 2

Problem 3

Problem 4

Problem 5

Problem 6

Problem 7

Problem 8

Problem 9

Problem 10

Problem 11

Problem 12

Problem 13

Problem 14

Problem 15

Problem 16

Problem 17

Problem 18

Problem 19

Problem 20

Problem 21

Problem 22

Problem 23

Problem 24

Problem 25

Problem 26

Problem 27

Problem 28

Problem 29

Problem 30

Problem 31

Problem 32

Problem 33

Problem 34

Problem 35

Problem 36

Problem 37

Problem 38

Problem 39

Problem 40

Problem 41

Problem 42

Problem 43

Problem 44

Problem 45

Problem 46

Problem 47

Problem 48

Problem 49

Problem 50

Problem 51

Problem 52

Problem 53

Problem 54

Problem 55

Problem 56

Problem 57

Problem 58

Problem 59

Problem 60

Problem 61

Video Transcript

No transcript available

The one-token statements continue and break may be used within loops to alter control flow; continue causes the next iteration of the loop to run immediately, whereas break terminates the loop and causes execution to resume after the loop. Both control structures must appear in loops. Both break and continue scope to the most deeply nested loop, but pass through non-loop statements.

Although these control statements may seem undesirable because of their goto-like behavior, their judicious use can greatly improve readability by reducing the level of nesting or eliminating bookkeeping inside loops.

Break Statements

When a break statement is executed, the most deeply nested loop currently being executed is ended and execution picks up with the next statement after the loop. For example, consider the following program:

while (1) {
  if (n < 0) break;
  foo(n);
  n = n - 1;
}

The while~(1) loop is a “forever” loop, because 1 is the true value, so the test always succeeds. Within the loop, if the value of n is less than 0, the loop terminates, otherwise it executes foo(n) and then decrements n. The statement above does exactly the same thing as

while (n >= 0) {
  foo(n);
  n = n - 1;
}

This case is simply illustrative of the behavior; it is not a case where a break simplifies the loop.

Continue Statements

The continue statement ends the current operation of the loop and returns to the condition at the top of the loop. Such loops are typically used to exclude some values from calculations. For example, we could use the following loop to sum the positive values in the array x,

real sum;
sum = 0;
for (n in 1:size(x)) {
  if (x[n] <= 0) continue;
  sum += x[n];
}

When the continue statement is executed, control jumps back to the conditional part of the loop. With while and for loops, this causes control to return to the conditional of the loop. With for loops, this advances the loop variable, so the the above program will not go into an infinite loop when faced with an x[n] less than zero. Thus the above program could be rewritten with deeper nesting by reversing the conditional,

real sum;
sum = 0;
for (n in 1:size(x)) {
  if (x[n] > 0)
    sum += x[n];
}

While the latter form may seem more readable in this simple case, the former has the main line of execution nested one level less deep. Instead, the conditional at the top finds cases to exclude and doesn’t require the same level of nesting for code that’s not excluded. When there are several such exclusion conditions, the break or continue versions tend to be much easier to read.

Breaking and Continuing Nested Loops

If there is a loop nested within a loop, a break or continue statement only breaks out of the inner loop. So

while (cond1) {
  ...
  while (cond2) {
    ...
    if (cond3) break;
    ...
  }
  // execution continues here after break
  ...
}

If the break is triggered by cond3 being true, execution will continue after the nested loop.

As with break statements, continue statements go back to the top of the most deeply nested loop in which the continue appears.

Although break and continue must appear within loops, they may appear in nested statements within loops, such as within the conditionals shown above or within nested statements. The break and continue statements jump past any control structure other than while-loops and for-loops.

What is used to skip current iteration of a loop?

The continue statement skips the rest of the instructions in a for or while loop and begins the next iteration. To exit the loop completely, use a break statement.

Which statement is used to skip the loop and continue with the next iteration?

The continue statement skips the current iteration of the loop and continues with the next iteration. Its syntax is: continue; The continue statement is almost always used with the if...else statement.

Which loop does break break?

Using break in a nested loop In a nested loop, a break statement only stops the loop it is placed in. Therefore, if a break is placed in the inner loop, the outer loop still continues. However, if the break is placed in the outer loop, all of the looping stops.

What is the break statement?

The break statement terminates the execution of the nearest enclosing do , for , switch , or while statement in which it appears. Control passes to the statement that follows the terminated statement.