1 2 3 4
|
for (unsigned n = 0; n < message.length(); n++)
{
//Some Code here
}
|
The
for
loop allows you to repeat a secion of code multiple times.
In C and C++ it's a lot more flexible than most other languages, and in this example includes;
The declaration and initialisation of a local unsigned integer n
(unsigned n = 0;
,
A check to loop again if the value of n is less than the length of the message
n < message.length();
And finally a increment on n every time round the loop
n++)
Inside the body of the for loop (
//Some Code here
) n is used to control the behaviour.
message[ n ] = LUT[ c -'a' ];
message is a
string, which can by treated as if it were an array of characters to access individual characters in the string, so
message[ n ]
is the n
th character in the string.
EG message = "Hello", message[1] = 'e' (starts at index 0 for first character).
LUT is also an array, so can be treated in the same way.
LUT[ c -'a' ];
is a bit of C or C++ 'trickery'.
c is a char, which is a 1 byte integer that can be treated as if it represents a character OR as a number.
c-'a'
makes the computer do both! Say c = 'e', then because characters are encoded using the ASCII chart, the computer translates 'e' into 101, and 'a' into 97, subtracts 97 from 101 to get 4.
So if c = 'e', LUT[ c - 'a'] => LUT['e' - 'a'] => LUT[101-97] => LUT[4] ='t'
(Pause for breath...)
It does all start to make sense after a while, honest:-)
If you haven't done so already it's well worth taking the time to go through the tutorial on this site (
http://www.cplusplus.com/doc/tutorial/) it covers most of what you need to create a wide range of programs in a fairly easy to understand manner.
Hope the above helps.