Introduction: First GAUSS Statements
This tutorial will introduce you to basic GAUSS statements, which are the building block of every GAUSS program, and also to the practice of entering interactive commands in the GAUSS program input/output window.
Begin by opening GAUSS and navigating to the Command Page by selecting the "Command" tab from the vertical toolbar on the left-hand side of the GAUSS user interface as shown in the image above.
Types of GAUSS Statements
There are two main types of statements in GAUSS: assignments and print statements.
Assignment statements
An assignment is performed by entering a variable name followed by an equals sign and then the new value on the right side of the equals sign. For example, suppose you have 5 friends coming over for dinner. We can make a variable named friends
that is equal to 5 by entering:
friends = 5;
The new value on the right-hand side of the equals sign can be a simple literal value as we just did or it can also be an expression. For example, if we are budgeting $12 per guest at our dinner party, we could assign a new variable dinner_budget
like this:
dinner_budget = friends * 12;
As you probably expect, GAUSS will allow you to make much more complicated expressions. For now, however, we will keep it very simple.
Print statements
Now that we have made some assignment statements, we will make a print statement to verify that our assignment accomplished what we expected. The most basic print statement is the print
keyword followed by a space and a variable name, like this:
print friends;
which should return the following output:
5.0000
The print
keyword will allow you to print more than one variable at a time. To print multiple variables, the print
keyword should be followed by a space delimited list of variable names. Let's suppose that the 5 friends coming over for dinner have a total of 3 kids. We can create a new variable kids
to keep track of the number of kids coming to the party by entering the following assignment statement:
kids = 3;
Now we can print friends
and kids
by entering the following statement:
print friends kids;
This should return:
5.0000 3.0000
Printing expressions
Let's take a closer look at this last print
statement. It is telling GAUSS to print the contents of the variable friends
and then to print the contents of the variable kids
by passing a list of variables, separated by spaces to the print
keyword. It is important to note that this is a space delimited list and NOT an expression. One common mistake for new GAUSS users is to put an expression in a print
statement, like this:
print friends + kids;
They expect it to return 8 because they think they are telling GAUSS to print the result of the statement (5 + 3). However, since we know that the print
statement takes a list of variables separated by spaces, we know that that statement is actually telling GAUSS to:
- print the contents of the variable
friends
- print the '+' operator
- print the contents of the variable
kids
.
An error will occur when GAUSS attempts to print the plus operator by itself.
To add an expression to a print
statement, you need to enclose it in parentheses like this:
print (friends + kids);
Since parentheses have a very high operator precedence, the above statement tells GAUSS to 1) Evaluate: friends
+ kids
and then to 2) print the result.
Implicit print statements
Earlier on we told you that print statements required the print
keyword at the beginning. However, this is actually not true. If you remove the print
keyword from any of the print statements above, you will notice that they work the same without the print statement. Therefore:
print (friends + kids);
is equivalent to:
(friends + kids);
If you try to print this expression:
print friends + kids;
or this equivalent expression:
friends + kids;
it will result in an error. You can successfully print this expression in an implicit print statement by either adding parentheses as we mentioned earlier:
(friends + kids);
or by removing the spaces between the elements in the expression:
friends+kids;
Since there are no spaces in this last example the entire expression is passed to the print
keyword as one item to be printed (after it is evaluated). These expressions can be combined with other variables or expressions in one print statement, for example:
print (friends + kids) friends kids;
or
friends+kids friends kids;
will return:
8.0000 5.0000 3.0000
Take a few minutes to create some variables and create print statements with and without expressions.