Spec-Zone.ru › Octave 9

10.6 Оператор break

Оператор break выполняет выход из самого внутреннего цикла while, do-until или for, в который он вложен. Оператор break можно использовать только внутри тела цикла. Следующий пример находит наименьший делитель заданного целого числа, а также определяет, является ли число простым:

num = 103;
div = 2;
while (div*div <= num)
  if (rem (num, div) == 0)
    break;
  endif
  div++;
endwhile
if (rem (num, div) == 0)
  printf ("Smallest divisor of %d is %d\n", num, div)
else
  printf ("%d is prime\n", num);
endif

Когда остаток от деления равен нулю в первом операторе while, Octave немедленно выходит из цикла. Это означает, что Octave сразу переходит к оператору, следующему за циклом, и продолжает выполнение. (Это сильно отличается от оператора exit, который останавливает всю программу Octave.)

Вот другая программа, эквивалентная предыдущей. Она показывает, что условие оператора while можно с тем же успехом заменить оператором break внутри if:

num = 103;
div = 2;
while (1)
  if (rem (num, div) == 0)
    printf ("Smallest divisor of %d is %d\n", num, div);
    break;
  endif
  div++;
  if (div*div > num)
    printf ("%d is prime\n", num);
    break;
  endif
endwhile

© 1996–2026 The Octave Project Developers
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://docs.octave.org/v9.4.0/The-break-Statement.html

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API