Pass Task 3.1: Name Tester
Overview
Control flow enables you to easily add conditions and loops to your programs. In this task you will create a small program that uses conditions and loops to output custom messages to users.
Purpose: Learn to use the control flow statements within a program.
Task: Create a program that tests a user's name and echoes a custom message.
Instructions
Create a small program that will check the user's name and respond with different messages for different people.
1. Download and extract the resources for this task.
2. Open NameTester.pas in Sublime Text.
3. Implement a Main procedure with the following logic:
- It reads a name from the user, and displays back a message.
- Check if the name entered is your name.
- If the name is your name, output the message 'Awesome name!'
- Otherwise output the silly name message
Procedure: Main
----------------
Uses: TerminalUserInput
---------------------------------
Variables:
- name (which stores a String value)
---------------------------------
Steps:
1: Assign name, the result of calling ReadString with the prompt: 'Please enter your name: '
2: if name is '-add your name here-' then
3: Output 'Awesome name!'
4:else
5: Output name, ' is a silly name'
4. Open a Terminal window and compile and run your program:
- Change into the directory with your code using the cd command
- Compile your program using fpc -S2 NameTester.pas
- Run your program using ./NameTester
5. One message for everyone isn't that much fun. Enhance your Silly Name Tester so that it has custom messages for at least one other person.
Saying that the name is 'silly' will have a much greater effect if we add lots of 'silly's to the out- put... Add a Output Silly Name procedure to your Name Test program. This will output the person's name and ' is a silly silly' ... with 100 'silly's, then ' name'.
Call this new procedure from main when the name is not your name. The pseudocode for this procedure follows:
Procedure: OutputSillyName
---------------------------------
Parameter:
- name (the silly name String) Local Variables:
- i (an integer, used to count the number of loops)
---------------------------------
Steps:
1: Make i equal 0
2: Output (staying on the same line) name, ' is a'
3: While i is less than 100
4: Output (on the same line) ' silly'
5: Increment i (make i equal i + 1)
6: Output ' name!' (moving to a new line)
Questions:
Assume that age has the value 5. List the actions the computer executes when it runs the fol- lowing code.
WriteLn('Message 0');
if age < 10 then begin
WriteLn('Message 1');
end else begin
WriteLn('Message 2');
end;
WriteLn('Message 3');
Assume that age has the value 11. List the actions the computer executes when it runs the fol- lowing code.
WriteLn('Message 0');
if age < 10 then begin
WriteLn('Message 1');
end else begin
WriteLn('Message 2');
end;
WriteLn('Message 3');
Assume that age has the value 7. List the actions the computer executes when it runs the fol- lowing code.
WriteLn('Message 0');
while age < 10 do begin
WriteLn('Message 1'); age := age + 1;
end;
WriteLn('Message 2');
Assume that age has the value
11. List the actions the computer executes when it runs the fol- lowing code.
WriteLn('Message 0');
while age < 10 do begin
WriteLn('Message 1'); age := age + 1;
end;
WriteLn('Message 2');
Assume that age has the value 8. List the actions the computer executes when it runs the fol- lowing code.
WriteLn('Message 0');
while age < 10 do begin
age := age + 2; WriteLn('Message 1'); age := age - 1;
end;
WriteLn('Message 2');