Paypal donation


Learn Object Pascal

Part 5 - Decisions and Loops

We have seen the arithmetic operators and the boolean operators in the part 4 of our series of tutorials.
Let's see how we can use these operators in order to branch sequential code.
I will post simple examples for each case.

if ... then statement:
Notice that the age is a simple integer variable.
  age:=10;
  if age<12 then begin
     Memo1.Lines.Add('Age: ' + IntToStr(age) + ': You are a child !');
  end; 
Another example (age and age_limit are variables of type integer) :
  age:=10;
  age_limit:=15;
  if age<age_limit then begin
     Memo1.Lines.Add('Age: ' + IntToStr(age) + ': You are a child !');
  end; 
if ... then ... else statement:
  age:=20;
  if age<18 then begin
     Memo1.Lines.Add('Age: ' + IntToStr(age) + ': You are a child !');
  end else begin
      Memo1.Lines.Add('Age: ' + IntToStr(age) + ': You are adult !');
  end; 


We can combine and, or logical operators like the examples below :
 age:=25;
  height_in_cm:=150;
  if (age>20) and (height_in_cm>140) then begin
     Memo1.Lines.Add('Age: ' + IntToStr(age) + 
	             ' ,Height(cm): ' + IntToStr(height_in_cm) + 
		     ': You are big person !');
  end else begin
      Memo1.Lines.Add('Age: ' + IntToStr(age) + 
	              ' ,Height(cm): ' + IntToStr(height_in_cm) + 
		      ': You are a small person !');
  end;     
 age:=15;
  height_in_cm:=150;
  if (age<20) or (height_in_cm<140)  then begin
     Memo1.Lines.Add('Age: ' + IntToStr(age) + 
	             ' ,Height(cm): ' + IntToStr(height_in_cm) + 
		     ': You are a small person !');
  end else begin
      Memo1.Lines.Add('Age: ' + IntToStr(age) + 
	              ' ,Height(cm): ' + IntToStr(height_in_cm) + 
		      ': You are a big person !');
  end; 
if ... then ... else if .... else statements:
 age:=21;
  if (age>=0) and (age<=10) then begin
     Memo1.Lines.Add('Age: ' + IntToStr(age) + ': You are between 0-10 years old');
  end else if (age>10) and (age<=20) then begin
      Memo1.Lines.Add('Age: ' + IntToStr(age) + ': You are between 11-20 years old');
  end else if (age>20) and (age<=30) then begin
      Memo1.Lines.Add('Age: ' + IntToStr(age) + ': You are between 21-30 years old');
  end else if (age>30) and (age<=40) then begin
      Memo1.Lines.Add('Age: ' + IntToStr(age) + ': You are between 31-40 years old');
  end else begin
      Memo1.Lines.Add('Age: ' + IntToStr(age) + ': You are 41+ years old');
  end;
nested if statements:
Notice that age and height_in_cm are integer variables
  age:=21;
  height_in_cm:=200;

  if age>18 then begin

     if height_in_cm<=179 then begin
        Memo1.Lines.Add('Age: ' + IntToStr(age) + 
		        ' ,Height(cm): ' + IntToStr(height_in_cm) + 
			': You are adult and short');
     end else begin // you are taller than 179cm
        Memo1.Lines.Add('Age: ' + IntToStr(age) + 
		        ' ,Height(cm): ' + IntToStr(height_in_cm) + 
			': You are adult and tall')
     end;

  end else begin //age<=18

      Memo1.Lines.Add('Age: ' + IntToStr(age) + ': You are not adult');

  end;        
case ... else statement:
The case statement compares a variable against a single constant value or against multiple values (list) or against a subrange of values.
Case is more efficient, and easier to maintain than multiple if ... else if ... nestings.
Below we can see a case using integers.
  //case using integer
  age:=35;
  case age of

       //single value example
       20 : begin //Here you must use constants or 
	          //hard-coded values -- you cannot use variables.
          Memo1.Lines.Add('Age: ' + IntToStr(age) + ': You are exactly 20');
          Memo1.Lines.Add('-----------------------------------------');
       end;

       //single value example (another)
       21 : begin //Here you must use constants 
	          //or hard-coded values -- you cannot use variables.
          Memo1.Lines.Add('Age: ' + IntToStr(age) + ': You are exactly 21');
          Memo1.Lines.Add('-----------------------------------------');
       end;

       //multiple 'single' values example
       25,26,27 : begin //Here you must use constants or 
	                //hard-coded values -- you cannot use variables.
          Memo1.Lines.Add('Age: ' + IntToStr(age) + ': You are between 25 and 27');
          Memo1.Lines.Add('-----------------------------------------');
       end;

       //subrange value example
       30 .. 40 : begin //Here you must use constants or 
	                //hard-coded values -- you cannot use variables.
          Memo1.Lines.Add('Age: ' + IntToStr(age) + ': You are between 30 and 40');
          Memo1.Lines.Add('-----------------------------------------');
       end;

       //else
       else begin
          Memo1.Lines.Add('Age: ' + IntToStr(age) + ': Unknown age');
          Memo1.Lines.Add('*****************************************');
       end;
  end;  //case   


