# HG changeset patch # User Steve Losh # Date 1319603800 14400 # Node ID 2ec4586dd43ae35a18b1e9ad02ae8d020f839051 # Parent 1b686fa14d4a052d89d60fecdc2b78ebf3c6be24 Loops! diff -r 1b686fa14d4a -r 2ec4586dd43a chapters/36.markdown --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/chapters/36.markdown Wed Oct 26 00:36:40 2011 -0400 @@ -0,0 +1,59 @@ +Looping +======= + +You might be surprised to realize that we've gone through thirty five chapters +of a programming language book without even mentioning loops! Vimscript offers +so many other options for performing actions on text (`normal!`, etc) that loops +aren't as necessary as they are in most other languages. + +Even so, you'll definitely need them some day, so now we'll take a look at the +two main kinds of loops Vim supports. + +For Loops +--------- + +The first kind of loop is the `for` loop. This may seem odd if you're used to +Java, C or Javascript `for` loops, but turns out to be quite elegant. Run the +following commands: + + :let c = 0 + + :for i in [1, 2, 3, 4] + : let c += i + :endfor + + :echom c + +Vim displays "10", which is the result of adding together each element in the +list. Vimscript `for` loops iterate over lists (or dictionaries, which we'll +cover later). + +There's no equivalent to the C-style `for (int i = 0; i < foo; i++)` loop form in +Vimscript. This might seem bad at first, but in practice you'll never miss it. + +While Loops +----------- + +Vim also supports the classic `while` loop. Run the following commands: + + :let c = 1 + :let total = 0 + + :while c <= 4 + : let total += c + : let c += 1 + :endwhile + + :echom total + +Once again Vim displays "10". This loop should be familiar to just about anyone +who's programmed before, so we won't spend any time on it. You won't use it +very often. Keep it in the back of your mind for the rare occasions that you +want it. + +Exercises +--------- + +Read `:help for`. + +Read `:help while`.