Lazarus – Case of – Simple Possibilities

 


Lazarus is an excellent solution for all Delphi and Pascal-like programming enthusiasts. But it's also designed for Windows-oriented solutions. Pascal-like programs include Free Pascal and Turbo Pascal, for example. And this is a continuation of the selection operator. A convenient and frequently used solution for iterating over values. How else can it be used? This is entry-level. In a simple way.


When I was learning this myself, I saw an interesting solution on a website dedicated to Lazarus. It's iterating over text values. The data type is a string. And we can iterate directly over them. It's very convenient! And to avoid worrying about upper and lower case, you can first convert the string being selected to lower case, so that all the letters are lower case. And then check against the values ​​written in lower case.

Here's a great illustration. This is selecting an operating system. And output the result to the Memo element.

 

Os:='Windows';

case LowerCase(Os) of

'dos': Memo1.Append('dos operating system is selected!');

'windows': Memo1.Append('windows operating system is selected!');

'mac os': Memo1.Append('mac os operating system is selected!');

'unix': Memo1.Append('unix operating system is selected!');

else

Memo1.Append('unknown operating system is selected!');

end;

 

It's great that it's a selection operator. It works with enumerations too. Enumerations are convenient as constants. Pre-selected values. A list of possible values. Like seasons. There are autumn, winter, spring, and summer. Or a medal. Bronze, silver, and gold. It's convenient to create a selection operator.

 

type

EMedal=(Gold, Silver, Bronze);

 

MyMedal:= EMedal.Bronze;

case MyMedal of

Silver: Memo1.Append('You got a SILVER medal!');

Bronze: Memo1.Append('You got a BRONZE medal!');

Gold: Memo1.Append('You got a GOLD medal!');

else Memo1.Append('You got NO MEDAL at all!');

end;

 


It's also interesting that the selection operator can also work with hexadecimal numbers. These are tricky numbers. Where the counting doesn't go from 0-9, but from 0-15. Where after 0-9 come Latin letters – A B C D E F. To add letters to the count up to 15, they are written with the $ symbol.

 

A:=$A;

case A of

$0: Memo1.Append('Hex 0');

$1..$5: Memo1.Append('Hex [1..5]');

$6: Memo1.Append('Hex is 6');

$7: Memo1.Append('Hex is 7');

$8..$9: Memo1.Append('Hex is [8..19]');

$A..$F: Memo1.Append('Hex is [A..F]');

else Memo1.Append('Hex out of range [0..F]');

end;

 



And finally. This is a combination of different types of conditions. In a line, as a range. And you can write it this way too. It's all very flexible. You can write a simple condition on one line. Or spread it out over several lines. You can write many different choices. For example, a range and specific values ​​right on one choice line. And of course, the else branch. You can write begin.end there for order. But it's not necessary. You can just use the else clause and a bunch of code that goes in this branch.

 

A:=15;

case A of

1..10,14,15,16: Memo1.Append('[1-10],14,15,16!');

21..30: Memo1.Append('[21-30]!');

else

Memo1.Append('Out of range [1-30]!');

Memo1.Append('your A is' + IntToStr(A));

end;

 

The selection operator is very useful. And it can do a lot. You can select a lot of things! And write a wide variety of conditions. I was just studying it myself. What can be written there? I was exploring some of the possibilities. So that I could actively use it in my own code.



Dima Link is making retro videogames, apps, a little of music, write stories, and some retro more.


WEBSITE: http://www.dimalink.tv-games.ru/home_eng.html 

ITCHIO: https://dimalink.itch.io/


TUMBLR: https://dimalink.tumblr.com/

BLOGGER: https://dimalinkeng.blogspot.com/

MASTODON: https://mastodon.social/@DimaLink

Comments