Let's see a char case (chr is a char variable)
  //case using char
  chr:='g';
  case chr of

       //single value example
       'a' : begin //Here you must use constants or 
	           //hard-coded values -- you cannot use variables.
          Memo1.Lines.Add('Char: ' + chr);
          Memo1.Lines.Add('--------------------A-------------------');
       end;

       //single value example (another)
       'b' : begin //Here you must use constants or 
	           //hard-coded values -- you cannot use variables.
          Memo1.Lines.Add('Char: ' + chr);
          Memo1.Lines.Add('-------------------B----------------------');
       end;

       //multiple 'single' values example
       'c','d','e' : begin //Here you must use constants or 
	                   //hard-coded values -- you cannot use variables.
          Memo1.Lines.Add('Char: ' + chr +  ': char is between c and e');
          Memo1.Lines.Add('-----------------------------------------');
       end;

       //subrange value example
       'f' .. 'j' : begin //Here you must use constants or 
	                  //hard-coded values -- you cannot use variables.
          Memo1.Lines.Add('Char: ' + chr +  ': char is between f and j');
          Memo1.Lines.Add('-----------------------------------------');
       end;

       //else
       else begin
          Memo1.Lines.Add(chr + ': Unknown character');
          Memo1.Lines.Add('*****************************************');
       end;
  end;  //case    
Notice : The case statement only supports ordinal types. So for example you can not use strings directly.
Let's see now how we can do looping.
Looping means repeating a "piece of code" over and over until some condition is met.

for ... to ... do loops.
For loops are fixed repetition so they only repeat a fixed number of times.
Use this type of loop when you can determine, beforehand, how many times the "code" to be repeated.
age , i and imax are integers.
This type of for increments i after each loop.
  age:=0;
  imax:=9;
  for i:=0 to imax do begin //i is increasing after each loop
      age:=age+1; //increase age
      Memo1.Lines.Add('For Counter i: ' + IntToStr(i) + 
	              ' Loop age: ' + IntToStr(age));
  end;
for ... downto ... do loops.
This type of loop decreases i after each loop.
  age:=0;
  imax:=9;
  for i:=imax downto 0 do begin //i is decreasing after each loop
      age:=age+1; //increase age
      Memo1.Lines.Add('For downto Counter i: ' + IntToStr(i) + 
	              ' Loop age: ' + IntToStr(age));
  end;  
for ... in ... do loop
In the example below we will loop character by character the str string.
counter is an integer that increases each time the parsed chr from str is equal to 's'.
We can say that counter counts the 's' chars in a given str string
 
  str:='this is a string';
  counter := 0;
  for chr in str do begin //loop char by char the hole string
      if chr='s' then begin
         counter := counter + 1;
      end;
  end;//for
  Memo1.Lines.Add('The string : "' + str +
                  '" contains: ' + IntToStr(counter) +
                  ' times the "s" character');  
Ths kind of loop we will be able to see it better in the future tutorials.
It is used in sets and in arrays which we will see them later.

While loops are considered "pretest" loops since they : initially test a Boolean expression it is TRUE then goes into the loop.
Use the while loop when you are unsure if you need to perform the inside loop task at all.

while ... do loop
  age:=0;
  while age < 6 do begin
        Memo1.Lines.Add('While loop age: ' + IntToStr(age));
        age:=age+1; //increase age
  end;
Repeat loops are "posttest" loops since the initially executes the loop and after each loop tests the Boolean expression. The loop is repeated if the expression is FALSE.
Use this kind of loop when you need to do the task at least once.

repeat ... until loop
  age:=0;
  repeat
     Memo1.Lines.Add('While loop age: ' + IntToStr(age));
     age:=age+1; //increase age
  until age = 3;
nested loops
Nested loops can be done using nested for / while and repeat loops.
In the example below we can see a nested for loop.
We are defining the variables:
year, year_max, month, month_max : integer;
  year_max:=3;  //three years
  month_max:=12; //twelve months
  for year:=1 to year_max do begin //for each year
      for month:=1 to month_max do begin  //for each month
          Memo1.Lines.Add('Year :' + IntToStr(year) + ' Month :' + IntToStr(month));
      end;
  end; 
Loops can be broken (break) under conditions and continue.
See the examples below :
  //break example
  age:=0;
  while age < 6 do begin
        Memo1.Lines.Add('While loop age: ' + IntToStr(age));
        age:=age+1; //increase age
        if age >=3 then begin
           break; //when the age reaches
           //3 years break the while loop
        end;
  end;
  //continue
  age:=0;
  while age < 6 do begin
        if age=3 then begin
           age:=age+1; //increase age
           continue; //this does not allow to print the age 3
        end;
        Memo1.Lines.Add('While loop age: ' + IntToStr(age));
        age:=age+1; //increase age
  end;  
The decisions and loops example can be downloaded from here.
In the next article we will talk about procedures and functions.

Part 1: Introduction - Part 2: Hello World - Part 3: Data types, constants and variables - Part 4: Operators - [[ Part 5: Decisions and Loops ]] - Part 6: Procedures and Functions - Part 7: Custom datatypes - Part 8: Enumerations, subranges and sets - Part 9: Records


Copyright © TrustFm.net 1998-2017 - Made by TrustFm - All Rights Reserved Worldwide