Hacking - The Art of Exploitation, 2nd Edition

(Romina) #1
Programming 11

{


Drive straight for 1 mile;
Add 1 to the counter;
}

The C-like pseudo-code syntax of a for loop makes this even more
apparent:

For (i=0; i<5; i++)
Drive straight for 1 mile;

In this case, the counter is called i, and the for statement is broken up
into three sections, separated by semicolons. The first section declares the
counter and sets it to its initial value, in this case 0. The second section is like
a while statement using the counter: While the counter meets this condition,
keep looping. The third and final section describes what action should be
taken on the counter during each iteration. In this case, i++ is a shorthand
way of saying, Add 1 to the counter called i.
Using all of the control structures, the driving directions from page 6
can be converted into a C-like pseudo-code that looks something like this:

Begin going East on Main Street;
While (there is not a church on the right)
Drive down Main Street;
If (street is blocked)
{
Turn right on 15th Street;
Turn left on Pine Street;
Turn right on 16th Street;
}
Else
Turn right on 16th Street;
Turn left on Destination Road;
For (i=0; i<5; i++)
Drive straight for 1 mile;
Stop at 743 Destination Road;

0x240 More Fundamental Programming Concepts


In the following sections, more universal programming concepts will be
introduced. These concepts are used in many programming languages, with
a few syntactical differences. As I introduce these concepts, I will integrate
them into pseudo-code examples using C-like syntax. By the end, the pseudo-
code should look very similar to C code.

0x241 Variables


The counter used in the for loop is actually a type of variable. A variable can
simply be thought of as an object that holds data that can be changed—
hence the name. There are also variables that don’t change, which are aptly
Free download pdf