Home Projects Basic Requirements Class Notes |
l | Class Notes | ||
| � | Loopings There are three types of Loopings, for loops, repeat...until loops, and while loops. Loops are used for repeating a set of commands couple of times or even create a trap. Here are the three kind : for loops for index := 1 to 20 do {This creates a loop that will be repeated 20 times} begin writeln('Hello World'); end; For the for loops you must always have a index variable that is an integer! while loops while x='Z' do {Checks if x is equal to Z} begin writeln('Z is equal to x!'); end; For the while loops you use conditional testing meaning, if true do this. repeat...until loops repeat {This will repeat the command within repeat and until,until conditional statement is true} writeln('x is not equal to 3'); until x=3; The repeat...until loop is used for repeating command until the conditional statement is true For the repeat...until loop you can also trap a person within a loop by doing the following : repeat...until loop with a trap repeat write('Enter the letter b')' readln(b); until b in ['b','B']; {this traps the person to enter one of the two letter to continue} Randomize In pascal you can actually have random numbers, you simply use the randomize function. To use the randomize function you must fist initialize the random number generator, simply enter randomize before you want to use the function. You only need to initialize it once within the program! Then when you want a random number you use random(number), the number is the the highest range number. here is an example : randomize function begin randomize; repeat A:=random(55) +1; {will generate random numbers between 1 and 54.999} writeln(A); until A=55; If you've notice I have a plus 1 after the generated number, because the computer will generated a random number between 1 and 54.99, and truncates the every thing after the decimal point. So keep that in mind Writeln The writeln function writes the text within the single quotes. writeln function begin writeln('Hello world'); end. |
_ | ||
| _ | _ | 07/10/01 |