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 pseudocode
should look very similar to C code.
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
12 0x200
called constants. Returning to the driving example, the speed of the car would
be a variable, while the color of the car would be a constant. In pseudocode,
variables are simple abstract concepts, but in C (and in many other
languages), variables must be declared and given a type before they can be
used. This is because a C program will eventually be compiled into an executable
program. Like a cooking recipe that lists all the required ingredients
before giving the instructions, variable declarations allow you to make preparations
before getting into the meat of the program. Ultimately, all variables
are stored in memory somewhere, and their declarations allow the compiler
to organize this memory more efficiently. In the end though, despite all of
the variable type declarations, everything is all just memory.
In C, each variable is given a type that describes the information that is
meant to be stored in that variable. Some of the most common types are int
(integer values), float (decimal floating-point values), and char (single character
values). Variables are declared simply by using these keywords before
listing the variables, as you can see below.
int a, b;
float k;
char z;
The variables a and b are now defined as integers, k can accept floatingpoint
values (such as 3.14), and z is expected to hold a character value, like A
or w. Variables can be assigned values when they are declared or anytime
afterward, using the = operator.
int a = 13, b;
float k;
char z = 'A';
k = 3.14;
z = 'w';
b = a + 5;
After the following instructions are executed, the variable a will contain
the value of 13, k will contain the number 3.14, z will contain the character w,
and b will contain the value 18, since 13 plus 5 equals 18. Variables are simply
a way to remember values; however, with C, you must first declare each
variable’s type.
Arithmetic Operators
The statement b = a + 7 is an example of a very simple arithmetic operator.
In C, the following symbols are used for various arithmetic operations.
The first four operations should look familiar. Modulo reduction may
seem like a new concept, but it’s really just taking the remainder after division.
If a is 13, then 13 divided by 5 equals 2, with a remainder of 3, which
means that a % 5 = 3. Also, since the variables a and b are integers, the
P rogramming 13
statement b = a / 5 will result in the value of 2 being stored in b, since that’s
the integer portion of it. Floating-point variables must be used to retain the
more correct answer of 2.6.
To get a program to use these concepts, you must speak its language. The
C language also provides several forms of shorthand for these arithmetic operations.
One of these was mentioned earlier and is used commonly in for loops.
These shorthand expressions can be combined with other arithmetic
operations to produce more complex expressions. This is where the difference
between i++ and ++i becomes apparent. The first expression means
Increment the value of i by 1 after evaluating the arithmetic operation, while the
second expression means Increment the value of i by 1 before evaluating the
arithmetic operation. The following example will help clarify.
int a, b;
a = 5;
b = a++ * 6;
At the end of this set of instructions, b will contain 30 and a will contain 6,
since the shorthand of b = a++ * 6; is equivalent to the following statements:
b = a * 6;
a = a + 1;
However, if the instruction b = ++a * 6; is used, the order of the addition
to a changes, resulting in the following equivalent instructions:
a = a + 1;
b = a * 6;
Since the order has changed, in this case b will contain 36, and a will still
contain 6.
Quite often in programs, variables need to be modified in place. For
example, you might need to add an arbitrary value like 12 to a variable, and
store the result right back in that variable (for example, i = i + 12). This
happens commonly enough that shorthand also exists for it.
0x243 Comparison Operators
Variables are frequently used in the conditional statements of the previously
explained control structures. These conditional statements are based on some
sort of comparison. In C, these comparison operators use a shorthand syntax
that is fairly common across many programming languages.
Most of these operators are self-explanatory; however, notice that the
shorthand for equal to uses double equal signs. This is an important distinction,
since the double equal sign is used to test equivalence, while the single
equal sign is used to assign a value to a variable. The statement a = 7 means
Put the value 7 in the variable a, while a == 7 means Check to see whether the variable
a is equal to 7. (Some programming languages like Pascal actually use := for
variable assignment to eliminate visual confusion.) Also, notice that an
exclamation point generally means not. This symbol can be used by itself to
invert any expression.
!(a < b) is equivalent to (a >= b)
These comparison operators can also be chained together using shorthand
for OR and AND.
Full Expression Shorthand Explanation
i = i + 12 i+=12 Add some value to the variable.
i = i - 12 i-=12 Subtract some value from the variable.
i = i * 12 i*=12 Multiply some value by the variable.
i = i / 12 i/=12 Divide some value from the variable.
Condition Symbol Example
Less than < (a < b)
Greater than > (a > b)
Less than or equal to <= (a <= b)
Greater than or equal to >= (a >= b)
Equal to == (a == b)
Not equal to != (a != b)
Logic Symbol Example
OR || ((a < b) || (a < c))
AND && ((a < b) && !(a < c))
P rogramming 15
The example statement consisting of the two smaller conditions joined
with OR logic will fire true if a is less than b, OR if a is less than c. Similarly,
the example statement consisting of two smaller comparisons joined with
AND logic will fire true if a is less than b AND a is not less than c. These
statements should be grouped with parentheses and can contain many
different variations.
Many things can be boiled down to variables, comparison operators, and
control structures. Returning to the example of the mouse searching for food,
hunger can be translated into a Boolean true/false variable. Naturally, 1
means true and 0 means false.
While (hungry == 1)
{
Find some food;
Eat the food;
}
Here’s another shorthand used by programmers and hackers quite
often. C doesn’t really have any Boolean operators, so any nonzero value is
considered true, and a statement is considered false if it contains 0. In fact,
the comparison operators will actually return a value of 1 if the comparison is
true and a value of 0 if it is false. Checking to see whether the variable hungry
is equal to 1 will return 1 if hungry equals 1 and 0 if hungry equals 0. Since the
program only uses these two cases, the comparison operator can be dropped
altogether.
While (hungry)
{
Find some food;
Eat the food;
}
A smarter mouse program with more inputs demonstrates how comparison
operators can be combined with variables.
While ((hungry) && !(cat_present))
{
Find some food;
If(!(food_is_on_a_mousetrap))
Eat the food;
}
This example assumes there are also variables that describe the presence
of a cat and the location of the food, with a value of 1 for true and 0 for false.
Just remember that any nonzero value is considered true, and the value of 0
is considered false.
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 pseudocode
should look very similar to C code.
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
12 0x200
called constants. Returning to the driving example, the speed of the car would
be a variable, while the color of the car would be a constant. In pseudocode,
variables are simple abstract concepts, but in C (and in many other
languages), variables must be declared and given a type before they can be
used. This is because a C program will eventually be compiled into an executable
program. Like a cooking recipe that lists all the required ingredients
before giving the instructions, variable declarations allow you to make preparations
before getting into the meat of the program. Ultimately, all variables
are stored in memory somewhere, and their declarations allow the compiler
to organize this memory more efficiently. In the end though, despite all of
the variable type declarations, everything is all just memory.
In C, each variable is given a type that describes the information that is
meant to be stored in that variable. Some of the most common types are int
(integer values), float (decimal floating-point values), and char (single character
values). Variables are declared simply by using these keywords before
listing the variables, as you can see below.
int a, b;
float k;
char z;
The variables a and b are now defined as integers, k can accept floatingpoint
values (such as 3.14), and z is expected to hold a character value, like A
or w. Variables can be assigned values when they are declared or anytime
afterward, using the = operator.
int a = 13, b;
float k;
char z = 'A';
k = 3.14;
z = 'w';
b = a + 5;
After the following instructions are executed, the variable a will contain
the value of 13, k will contain the number 3.14, z will contain the character w,
and b will contain the value 18, since 13 plus 5 equals 18. Variables are simply
a way to remember values; however, with C, you must first declare each
variable’s type.
Arithmetic Operators
The statement b = a + 7 is an example of a very simple arithmetic operator.
In C, the following symbols are used for various arithmetic operations.
The first four operations should look familiar. Modulo reduction may
seem like a new concept, but it’s really just taking the remainder after division.
If a is 13, then 13 divided by 5 equals 2, with a remainder of 3, which
means that a % 5 = 3. Also, since the variables a and b are integers, the
P rogramming 13
statement b = a / 5 will result in the value of 2 being stored in b, since that’s
the integer portion of it. Floating-point variables must be used to retain the
more correct answer of 2.6.
To get a program to use these concepts, you must speak its language. The
C language also provides several forms of shorthand for these arithmetic operations.
One of these was mentioned earlier and is used commonly in for loops.
These shorthand expressions can be combined with other arithmetic
operations to produce more complex expressions. This is where the difference
between i++ and ++i becomes apparent. The first expression means
Increment the value of i by 1 after evaluating the arithmetic operation, while the
second expression means Increment the value of i by 1 before evaluating the
arithmetic operation. The following example will help clarify.
int a, b;
a = 5;
b = a++ * 6;
At the end of this set of instructions, b will contain 30 and a will contain 6,
since the shorthand of b = a++ * 6; is equivalent to the following statements:
b = a * 6;
a = a + 1;
However, if the instruction b = ++a * 6; is used, the order of the addition
to a changes, resulting in the following equivalent instructions:
a = a + 1;
b = a * 6;
Since the order has changed, in this case b will contain 36, and a will still
contain 6.
Quite often in programs, variables need to be modified in place. For
example, you might need to add an arbitrary value like 12 to a variable, and
store the result right back in that variable (for example, i = i + 12). This
happens commonly enough that shorthand also exists for it.
0x243 Comparison Operators
Variables are frequently used in the conditional statements of the previously
explained control structures. These conditional statements are based on some
sort of comparison. In C, these comparison operators use a shorthand syntax
that is fairly common across many programming languages.
Most of these operators are self-explanatory; however, notice that the
shorthand for equal to uses double equal signs. This is an important distinction,
since the double equal sign is used to test equivalence, while the single
equal sign is used to assign a value to a variable. The statement a = 7 means
Put the value 7 in the variable a, while a == 7 means Check to see whether the variable
a is equal to 7. (Some programming languages like Pascal actually use := for
variable assignment to eliminate visual confusion.) Also, notice that an
exclamation point generally means not. This symbol can be used by itself to
invert any expression.
!(a < b) is equivalent to (a >= b)
These comparison operators can also be chained together using shorthand
for OR and AND.
Full Expression Shorthand Explanation
i = i + 12 i+=12 Add some value to the variable.
i = i - 12 i-=12 Subtract some value from the variable.
i = i * 12 i*=12 Multiply some value by the variable.
i = i / 12 i/=12 Divide some value from the variable.
Condition Symbol Example
Less than < (a < b)
Greater than > (a > b)
Less than or equal to <= (a <= b)
Greater than or equal to >= (a >= b)
Equal to == (a == b)
Not equal to != (a != b)
Logic Symbol Example
OR || ((a < b) || (a < c))
AND && ((a < b) && !(a < c))
P rogramming 15
The example statement consisting of the two smaller conditions joined
with OR logic will fire true if a is less than b, OR if a is less than c. Similarly,
the example statement consisting of two smaller comparisons joined with
AND logic will fire true if a is less than b AND a is not less than c. These
statements should be grouped with parentheses and can contain many
different variations.
Many things can be boiled down to variables, comparison operators, and
control structures. Returning to the example of the mouse searching for food,
hunger can be translated into a Boolean true/false variable. Naturally, 1
means true and 0 means false.
While (hungry == 1)
{
Find some food;
Eat the food;
}
Here’s another shorthand used by programmers and hackers quite
often. C doesn’t really have any Boolean operators, so any nonzero value is
considered true, and a statement is considered false if it contains 0. In fact,
the comparison operators will actually return a value of 1 if the comparison is
true and a value of 0 if it is false. Checking to see whether the variable hungry
is equal to 1 will return 1 if hungry equals 1 and 0 if hungry equals 0. Since the
program only uses these two cases, the comparison operator can be dropped
altogether.
While (hungry)
{
Find some food;
Eat the food;
}
A smarter mouse program with more inputs demonstrates how comparison
operators can be combined with variables.
While ((hungry) && !(cat_present))
{
Find some food;
If(!(food_is_on_a_mousetrap))
Eat the food;
}
This example assumes there are also variables that describe the presence
of a cat and the location of the food, with a value of 1 for true and 0 for false.
Just remember that any nonzero value is considered true, and the value of 0
is considered false.
0 comments:
Post a Comment