| A basic explaination of conditions |
|
The If..Then..Else Statement This compares values and then from the result allows further code to be executed or if the first statement is not true then it can move to the else statement and run the code after that. for example: if value1 = value2 then label1.caption := 'they are the same'
else label1.caption := 'they are not the same';
So if [value1] and [value2] are the same then the labels caption states that [they are the same]. However if they are not the same the else statement which then changes the labels caption to state that [they are not the same] .
Notice that the [;] is not used until the very end of the whole condition. However if there were more lines between the [if] and the [else] then each line would end with the [;] but there would be an [end] condition without the [;] so that the [else] statement afterwards is used. For Example:
if value1 = value2 then The While..Do..End Statement This conditional loop is very powerful and flexible. The [while] loop allows for the use of a condition very much like the [if] statement however whereas an [if ] statement runs the lines of code once, a [while] loop runs the lines of code until the condition became true.
Also the values involved that are part of the condition need to be initialized (meaning to be cleared or reset) before the [while] loop starts. Here follows an example of a [while] loop:
value1 := 0;
So this [while] loop counts up to ten from zero and outputs the count to the labels caption so it can be displayed on screen. A [while] loop is generally used when the start and end stage of the loop could be unknown. This is why the increase made in the loop is within the code as part of the [while] loop. The [inc] command, in this case, is increasing [value1] each time the loop cycles around. The above example is ideal for the [while] loop as there is a chance that [value2] could change whilst it loops.
for value1 := 1 to 10 do
Also, unlike the [while] loop, the loops increments are done automatically. This kind of loop is fine as long as the range is known. |