program security;uses crt;
var
 input : String;
begin
clrscr;
 writeln('Enter the password');
 readln(input);
if (input = 'Pascal') then
 writeln('Pascal is easy!')
{Note no semicolon for one line}
else if (input = 'Basic') then
begin
 writeln('Basic is not');
 writeln('very hard!');
end
else
 writeln('Wrong password!');
end.
This first prompts the user to enter a password and then reads that into intput.
First it checks to see if input is 'Pascal'. Note that this is case sensitive.
If it is 'Pascal' then it writes 'Pascal is easy!' and goes to the end of the if
statement (which happens to be the end of the program) otherwise it checks the
next condition. Is it 'Basic'? if it is then write 'Basic is not very hard!'
(over two lines).If it is not then try the next condition. The next condition is
ELSE. The code in the ELSE part of the if statement is executed if none of the
other conditions in the if statement are met.
Another flow control command, which is actually considered bad practice to
use, but is quite useful in some situations is goto. To use this declare
a label in the label section and use it in your code. Then just say :
goto label;
eg:
label label1;
begin
 .
 .
 .
 label1:
{Note the colon!}
 .
 .
 .
goto label1;
{Note no colon}
 .
 .
 .
end.
Easy!
You might have noticed that sometimes the if statement may get a
little cumbersome to use. ie:
if i = 0 then
else if i = 1 then
else if i = 2 then
And so on. This is really cumbersome and annoying (you have to type the same
thing over and over). So what you want to use is the case statement. The
above example would be done like so:
case i of
0:...
1:...
2:...
3:...
4:...
5:...
6:...
end;
|
Very handy. If you want to have multiple lines of code for one of the options,
then you must put the multiple lines between begin and end. If you
try to use the case statement with a String type (or any type that isn't a
char or integer) then Pascal will give you an error. You can only use
ordinal
types with the case statment.
Another useful set of commands are the 'EXIT' commands. These are:
Halt
This does the simple task of ending your program.
Exit
This command exits from the current procedure/function.
Break
This command exits from the current loop.
|