The Main Loop

In any interactive session, the Wolfram System 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", the Wolfram System 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 the Wolfram System 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^(th) input line
InString[n]the textual form of the n^(th) input line
%n or Out[n]the expression on the n^(th) output line
Out[{n1,n2,}]a list of output expressions
%%% (n times) or Out[-n]the expression on the n^(th) previous output line
MessageList[n]a list of messages produced while processing the n^(th) line
$Linethe current line number (resettable)

Input and output expressions.

In a standard interactive session, there is a sequence of input and output lines. The Wolfram System 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.

This assigns a value to .
Click for copyable input
Now the value for is used.
Click for copyable input
This removes the value assigned to .
Click for copyable input
This is reevaluated in your current environment, where there is no value assigned to .
Click for copyable input
This gives the textual form of the second input line, appropriate for editing or other textual manipulation.
Click for copyable input
$HistoryLengththe number of previous lines of input and output to keep

Specifying the length of session history to keep.

The Wolfram System 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 the Wolfram System 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.

$PreReada function applied to each input string before being fed to the Wolfram System
$Prea function applied to each input expression before evaluation
$Posta function applied to each expression after evaluation
$PrePrinta function applied after Out[n] is assigned, but before the result is printed
$SyntaxHandlera function applied to any input line that yields a syntax error

Global functions used in the main loop.

The Wolfram System 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 the Wolfram System. 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.

This tells the Wolfram System to replace by {} in every input string.
Click for copyable input
You can now enter lists as expressions.
Click for copyable input
You can remove the value for $PreRead like this, at least so long as your definition for $PreRead does not modify this very input string.
Click for copyable input

Once the Wolfram System has successfully read an input expression, it then evaluates this expression. Before doing the evaluation, the Wolfram System 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 the Wolfram System. Thus, for example, to make the Wolfram System get a numerical approximation to every result it generates, all you need do is to set $Post=N.

This tells the Wolfram System to apply N to every result it generates.
Click for copyable input
Now the Wolfram System gets a numerical approximation to anything you type in.
Click for copyable input
This removes the postprocessing function you specified.
Click for copyable input

As soon as the Wolfram System 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 the Wolfram System to print the result. However, before doing this, it applies any function you have specified as the value of $PrePrint.

This tells the Wolfram System to shorten all output to two lines.
Click for copyable input
Only a twoline version of the output is now shown.
Click for copyable input
This removes the value you assigned to $PrePrint.
Click for copyable input

There are various kinds of output generated in a typical Wolfram System 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.

$Outputstandard output and text generated by Print
$Echoan echo of each input line (as stored in InString[n])
$Urgentinput prompts and other urgent output
$Messagesstandard messages and output generated by Message

Output channels in a standard Wolfram System session.

By modifying the list of streams in a given output channel, you can redirect or copy particular kinds of Wolfram System 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 the Wolfram System saved in a file.

Streams[]list of all open streams
Streams["name"]list of all open streams with the specified name
$Inputthe name of the current input stream
$InputFileNamethe full absolute path to the file from which input is currently being sought

Open streams in a Wolfram System session.

The function Streams shows you all the input, output, and other streams that are open at a particular point in a Wolfram System session. The variable $Input gives the name of the current stream from which the Wolfram System 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.

$MessagePrePrinta function to be applied to expressions that are given in messages
$Languagelist of default languages to use for messages

Parameters for messages.

There are various global parameters that determine the form of messages generated by the Wolfram System.

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", the Wolfram System allows you to specify the language in which you want messages to be produced. In a particular Wolfram System session, you can assign a list of language names as the value of $Language.

Exit[]orQuit[]terminate your Wolfram System session
$Epiloga global variable to be evaluated before termination

Terminating Wolfram System sessions.

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

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

$IgnoreEOFwhether to ignore the endoffile character

A global variable that determines the treatment of endoffile characters.

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

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

Note that if you use the Wolfram System 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.