Credits Overview Plotting Styles Commands Terminals

iteration

figure_newsyntax

gnuplot supports command iteration and block-structured if/else/while/do constructs. See if, while, and do. Simple iteration is possible inside plot or set commands. See plot for. General iteration spanning multiple commands is possible using a block construct as shown below. For a related new feature, see the summation expression type. Here is an example using several of these new syntax features:

      set multiplot layout 2,2
      fourier(k, x) = sin(3./2*k)/k * 2./3*cos(k*x)
      do for [power = 0:3] {
          TERMS = 10**power
          set title sprintf("%g term Fourier series",TERMS)
          plot 0.5 + sum [k=1:TERMS] fourier(k,x) notitle
      }
      unset multiplot

Iteration is controlled by an iteration specifier with syntax

     for [<var> in "string of N elements"]

or

     for [<var> = <start> : <end> { : <increment> }]

In the first case <var> is a string variable that successively evaluates to single-word substrings 1 to N of the string in the iteration specifier. In the second case <start>, <end>, and <increment> are integers or integer expressions.

The scope of the iteration variable is private to that iteration. See scope. You cannot permanently change the value of the iteration variable inside the iterated clause. If the iteration variable has a value prior to iteration, that value will be retained or restored at the end of the iteration. For example, the following commands will print 1 2 3 4 5 6 7 8 9 10 A.

     i = "A"
     do for [i=1:10] { print i; i=10; }
     print i