RSS | Roulette Scripter Studio | Lesson 6 | For, Repeat ... Until, and While (all about loops)
     
This tutorial was made by Silver
http://www.money-maker-machine.com/forum/profile/Silver/
 
Lesson 6. For, Repeat ... Until, and While (all about loops).
 
0. Loops
Loops are used when you want to repeat code a lot of times. For example, if you wanted to print "Hello" on the screen 10 times you would need 10 print commands. You could do the same thing by putting 1 print command inside a loop which repeats itself 10 times.

There are 3 types of loops which are the for loop, while loop and repeat until loop.

I.
FOR loop


The for loop uses a loop counter variable, which it adds 1 to each time, to loop from a first number to a last number.
The general form is:
for index := StartingLow to EndingHigh do statement;

Ex.6.1: The following example will print the text Hello 10 times:
var k;
begin
  for k:=1 to 10 do print('Hello !');
end;


If you want to have more than 1 command inside a loop then you must put them between a begin and an end.

Ex.6.2: The following example make 500 spins, and after each zero landed will bet on High:
var k, landed_number;
begin
    set_roulette_window_name('European Roulette - onlineCasino.com');
    for k:=1 to 500 do begin
      click_spin();
      landed_number:=get_last_number();
      if landed_number=0 then begin
        print('Landed number is Zero');
        print('Will bet chip nr.2 on High');
        click_chip2();
        click_high();
      end;
    end;
end;


In the for-to-do loop, the starting value MUST be lower than the ending value, or the loop will never execute! If you want to count down, you should use the for-downto-do loop:
for index:=StartingHigh downto EndingLow do statement;

Ex.6.3: The following example will print the value of variable K from High to Low order:
var k;
begin
  for k:=10 downto 1 do print(k);
end;


II.
While loop


The while loop repeats while a condition is true. The condition is tested at the top of the loop and not at any time while the loop is running as the name suggests. A while loop does not need a loop variable but if you want to use one then you must initialize its value before entering the loop.

The pretest loop has the following format:
while BooleanExpression do statement;

The loop continues to execute until the Boolean expression becomes FALSE. In the body of the loop, you must somehow affect the Boolean expression by changing one of the variables used in it. Otherwise, an infinite loop will result:
Example of infinite loop:
a := 5;
while a < 6 do
  print (a);


Remedy this situation by changing the variable's value:
a := 5;
while a < 6 do
  begin
    print (a);
    a := a + 1
  end;


The WHILE ... DO lop is called a pretest loop because the condition is tested before the body of the loop executes. So if the condition starts out as FALSE, the body of the while loop never executes.

Ex.6.4: The following example make free spins till 0 lands, and in this case will bet chip2 on Low.
var landed_number;
begin
    set_roulette_window_name('European Roulette - onlineCasino.com');
    landed_number:=-1;
    while landed_number<>0 do begin
      click_spin();
      landed_number:=get_last_number();
    end;
    print('Landed number = 0.');
    print('Will bet chip nr.2 on Low');
    click_chip2();
    click_low();
    click_spin();
end;


III.
Repeat loop


The repeat until loop is like the while loop except that it tests the condition at the bottom of the loop. It also doesn't have to have a begin and an end if it has more than one command inside it.

The posttest loop has the following format:
    repeat
        statement1;
        statement2
      until BooleanExpression;


  In a repeat loop, compound statements are built-in -- you don't need to use begin-end. Also, the loop continues until the Boolean expression is TRUE, whereas the while loop continues until the Boolean expression is FALSE.

This loop is called a posttest loop because the condition is tested after the body of the loop executes. The REPEAT loop is useful when you want the loop to execute at least once, no matter what the starting value of the Boolean expression is.

Ex.6.5: The following example make free spins till 0 lands, and in this case will bet chip2 on Low.
var landed_number;
begin
    set_roulette_window_name('European Roulette - onlineCasino.com');
    repeat
      click_spin();
      landed_number:=get_last_number();
    until landed_number=0;
    print('Landed number = 0.');
    print('Will bet chip nr.2 on Low');
    click_chip2();
    click_low();
    click_spin();
end;


IV.
Break and Continue


The Break command will exit a loop at any time. The following script will print only first 5 values and will  exits the loop.
var k;
begin
    k:=0;
    repeat
      k := k + 1;
      print('Hello!');
      if K>5 then Break;
      print(k);
    until K=10;
end;


The Continue command will jump back to the top of a loop. This example will print 10 times Hello and only 5 times the value of K.
var k;
begin
    k:=0;
    repeat
      k:=k+1;
      print('Hello!');
      if K>5 then Continue;
      print(k);
    until k=10;
end;
 
Copyright © 2007-2011, All rights reserved to Money Maker Machine