And now, what about value assignment to arrays?
We can choose to put everyone manually like single variables.
If MyArray is declared as array of integer then correctly we write MyArray[x] := 12; .
[Notice that only MyArrays is the same for MyArray ]
Good, we know the correspondece between array elements and single variable about assignment.
But always try to see if you can automize operations: code is made for this too.
Very basic example: you want to assign 9 values whose arguments change according to a progression.
We use powerful structure to automize these steps: we're going to use loops. They are three:
- For-do loop: progressive up or down, is used when you know the nember of steps required. Once running it goes till the end, It cannot be stopped by internal code;
- While-do loop: this tests an initial condition, which if is true then makes the loop begin, and iterate until it remains true. It can be stopped internally putting a way to change condition value to false, without which the loop becomes infinite;
- Repeat-until loop: same as while-do but with the difference that the inner code is executed at least once, after that a condition is tested to check if to run loop again or to stop it.
// for-do loop; index i goes from 1 to 9 for i:=1 to 9 do begin Players[i]:='Player number' + inttostr(i); end; // while-do loop i:=1; // numbers > 0 are treated as true values in tests while (i) do begin Players[i]:='Player number' + inttostr(i); // Here we manage condition to avoid infinite loop if i=9 then i := 0 // no need of semicolon else i := i+1; end; // repeat-until loop i:=1; repeat Players[i]:='Player number' + inttostr(i); if i=9 then i := 0 else i := i+1 // no need of semicolon until (i); // numbers > 0 are treated as true values in tests
These examples aim to get the same result: populating an array of strings with the values that change progressively.
With loop finished we have Players containing the string 'Player number 5'.
We need a little digression on permitted chars in strings. To assign a string value to a string variable you need to delimit chars between single quotes.
'this is @ 123 try-of-? yes' has no sense for us, but it is always a string. Can a single quote appear inside a string?
Yes, but we need an escape mode to let the compiler catch what we want: in this case, after we've opened a string with the first single quote, we write two contiguous ones. That is:
hw2sw: string; ... ... hw2sw := 'hw2sw site''s cool!';
Other ways exist but this one is quick.
Strings can be concateneted.
S1, S2: string; ... S1 := 'from '; S2 := ' software'; ... S1 := S1 + 'hardware to' + S2; // S1 now is --> from hardware to software