Reason
  • 문서
  • 해보기
  • API
  • 커뮤니티
  • 블로그
  • Languages icon한국어
    • 日本語
    • English
    • Deutsch
    • Español
    • Français
    • Português (Brasil)
    • Русский
    • Українська
    • 中文
    • 繁體中文
    • 번역 돕기
  • GitHub

›언어 기본

소개

  • What & Why

Setup

  • 설치
  • 에디터 플러그인

언어 기본

  • 개요
  • Let 바인딩
  • 원시 타입
  • 기본 자료구조
  • 타입
  • 레코드
  • Variant
  • Options and nullability
  • 함수
  • 재귀
  • 비구조화
  • 패턴 매칭
  • Mutable Bindings
  • 반복문
  • Modules

Advanced Features

  • JSX
  • 외부 접근
  • 예외
  • 오브젝트

JavaScript

  • 연동
  • 문법 치트시트
  • Pipe First
  • 프라미스
  • 라이브러리
  • JS에서 변환

추가 사항

  • 자주 물어보는 질문
  • 추가적으로 매력적인 것들
Translate

반복문

Quick overview: Loops

Loops are discouraged in most cases. Instead functional programming patterns like map, filter, or reduce can usually be used in their place.

For Loops

For loops use the for, in, and to keywords. For loops always increment or decrement a counter by one on each iteration. This increment cannot be changed, use while loops for more control over the increment.

let x = 1;
let y = 5;

for (i in x to y) {
  print_int(i);
  print_string(" ");
};
/* Prints: 1 2 3 4 5 */

The reverse direction is also possible using the downto keyword:

for (i in y downto x) {
  print_int(i);
  print_string(" ");
};
/* Prints: 5 4 3 2 1 */

While Loops

While loops allow executing code until a certain condition is met. It is not restricted to counting between two integers like for loops.

It is common to use refs with while loops.

let i = ref(1);

while (i^ <= 5) {
  print_int(i^);
  print_string(" ");
  i := i^ + 2;
};
/* Prints: 1 3 5 */

Break

There is no break or early return behavior in Reason. In order to achieve something similar create a boolean ref:

let break = ref(false);

while (!break^ && condition) {
  if (shouldBreak()) {
    break := true;
  } else {
    /* normal code */
  };
};
← Mutable BindingsModules →
  • For Loops
  • While Loops
    • Break