Day 4
This is the day 4 page for computer programming course 101, an introductions to computer programming. Day 4 introduces the concept of statements.Below are the topics and course materials we are learning today.
Statements
Statements define the algorithmic logic of your programs. Simple statements like assignments and procedure calls can combine to form loops, conditional statements, and other structured statements. Multiple statements within a block are separated by semicolons.Simple Statements
A simple statement doesn't contain any other statements. Simple statements include assignments, calls to procedures and functions.An assignment statement has the form:
1
Variable := Expression;
1
I :=
3
;
1
I := I +
1
;
1
X := Y + Z;
2
Done := (I >=
1
)
and
(I <
100
);
3
Purple := Mix(Blue, Red);
4
I := Sqr(J) - I * K;
5
SomeText[I +
1
] := Chr(I);
Procedure and Function Call Statements
A procedure call consists of the name of a procedure, followed by an argument list if required. Here are some examples of procedure or function calls.1
WriteLn(
'Hello World!'
);
2
PrintHeading;
3
Transpose(X, Y, Z);
4
Find(FirstName, LastName);
5
CalculateRoute(Location);
6
Sqrt(X);
Compound Statements
A compound statement is a sequence of other (simple or structured) statements to be executed in the order in which they are written. The compound statement is bracketed by the reserved words begin and end, and its constituent statements are separated by semicolons. For example:1
begin
2
T := X;
3
X := Y;
4
Y := Z;
5
Z := T;
6
end
;
1
begin
2
I := Length(S);
3
while
I >
0
do
4
begin
5
WriteLn(S[I]);
6
I := I -
1
;
7
end
;
8
end
;
1
if
Condition
then
2
begin
3
end
;
Flow Control Statements
Most all computer languages offer various kinds of statements to control the flow of your programs. These flow control statement have different uses, and different condition types, but all are used to divert the flow of statements between on or more other statements.If Statements
The if statement is probably the most common flow control statement computer programmers use. It is used to divert the flow to a statement in the event a condition is True, and optionally to another statement if the condition is False. There two form of the if statement take the form of:1
if
Expression
then
StatementOne;
1
if
Expression
then
StatementOne
else
StatementTwo;
1
if
FileExists(FileName)
then
2
FileRead(FileName)
3
else
4
WriteLn(
'Could not open file '
, FileName);
If statements, like all other conditional statements, can be nested. When nesting if statements they may take the following form:
01
if
Total >
0
then
02
if
Total > AccountBalance
then
03
WriteLn(
'Total exceeds account balance. Purchase declined.'
)
04
else
05
begin
06
Order := ProcessCurrentTransaction;
07
WriteLn(
'You order number is '
, Order);
08
end
09
else
10
WriteLn(
'You have nothing in your shopping cart'
);
Case Statements
The case statement may provide a readable alternative to deeply nested if conditionals. A case statement has the form of:1
case
Expression
of
2
Label1: Statement1;
3
Label2: Statement2;
4
Label3: Statement2;
5
else
Statement4;
6
end
;
01
case
CurrentKey
of
02
KeyLeft: PlayerMoveLeft;
03
KeyRight: PlayerMoveRight;
04
KeySpace: PlayerFire;
05
KeyEnter: ToggleChat;
06
KeyTilda:
07
begin
08
GamePause;
09
ShowConsole;
10
PrintInfo;
11
end
;
12
else
13
LogKeyPress(
'No action is mapped to '
, CurrentKey);
14
end
;
Looping Statements
Looping statements allow you to execute a sequence of statements repeatedly, using a control condition to determine when the execution stops. Pascal has three kinds of looping statements, repeat , while statements, and for statements.All looping statements allow for special procedures to prematurely either stop or restart a loop. These procedures are Break, terminates the statement in which it occurs, and Continue to restart a executing the at the next iteration of the loop.
Repeat Statements
The syntax of a repeat statement is:1
repeat
Statement
until
Expression;
Here is an example of a repeat statement:
1
repeat
2
WriteLn(
'Enter a value from (1..100):'
);
3
ReadLn(I);
4
until
(I >=
1
)
and
(I <=
100
);
While Statements
A while statement is similar to a repeat statement, except that the control condition is evaluated before the first execution of the statement sequence. Hence, if the condition is False, the statement sequence is never executed.The syntax of a while statement is:
1
while
Expression
do
Statement;
Here is an example of a while statement:
1
while
Time < Alarm
do
2
begin
3
CheckMessages;
4
Sleep(
10
);
5
end
;
For Statements
A for statement, unlike a repeat or while statement, requires you to specify explicitly the number of iterations you want the loop to go through. The syntax of a for statement is:1
for
Counter := StartValue
to
FinalValue
do
Statement;
1
for
Counter := FinalValue
downto
StartValue
do
Statement;
Here is an example of a for statement:
1
for
I :=
1
To
Length(Text)
do
2
Text[I] := UpCase(Text[I]);
Homework
This day your homework is to modify the following number guess program to meet these requirements:01
program
NumberGuess;
02
03
var
04
Number, Guess: Integer;
05
begin
06
Number := Random(
99
) +
1
;
07
WriteLn(
'My number is between 1 and 100'
);
08
repeat
09
WriteLn(
'Guess what my number is:'
);
10
ReadLn(Guess);
11
until
;
12
WriteLn(
'You guessed it! My number was '
, Number);
13
end
.
- The program must tell the user if their guess is lower or higher than the computer's number
- The repeat block must end when the number and the guess match
- The program must count the number of guesses the user made and print that number before the program ends
- The source code file name must be course101-day004.pas