In writing the script program, may We always see and always follow the instructions or manuals. This is certainly not wrong, Because We then write a program That will from Easily read, Either by yourself or others. But there are Things That May not be described in the manual or reference, and this is usually obtained by learning from people WHO have long engaged in programming. Here Are Some tips related to writing boolean expressions (in Delphi / Pascal) WHO comes from Pascal Newsletter.
First Tips
Consider the Following example of writing a program line. If b is a boolean variable, then:
First Tips
Consider the Following example of writing a program line. If b is a boolean variable, then:
if a = x then
b : = True
else
b : = False;
b: = a = x;
Writing the code above was Used to take a look and cans be understood That if a value x then b nilanya true and if Otherwise, then b is false. Writing the code above Could have condensed into a line of code is shorter and Concise, namely:
b: = a = x;
Explanation:
Since a = x is an expression of comparison, and always produces a value of true or false, then the result cans We put in the variable b. So if the ratio a = x produces the value true, then the variable b will of be true automatically and Otherwise.
Second Tip
Based on the above tips first, then if We Want to write lines like the Following programs:
if a = x then
b: = False
else b: = True;
So in fact could easily be written in a line of code:
b: = a <> x;
Since a = x is an expression of comparison, and always produces a value of true or false, then the result cans We put in the variable b. So if the ratio a = x produces the value true, then the variable b will of be true automatically and Otherwise.
Second Tip
Based on the above tips first, then if We Want to write lines like the Following programs:
if a = x then
b: = False
else b: = True;
So in fact could easily be written in a line of code:
b: = a <> x;
Explanation:
The above code is similar to the first tips, only the result is reverse. So if a is not equal to x, then the value b = True. Actually it could also be written as follows:
b: = not (a = x)
But this seems to make the expression appear much more complex because it uses more than one operator (notes and =), and also a bit more difficult to read.
Third Tips
These tips may often we write in programming. Example there are lines of the following programs:
if b = True then c = '*';
So can we write more briefly with the following code:
But this seems to make the expression appear much more complex because it uses more than one operator (notes and =), and also a bit more difficult to read.
Third Tips
These tips may often we write in programming. Example there are lines of the following programs:
if b = True then c = '*';
So can we write more briefly with the following code:
if b then c = '*';
Explanation:
statement "if" has shown an evaluation if the condition is true, so we do not need to write down the comparison, since b is a boolean variable which is only true or false.
Fourth Tips
The following tips tips similar to third, just flip a statement. For example there is a line of code:
if b = False then c = '*';
Then it could be written more briefly as follows:
if not b then c = '*';
For those who are still beginning to learn programming, may be somewhat difficult to read and understand the above line of code, but if long accustomed to easily and more quickly in writing.
For those who are still beginning to learn programming, may be somewhat difficult to read and understand the above line of code, but if long accustomed to easily and more quickly in writing.
While writing the program code above provides advantages in terms of speed, and perhaps habits and beginner programmers who are experts, but all the writing in terms of programming there is nothing wrong and all give the correct result. In addition, the Delphi compiler is smart enough to generate identical machine code from all the above code. But at least it has given more understanding of the boolean expression. Hopefully useful.
0 comments on Delphi Coding Tips - Boolean Expressions :
Post a Comment and Don't Spam!
Dont Spam please