Skip to Content
Learn Red ? Fundamentals of Red
book

Learn Red ? Fundamentals of Red

by Ivo Balbaert
May 2018
Beginner content levelBeginner
252 pages
6h 19m
English
Packt Publishing
Content preview from Learn Red ? Fundamentals of Red

Repeating with a condition – while or until

On other occasions, you might want the loop continuations to depend on a condition:

  • You may want to continue while a certain condition is true, and stop with the loop when the condition turns false

  • You may want to do the opposite—loop until a certain condition is true

Here is an example of the while word, printing out the odd integers smaller than 10:

 n: 1 while [n <= 10 ][     if odd? n [prin n prin " "]     n: n + 1 ] ;== 1 3 5 7 9
Note that the condition (here, n <= 10) must be in a code block, [ ]. This block may contain multiple statements and expressions; the last one is taken as a condition.

We can have the same result by simulating a second step:

 n: 1 while [n <= 10 ][     prin n prin " " n: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

How I Built a Personal Board of Directors With GenAI

How I Built a Personal Board of Directors With GenAI

Vipin Gupta
How to Become a Game-Changing Leader

How to Become a Game-Changing Leader

Douglas A. Ready, Alan Mulally
Why AI Demands a New Breed of Leaders

Why AI Demands a New Breed of Leaders

Faisal Hoque, Thomas Davenport, Erik Nelson

Publisher Resources

ISBN: 9781789130706Supplemental Content