Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Break and Continue with Nested Loops | Nested Loops
Python Loops
course content

Зміст курсу

Python Loops

Python Loops

1. While Loops: Get Started
2. While Loops: Essentials
3. For Loops
4. Nested Loops

Break and Continue with Nested Loops

You still can use break and continue statements inside the nested loops. But the execution depends on the statement's position in code. Thus, if you use break inside the inner loop, after executing it you leave the inner loop only, but continue to execute commands inside the outer one.

12345678
for i in range(10): print('Outer loop starts!') for j in range(10): print(i, j) if i==j: print('Leaving inner loop') break print('Outer loop is over!')
copy

The same about continue: you'll go back to the beginning of the inner loop.

Infinite Nested Loops

As you may guess, if one of the loops in infinite, no matter it is outer or inner one, the whole program is infinite, too. Remember to double-check the process and stop conditions for each loop.

Завдання

Is everything okay with the given code? Ware the infinite program execution!

  • Think about which loop causes the infinite execution.
  • Modify the code to make it valid and finite.

Завдання

Is everything okay with the given code? Ware the infinite program execution!

  • Think about which loop causes the infinite execution.
  • Modify the code to make it valid and finite.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 4. Розділ 2
toggle bottom row

Break and Continue with Nested Loops

You still can use break and continue statements inside the nested loops. But the execution depends on the statement's position in code. Thus, if you use break inside the inner loop, after executing it you leave the inner loop only, but continue to execute commands inside the outer one.

12345678
for i in range(10): print('Outer loop starts!') for j in range(10): print(i, j) if i==j: print('Leaving inner loop') break print('Outer loop is over!')
copy

The same about continue: you'll go back to the beginning of the inner loop.

Infinite Nested Loops

As you may guess, if one of the loops in infinite, no matter it is outer or inner one, the whole program is infinite, too. Remember to double-check the process and stop conditions for each loop.

Завдання

Is everything okay with the given code? Ware the infinite program execution!

  • Think about which loop causes the infinite execution.
  • Modify the code to make it valid and finite.

Завдання

Is everything okay with the given code? Ware the infinite program execution!

  • Think about which loop causes the infinite execution.
  • Modify the code to make it valid and finite.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

Секція 4. Розділ 2
toggle bottom row

Break and Continue with Nested Loops

You still can use break and continue statements inside the nested loops. But the execution depends on the statement's position in code. Thus, if you use break inside the inner loop, after executing it you leave the inner loop only, but continue to execute commands inside the outer one.

12345678
for i in range(10): print('Outer loop starts!') for j in range(10): print(i, j) if i==j: print('Leaving inner loop') break print('Outer loop is over!')
copy

The same about continue: you'll go back to the beginning of the inner loop.

Infinite Nested Loops

As you may guess, if one of the loops in infinite, no matter it is outer or inner one, the whole program is infinite, too. Remember to double-check the process and stop conditions for each loop.

Завдання

Is everything okay with the given code? Ware the infinite program execution!

  • Think about which loop causes the infinite execution.
  • Modify the code to make it valid and finite.

Завдання

Is everything okay with the given code? Ware the infinite program execution!

  • Think about which loop causes the infinite execution.
  • Modify the code to make it valid and finite.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів

Все було зрозуміло?

You still can use break and continue statements inside the nested loops. But the execution depends on the statement's position in code. Thus, if you use break inside the inner loop, after executing it you leave the inner loop only, but continue to execute commands inside the outer one.

12345678
for i in range(10): print('Outer loop starts!') for j in range(10): print(i, j) if i==j: print('Leaving inner loop') break print('Outer loop is over!')
copy

The same about continue: you'll go back to the beginning of the inner loop.

Infinite Nested Loops

As you may guess, if one of the loops in infinite, no matter it is outer or inner one, the whole program is infinite, too. Remember to double-check the process and stop conditions for each loop.

Завдання

Is everything okay with the given code? Ware the infinite program execution!

  • Think about which loop causes the infinite execution.
  • Modify the code to make it valid and finite.

Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Секція 4. Розділ 2
Перейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt