Pascal - Statements
Every program is in essence a set of instructions for the computer to perform. The statements of a program provide the instructions that are to be performed. Statements in Pascal are described in the following syntax diagram. This indicates that a statement is either a simple statement, structured statement or a asm statement. Statements optionally be labelled for use with the goto statement.
A simple statement does not contain other statements. There are four kinds of simple statement, these are the assignment statement, procedure statement, raise statement, and goto statement. Simple statements are described in the following syntax diagram.
The assignment statement is used to assign a value to a variable. The assignment statement starts with a variable identifier followed by one of the assignment operators and then an expression. There are five different assignment operators, each of which provides slightly different functionality.
- := this is the basic assignment operator. This indicates that the value of the expression is assigned to the varaible.
- += this adds the value of the expression to the variable. i += a + 1; is the same as i := i + (a + 1);
- -= this subtracts the value of the expression from the variable. i -= a + 1; is the same as i := i - (a + 1);
- *= this assigns the value of the variable multiplied by the expression. i *= a + 1; is the same as i := i * (a + 1);
- /= this assigns the value of the variable divided by the expression. i /= a + 1; is the same as i := i / (a + 1);
Within a function you can assign a value to the function identifier (i.e. the name of the funtion). This assigns the value that is returned when the function ends. With Delphi Pascal there is now a special result identifier which can be set for this purpose (i.e. to set the value to return). Use this result identifier rather than assigning a value to the function identifier.
The following code illustrates valid assignment statements.
a := 10; //assign to a the value 10 myVariable := a + 1; //assign to myVariable the value of a + 1 i += 1; //this is the same as i := i + 1 i *= a + 10; //this is the same as i := i * (a + 10)
The procedure statement is used to call (i.e. execute) a procedure. There are actually four different mechanisms used to call procedures. The one mainly used is to use the procedure identifier to identify which procedure should be called. Following the procedure identifier is the list of arguments that are to be passed to the procedure's parameters. This is described in the following syntax diagram.
The code below illustrates the declaration of a number of procedures followed by examples which call these procedures.
procedure Main(); ... procedure SayHello(name: String); ... procedure PrintMultiple(message: String; times: Integer); ... procedure PrintMax(value1, value2, value3: Integer); ... //example calls to the above procedures. Main(); SayHello('Fred'); SayHello(myName); //passes the value of myName to the name parameter PrintMultiple('silly ', 10); PrintMultiple('silly ', i + 6); PrintMax(v1, v2, v3); //passes the value of the v1, v2, and v3 variables PrintMax(10, v2, 23);
The goto statement allows you to write code that jumps to a label within the program. The use of this statement represents poor programming practice as this bypasses the structured programming constructs.
The raise statement is part of Delphi Pascal's exception handling mechanisms. This is covered in the exceptions section.
A structured statement contains other statements. There are five different kinds of structured statement in Delphi Pascal. These are the compound statement, repetitive statement, conditional statement, exception statement, and with statement. We will examine the compound statement, repetitive statement, conditional statement here, while the exception statement is covered in exceptions and the with statement is covered in records.
The compound statement is used to group together a number of statements within a single statement. The compound statement is used to group the statements within programs, as well as functions and procedures. The compound statement can also be used within other structured statements such as the while statement and if statement.
The compound statement starts with the begin keyword. The begin keyword is then followed by one or more statements separated by semicolons (;). The end of the compound statement is marked by the end keyword. The following are examples of valid compound statements.
//a compound statement containing a single empty statement. begin end //a compound statement containing a single procedure statement //calling WriteLn begin WriteLn('Hello World') end //a compound statement containing a procedure statement //calling WriteLn, and an empty statement begin WriteLn('Hello World'); end //a compound statement containing: // * A procedure statement calling WriteLn('1') // * A compound statement containing: // - A procedure statement calling WriteLn('2') // * A procedure statement calling WriteLn('3') begin WriteLn('1'); begin WriteLn('2') end WriteLn('3') end
Pascal contains three different kinds of repetative statement: the for statement, repeat statement, and while statement. Each of these statements allows one (or more) statements to be repeated a number of times. The following syntax diagram illustrates this syntax.
The for statement is used to create a loop that executes a fixed number of times. The control variable in the for loop will start at the initial value and be either incremented by one (in the case of a to for loop) or decremented by one (in the case of a downto for loop) for each time the body of the loop executes. The loop will continue to execute until the control variable passes the final value. The following shows the syntax diagram for the for statement.
The following code snippets are examples of valid for statements (aka for loops). Notice that the for loop executes a single statements, if you want multiple statements to be performed you can put them inside a compound statement.
//this for loop writes the value of i to the console for i := 0 to 10 do WriteLn('i = ', i); //this for loop does the same as above for myIndex := 0 to 10 do begin Write('myIndex = '); WriteLn(myIndex); end; //the initial and final values can be any valid expression (with an ordinal type) for index := 10 - 10 + 1 * 2 to MyFunction(10) - 2 do begin ... end;
The repeat statement is used to repeat a set of statements until a condition is met (i.e. while the condition is false). The repeat statement starts with the repeat keyword and is followed by a number of statements separated by semicolons (;). This means that you dont need to use a compound statement in conjuncture with the repeat statement as you do with the for statement, and while statement. The end of the repeat statement is marked by the until keyword and a Boolean expression. The Boolean expression controls the number of times the loop executes. The repeat statement is shown in the following syntax diagram.
The following code snippets show valid repeat statements. Notice that unlike the for statement you dont need to add a compound statement to contain the multiple statements.
//Repeats until the user presses a key (from the CRT unit) repeat Write('.'); Delay(100); until KeyPressed; //Repeats until x > 10; repeat Write('x is ', x); x += 1; until x > 10;
The while statement executes a statement repeatedly while the expression is true. The while statement starts with the while keyword followed by an expression and then the do keyword. The expression controls the number of times the loop is repeated. After the do keyword there is a single statement that is executed while the expression is true. If you want the loop to execute multiple statements you need to use a compound statement here. The following syntax diagram illustrates the while statement.
The following code snippets are all valid while statements. Notice the use of the compound statement to group together the statements.
//This while loop prints out the value of x while x < 10 do begin WriteLn('x = ', x); x += 1; end; //Similar to the repeat code above, //this will write a number of '.' characters to the console //while the user has not pressed a key (needs the CRT unit) while Not KeyPressed() do begin Write('.'); Delay(100); end; //the same as above while false = KeyPressed() do begin Write('.'); Delay(100); end;
A conditional statement is a statement that allows a selected statement to be executed based upon some condition. There are two kinds of conditional statement in Pascal: the case statement and the if statement. Both of these statements provide the ability to select which statement is to be executed in a given condition. The conditional statement is shown in the following syntax diagram.
The case statement allows you to execute statements based upon the value of a variable of an ordinal type. The case statement starts with the case keyword and is followed by an expression which is used to determine the value to be used to control the case statement. The of keyword follows the expression. After of, there is a number of case parts which are separated by semicolons (;). Each of these cases provides a number of a constants or ranges of constants separated by commas (,), followed by a colon (:) and the statement to execute when the value matches one of these constants. Each of the case 's indicates the value (or range of values) the expression must have in order to execute the statement. Following the case parts is the else part the statement within this part is executes in the case where the expression does not match any of the cases.
The following diagram more clearly describes this syntax.
The following are all valid case statements:
case x of 1: WriteLn('x is 1'); 2..5, 7: WriteLn('x is between 2 and 5, or it is 7'); 8, 9, 10: WriteLn('x is 8, 9, or 10'); else WriteLn('x is not one of the cases...'); end; //the following gets the 2nd character from aStringVariable case aStringVariable of 'a'..'z': WriteLn('Its lower case'); 'A'..'Z': WriteLn('Its upper case'); else WriteLn('Its something else...'); begin //using compound statements looks awkward... its usually better to //call procedures passing in what you need case x + 2 * y of -5..5: begin Write('here'); WriteLn(' in the -5, 5 case'); end; -10..-6: ...
The if statement provides the ability to selectively run one statement or another. The if statement starts with the if keyword followed by an expression and the then keyword. Following the then keyword is a single statement that is executed when the expression is true. You can also add an else to the if statement. This is the else keyword followed by a single statement that is executed when the expression is false. If you want to perform more than a single statement you need to group them together within a compound statement. The following syntax diagram illustrates the syntax of the if statement.
The following code snippets illustrate valid if statements.
//This shows a single if statement without an else part //the statement that follows this is executed regardless of the //value of a > b. if a > b then WriteLn('a is larger than b'); if a > b then WriteLn('a is larger than b'); else WriteLn('a is not larger than b'); //this is the same as above but includes compound statements //allowing more than one statement to be executed in each //branch if a > b then begin WriteLn('a is larger than b'); WriteLn('this is shown when the expression is true'); end else begin WriteLn('a is not larger than b'); WriteLn('this is shown when the expression is false'); end
Statements are used to express the instructions for your program. These are either simple statements or structured statements. All Pascal programs, regardless of their complexity, are made up of these statements. When you are writing programs these syntax diagrams can help you ensure that you get the syntax correct. A syntactically correct program will compile and execute, the next step in learning to program is then to ensure that the program does what you want...