📄 pascal.txt
字号:
You can do the same thing with "<" and "<=".
You can as well check if the values aren't equal: "<>". This will come alone without any
">" or "<".
Ok, that was one condition "if"... Now the multi condition "if":
Let say:
a:=45;
d:=32;
if ((a>d)and(77-d=a)) then
...
What we did here is: "a" getting the value of 45, and "d" will get 32.
The "if" will check if the value in "a" is bigger than the value in "d",
AND 77-(the value of "d")=(the value of "a", meaning 45) .
In this case the if will be right!
The AND in the middle of the "if" mean that the condition in the first ( ) and the second ( )
must be right too!!!
There can be an OR instead of AND...
if ((a>d)or(77-d=a)) then
The OR means that you need that only one condition to be right, so the "if" will be activated!
And in the AND you need that both of the conditions to be correct, so the "if" will be activated!
What we did for now was a SIMPLE "if". Now will come not the hard part of the "if", but not as
simples as the "if" we did before, no we'll add an ELSE to the "if".
The idea of the "ELSE" is: When the condition of the if won't activated, the program will
continue to the else part, but if the "if" is activated, the programs will jump over the "ELSE"
part, and continue the program like nothing happen!
The trick of the "ELSE" is: that the sign ; will never come before the "ELSE"...
Look:
if (2=9) then
writeln('I the man') { you see... no ; before the "ELSE" }
{ the unsigned last command before the "ELSE" includes the end;
so instead of end; it will be end }
else
writeln('The DP is cool!');
In this case the program will see than 2 isn't equal to 9, and will jump to the "ELSE" part,
and print on the screen: The DP is cool!
(and it will get down one line, remeber... writeln is one line down, and write is stay on the
same line! )
Check out this full program with if and else examples:
program the_example;
var
a, b, c: integer;
begin { the main begin }
a:=1;
b:=2;
c:=3;
if ((b-a=a)and(b+a=c)) then { this will check if the condition of 2-1=1, and the condition of
2+1=3 are true, if so then the variable 'a' will receive zero into itself}
a:=0
else { or else, if even ONE of the contiosions is wrong/false then the variables 'b' and 'c', will
receive zero, each and every one of them ('b' and 'c') }
b:=0;
c:=0;
end. { the main end }
1.6) Case:
**********
First thing you need to know about the "CASE" is that it kind of like "if".
But it's most like the Simple type of "if", with "ELSE".
Take this small program for example:
program case_example;
var
a: integer;
begin { the main begin }
readln(a); { an input by the user to the variable 'a' }
case (a) of
1: writeln('you pressed 1');
2: writeln('you pressed 2');
3: writeln('you pressed 3');
end;
end. { the main end }
The "CASE" is on the variable "a", the "CASE" will check if the value in "a" is one of the list's
under the "CASE", the number or the char before the : is the value you like to check for,
And the line after the : is the command you like to activate if the value is correct!
( NOTE: you CAN'T use more than one command here after the : ).
And when you like to close the "Case", you must put end; but NO begin after/under the
"CASE ( { variable you like to check } ) OF" !!!
1.7) Consts:
************
Sometimes in your program you need some kind of number for constant use!!!
Let say you want to check some variables if they're equal to: 3741 .
So instead of every time writing the number (3741), you can declare the number in a special
variable. Who to do it?! you ask, no problem!
If you like to do such a thing, you must do it after the "PROGRAM", and before the "VAR"!!!
After writing "CONST".
Look at this small example:
program const_example;
const
m = 3741; { we declared "m" as a const variable, you can't change it! }
{ the const variable can be change, and can use as how many as you like!!! }
var
a, c: integer; { just a regular global variables of the type of integer! }
begin { the main begin }
a:=7315;
c:=m;
if (a=m)
writeln('A is cool!') { and here there is no ; in the end, because the next command will be "ELSE"! }
else { you see, we didn't use another begin here, because the "if" comes right after the "ELSE",
like one command, and after the "if" the same thing... only one command!}
if (c=m)
writeln('C is so cool, when I stucked a big Cock to every girl in the school.');
end. { the main end }
1.8) Random numbers:
************************
Sometimes if you need to use a random number. Why you ask?!
Well, lets take a small example of a card game, evry time you will take a card the program must
use some kind of number, right?! But what number should the program use... You need to tell the
program in what range of number you need the program to choose (the program isn't the one who
takes those number, but the PC timer is. You see all the program is does is to take a number
from the running time of the computer. It's a lame example of who the "RANDOM" number idea
works, but it's all you need to know about it!)
OK, there are three types of "RANDOM" number.
1. A random number which it's range will be from 0 to 1, so the variable who'll get the result
of the random into itself, will be a "REAL" type of variable.
For example, the number which may come up when you'll do the next:
b:=random; { remember that "b" is a "REAL" type of variable!!! }
and the numbers could be in the variable "b", after this command are: 0.146... , 0.164...
and more.
2. Another type of Random is where YOU decide, from what range the program will take a random.
Lets say you like a random number from 4 to 9, ok?!
Look:
a:=random(9+1)+4; { the variable "a", is an integer type! }
What we did here was, we added to the number in the ( ) 1 -> 9+1
(you can write 10 it's just the same, but it's no mistake to keep using 9+1),
because the program will automatically sub from the number in the ( ) 1 .
So, if we haven't put +1 after the 9, then the range of the random numbers would be 4 to 8,
got it!? GOOD!
Now, after the ( ) we put +4, so the program would know from where to start looking for
a random number. If you will put -4 after the ( ) you will get the range from -4 to 9 !
NOTE: The only PROBLEM with this way of 'ranging the random numbers', is that every time
you will run your program the rundom number will be every time the same one!!! (BUMMER)
3. The threes and the last way will also let you range a random numbers,
but this way WILL SOLVE THE BUMMER problem of the last way!!!
All YOU need to do is to use the LAST way, BUT, you must put: "RANDOMIZE" after the
main "BEGIN"!!! So after you placed the "RANDOMIZE" under the main "BEGIN". So each time the
will program runs, the random number will be different,
(but won't get out of the specified range)!
Take a look at this random example, in a full program:
program random_example;
begin { the main begin }
writeln('the random number between 0 to 1, is: ',random);
writeln('and the random number between 35 to 150, is: ',random(115)+35); { 150-35=115 }
end. { the main end }
1.9) GoToXY:
*************
Sometimes you like to play with location of the spot of where the next line will be written next,
So, how do you do it, simply! With command GoToXY which you use inside your code.
Look at the next example:
gotoxy(3,4); { you can always put variables instead of values, but the values and variables
MUST be in an integer type form! }
after this small example of the command, the spot on the screen will be 3 places from the
top left side of the screen, and 4 places under the it!
It's just like in math the first value is the X and the second one is the Y!
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
2) Loops.
2.1) For...to...do:
******************
Sometimes in your program you need to do some thing a few times, this thing called a loop!
The first type of the loop, and the most simple one if "FOR" loop.
In this loop you have to know 'how many time you want to do a specific thing?!'.
Lets take the most simple example ever, printing on the screen 10 times the word 'cool':
program loop_cool;
var
i: integer; { here we declared an index for the loop! the name of the variable can be
every name you like, just like a simple variable, because it's a simple variable }
begin { Main Begin }
for i:=1 to 10 do { This loop is for 1 (including 1) to 10 (including 10) }
writeln('cool'); { Because we have here only one command after the "FOR", you won't need the
"BEGIN" under it! But if you had more than one command you like to be
in the loop, you MUST put the "BEGIN" under the "FOR", and "END;" under
the last command you like the loop to do! }
end. { Main End }
The main idea of this loop is that it will reapeat it self from the first command inside the
loop (in this case is: "WRITELN('cool');"), to the last one for how many time as you wish
(in this case the number of times is 10).
At the end of this loop the value inside of the variable "i" will be 10.
You can do the loop upside down, by placing "DOWNTO" between the 1 and 10 instead of just "TO"!
So at the end of this loop with the "DOWNTO" instead of "TO", the value of the variable "i" will
be 1, you will see why is pascal own this "DOWNTO" thing after you will learn the Arrays!
2.2) While...do:
****************
The last loop was the "FOR" loop, we knew the exact times we like to do the loop, but sometime
you just can't know the it while programming the program... so you need to let the program to
decide of how many time the loop will run. We'll use now a loop which will check the given
condition(s) (the condition(s) you will give the loop, just like the "if") every time it'll
repeat itself. The loop called: "While", and this is how this loop's structure looks like:WHILE ( here will come the condition you like to check ) DO
The condition will look like just like the "if"'s condition!
The idea of the one or more commands after if just like the "FOR" loop.
The only thing you need to do so the loop won't be endless... You need that the condition in
some place to be UNcorect! So lets take this small example of the "While" loop:
In this example the idea is to print the line "DP is cool" for 9 times!
Look:
program while_loop_example;
var
i: integer;
begin { the main begin }
i:=0; { we placed 0 in the variable "i", so we could INCrease the variable by one every
time the loop will be repeated! }
While (i<9) do
begin { the begin of the loop }
writeln('DP is cool!'); { this is the line we need to print on the screen! }
inc(i); { here we Increasing the variable "i" by 1, it's the same if you'll do "i:=i+1;" }
{ NOTE: there is also another command like "INC", but upside down: "DEC" is just
like: "i:=i-1;" GOT IT?! }
end; { the end of the loop }
{ When the "i" won't be less than 9, the loop will be stoped! }
{ and the program will continue the program after the end; (in this case) }
{ at the end of the loop the value in "i" will be 9. }
end. { the main end }
the way, if we place before the "WHILE" loop, into the variable "i" the value 9 or more...
The program will just check if the condition is correct, if it's than it will go into the loop...
And if the value of i (here) is makes the condition incorrect, that it won't even go into the
loop (inside the loop), and value of "i" won't change, the program is actually will jump over
the loop, and continue the program like nothing happen!!!
I hope this's clear to you! Like it's to me! ;)
2.3) Repeat...until:
*******************
Like I said in the "WHILE" loop if the condition isn't correct, then it won't even execute the
inside of the loop even one time... But sometimes we need to do the loop at list once.
OK... let start the explanation on this most simple loop... All you need to remember is that
this Repeat loop is the opposite of the "While" loop. In "While" loop you checked the condition
first, and then entered the loop, but in "Repeat" case no matter what the condition is, the
inside of the loop will be done at list once. The "Repeat" loop will be stoped, ONLY when the
condition is correct, check out this simple example for the "Repeat" loop...
Look:
program repeat_loop_example;
var
i: integer;
begin { the main begin }
repeat { Well, we may have more than one command inside the loop, but the "Repeat" itself
is like the "Begin" in other loops, and the "Until" in this loop acts like the "End;" in the
others! }
writeln('Enter some kind of number value...'); { the line we want to print on the screen! }
readln(i); { Here you need to make an input to the variable "i", the "i" could be in a "REAL"
or an "INTEGER" type form (this means you CAN'T enter a letter or any other sign
from the keyboard, ONLY numbers!!!) when you'll enter the number you wish just
press ENTER to enter it into the variable "i". }
until (i=0); { The loop will repeat itself until the value in the variable "i" will be 0 }
end. { the main end }
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -