# The Main Loop

In any interactive session, *Mathematica* effectively operates in a loop. It waits for your input, processes the input, prints the result, then goes back to waiting for input again. As part of this "main loop", *Mathematica* maintains and uses various global objects. You will often find it useful to work with these objects.

You should realize, however, that if you use *Mathematica* through a special front end, your front end may set up its own main loop, and what is said here may not apply.

In[n] | the expression on the n input line |

InString[n] | the textual form of the n input line |

%n or Out[n] | the expression on the n output line |

Out[{n_{1},n_{2},...}] | a list of output expressions |

%%...% (n times) or Out[-n] | the expression on the n previous output line |

MessageList[n] | a list of messages produced while processing the n line |

$Line | the current line number (resettable) |

In a standard interactive session, there is a sequence of input and output lines. *Mathematica* stores the values of the expressions on these lines in In[n] and Out[n].

As indicated by the usual In[n]:= prompt, the input expressions are stored with delayed assignments. This means that whenever you ask for In[n], the input expression will always be reevaluated in your current environment.

In[1]:= |

Out[1]= |

In[2]:= |

Out[2]= |

In[3]:= |

In[4]:= |

Out[4]= |

In[5]:= |

Out[5]//InputForm= | |

$HistoryLength | the number of previous lines of input and output to keep |

Specifying the length of session history to keep.

*Mathematica* by default stores *all* your input and output lines for the duration of the session. In a very long session, this may take up a large amount of computer memory. You can nevertheless get rid of the input and output lines by explicitly clearing the values of In and Out, using Unprotect[In, Out], followed by Clear[In, Out]. You can also tell *Mathematica* to keep only a limited number of lines of history by setting the global variable $HistoryLength.

Note that at any point in a session, you can reset the line number counter $Line, so that, for example, new lines are numbered so as to overwrite previous ones.

$PreRead | a function applied to each input string before being fed to Mathematica |

$Pre | a function applied to each input expression before evaluation |

$Post | a function applied to each expression after evaluation |

$PrePrint | a function applied after Out[n] is assigned, but before the result is printed |

$SyntaxHandler | a function applied to any input line that yields a syntax error |

Global functions used in the main loop.

*Mathematica* provides a variety of "hooks" that allow you to insert functions to be applied to expressions at various stages in the main loop. Thus, for example, any function you assign as the value of the global variable $Pre will automatically be applied before evaluation to any expression you give as input.

For a particular input line, the standard main loop begins by getting a text string of input. Particularly if you need to deal with special characters, you may want to modify this text string before it is further processed by *Mathematica*. You can do this by assigning a function as the value of the global variable $PreRead. This function will be applied to the text string, and the result will be used as the actual input string for the particular input line.

In[6]:= |

Out[6]= |

In[7]:= |

Out[7]= |

In[8]:= |

Once *Mathematica* has successfully read an input expression, it then evaluates this expression. Before doing the evaluation, *Mathematica* applies any function you have specified as the value of $Pre, and after the evaluation, it applies any function specified as the value of $Post. Note that unless the $Pre function holds its arguments unevaluated, the function will have exactly the same effect as $Post.

$Post allows you to specify arbitrary "postprocessing" to be done on results obtained from *Mathematica*. Thus, for example, to make *Mathematica* get a numerical approximation to every result it generates, all you need do is to set $Post=N.

In[9]:= |

Out[9]= |

In[10]:= |

Out[10]= |

In[11]:= |

As soon as *Mathematica* has generated a result, and applied any $Post function you have specified, it takes the result and assigns it as the value of Out[$Line]. The next step is for *Mathematica* to print the result. However, before doing this, it applies any function you have specified as the value of $PrePrint.

In[12]:= |

In[13]:= |

Out[13]= |

In[14]:= |

There are various kinds of output generated in a typical *Mathematica* session. In general, each kind of output is sent to a definite *output channel*, as discussed in "Streams and Low-Level Input and Output". Associated with each output channel there is a global variable that gives a list of the output streams to be included in that output channel.

$Output | standard output and text generated by Print |

$Echo | an echo of each input line (as stored in InString[n]) |

$Urgent | input prompts and other urgent output |

$Messages | standard messages and output generated by Message |

Output channels in a standard *Mathematica* session.

By modifying the list of streams in a given output channel, you can redirect or copy particular kinds of *Mathematica* output. Thus, for example, by opening an output stream to a file, and including that stream in the $Echo list, you can get each piece of input you give to *Mathematica* saved in a file.

Streams[] | list of all open streams |

Streams["name"] | list of all open streams with the specified name |

$Input | the name of the current input stream |

$InputFileName | the full absolute path to the file from which input is currently being sought |

Open streams in a *Mathematica* session.

The function Streams shows you all the input, output, and other streams that are open at a particular point in a *Mathematica* session. The variable $Input gives the name of the current stream from which *Mathematica* input is being taken at a particular point. $Input is reset, for example, during the execution of a Get command. Get also sets $InputFileName to the full absolute path of the file from which input is read.

$MessagePrePrint | a function to be applied to expressions that are given in messages |

$Language | list of default languages to use for messages |

There are various global parameters that determine the form of messages generated by *Mathematica*.

As discussed in "Messages", typical messages include a sequence of expressions that are combined with the text of the message through StringForm. $MessagePrePrint gives a function to be applied to the expressions before they are printed. The default for $MessagePrePrint uses Short for text formatting and a combination of Short and Shallow for typesetting.

As discussed in "International Messages", *Mathematica* allows you to specify the language in which you want messages to be produced. In a particular *Mathematica* session, you can assign a list of language names as the value of $Language.

Exit[]orQuit[] | terminate your Mathematica session |

$Epilog | a global variable to be evaluated before termination |

Terminating *Mathematica* sessions.

*Mathematica* will continue in its main loop until you explicitly tell it to exit. Most *Mathematica* interfaces provide special ways to do this. Nevertheless, you can always do it by explicitly calling Exit or Quit.

*Mathematica* allows you to give a value to the global variable $Epilog to specify operations to perform just before *Mathematica* actually exits. In this way, you can, for example, make *Mathematica* always save certain objects before exiting.

$IgnoreEOF | whether to ignore the end-of-file character |

A global variable that determines the treatment of end-of-file characters.

As discussed in "Special Characters: Strings and Characters", *Mathematica* usually does not treat special characters in a special way. There is one potential exception, however. With the default setting $IgnoreEOF=False, *Mathematica* recognizes end-of-file characters. If *Mathematica* receives an end-of-file character as the only thing on a particular input line in a standard interactive *Mathematica* session, then it will exit the session.

Exactly how you enter an end-of-file character depends on the computer system you are using. Under Unix, for example, you typically press Ctrl+D.

Note that if you use *Mathematica* in a "batch mode", with all its input coming from a file, then it will automatically exit when it reaches the end of the file, regardless of the value of $IgnoreEOF.