|
Beginner’s Guide to Web Development – Programming Basics
This section's goal is to give you an overview of programming features and constructs that you will
need to know in order to be a good web developer. We are going to cover arithmetic operations; comparison
operations; and "if", "else", and "for" statments. Coupled with your knowledge of Java basics learned in the previous section,
your knowledge of programming basics will get you on the right track to being a successful web developer.
Arithmetic Operations
We are going to cover the four basic arithmetic operations. These are addition, subtraction, multiplication, and
division.
Addition:
Addition is simple. The following adds four to x:
double x = 4.1;
x = x + 4.0;
The result in this case: x is now equal to 8.1.
As a sidenote, arithmetic operations can become tricky if you are dealing with different data types. For example, adding a byte
and a short may not give you the answer you think you should get. Thus, be careful and for simplicity, try to perform operations
on like data types and beware of overflow. Remember the min and max values, which were listed in the previous section, for the various data types.
Also, as a side note, most programmers use the integer and double data types almost exclusively when
dealing with numbers, for example, integers for whole numbers and double for decimals. This technique
helps to eliminate the complexity associated with data type conversion.
Subtraction:
Subtraction is also simple. The following subtracts four from x:
int x = 5;
x = x - 4;
The result in this case: x is now 1;
Multiplication:
Here's an example of multiplication:
double x = 2.5;
x = x * 3.0;
The result here: m is now 7.5.
Division:
Division requires care in its use. For example, when dividing integers that don't divide evenly into
each other, the decimal result will be truncated. For example, 5 divided by 2 is 2.5. However, when using
integers, 5 / 2 is 2. Thus, to get the correct answer for 5 / 2, you will need to use a double or float.
Here's an example of division:
double x = 5.0;
x = x / 2.0;
The result here: x is 2.5.
That covers the arithmetic operations. Now it's time for comparison operations.
Comparison Operations
We are going to cover the six comparison operations. They are less than (<), less than or equal to (<=),
greater than (>), greater than or equal to (>=), equals to (==), and not equal to (!=);
Comparison operators return a boolean data type. Thus, the value of the comparison is either true or false.
Let's go over an example for each of the comparison operators.
Less Than
int x = 4;
int y = 5;
boolean xy = x < y;
boolean yx = y < x;
In the above code, the variable xy would be equal to true since the number '4' is less than the number '5'. The
variable yx would be false since '5' is not less than '4'.
Less Than or Equal To
int a = 4;
int b = 4;
int c = 5;
boolean ab = a <= b;
boolean ac = a <= c;
boolean ca = c <= a;
The results of the above code are as follows:
ab = true
ac = true
ca = false
Greater Than
int x = 4;
int y = 5;
boolean xy = x > y;
boolean yx = y > x;
In the above code, xy would be false, yx would be true.
Greater Than or Equal To
int a = 4;
int b = 4;
int c = 5;
boolean ab = a >= b;
boolean ac = a >= c;
boolean ca = c >= a;
In the above, the following occurs,
ab = true
ac = false
ca = true
Equals To
The '==' test can be misleading. For primitive data types, '==' tests to see if the types share the same
value. However, for objects, '==' determines if the objects refer to the same memory address. Thus, you probably will
not use '==' when referring to objects. Objects have a built-in function 'equals' that can be used instead.
Here is an example of '==' using primitive data types.
int x = 3;
int y = 3;
int z = 4;
boolean xy = x == y;
boolean xz = x == z;
The results of the above are
xy = true
xz = false
Here is an example that tests for equality using objects.
String s = "hello";
String t = "hello";
boolean st = s.equals(t);
The variable st in the example above equals true.
Not Equal To
Not equal to '!=' has the same constraints as '=='. For primitive data types, use '!='. For objects,
use 'equals'. If the result is false, the two objects are not equal. Here are some examples.
int x = 1;
int y = 2;
boolean xy = x != y;
The boolean xy in the above is true since x does not equal y.
String s = "hello";
String t = "hi";
boolean st = s.equals(t);
The boolean st in the above evaluates to false.
Now we're going to move on to "if", "else", and "for" statements. As you will see, comparison operations play a
large role in the functionality of these statements.
if, else, and for
The "if" Statement
The first statement we're going to cover is the "if" statement. The purpose of the "if" statement
is to execute a block of code if a certain condition is met. If that condition is not met, the "if" statement
does not execute. Take a look at the following example:
int x = 4;
if ( x > 2)
{
x = x + 1;
}
In the above code, the final value of the variable x is '5'. This is because the condition in the "if" statement was
true, thus the code block inside the statement is executed. Below is the basic structure of an "if" statement.
if ( [comparison operation] )
{
[code block];
}
Let's take a look at another example:
int x = 4;
if ( x < 3)
{
x = x + 1;
}
In the above code, the final value of the variable x is '4'. This is becuase the code block inside the "if"
statement will never execute. The reason for this is the comparison operations -- x < 3 -- returns false. In this
case, x is not less than '3'.
The "else" Statement
The "else" statement has a unique requirement: it can not exist without an "if" statement preceeding it.
An else statement only gets executed if there is a preceeding "if" statement, and the comparison
operation of that "if" statement fails. Let's look at an example:
int x = 4;
if ( x < 3)
{
x = x + 1;
}
else
{
x = x * 2;
}
The final result of the variable x in the above case is '8'. The "if" statement's comparison operation fails. 'x' is
not less than '3'. Thus, the "else" statement gets executed.
The structure of an "else" statement is the following:
else
{
[code block];
}
Remember, the "else" statement only gets executed if the preceeding "if" statement does not. Thus, if the comparison
operation of the corresponding "if" statement evaluates to true, the code inside the "else" statement will not execute.
Now that we've covered "if" and "else" statements, it's time to cover the "for" loop.
The "for" Loop
The "for" loop is a very powerful construct that allows you execute a block of code repeatedly
provided that a certain condition is met before each execution. Let's take a look at the structure of a
"for" loop
for ( [initialize a variable]; [comparison operation]; [iteration expression])
{
[code block];
}
Here's an example:
int x = 0;
for (int i = 0; i < 5; i++)
{
x = x + 1;
}
The final value of the variable x above is '5'. This is because the line "x = x + 1" was executed five times.
The code block of the "for" loop gets executed as long as the variable 'i' is less than '5'. Each time after the code block gets executed the
iteration expression gets executed. In this case, the iteration expression is "i++". "i++" is simply shorthand for "i = i + 1".
Then, the loop starts over again. The expression "i < 5" is evaluated. If this expression returns true, the code block gets executed
again. This process repeats until 'i' is no longer less than '5'.
One thing to note about loops is that other statements such as "if" and "else" statements can be placed inside of the loop. This is also
true of the "if" and "else" statements. In other words, loops can be placed inside "if" and "else" statements.
Take a look at this example of an "if" statement inside of a loop:
int x = 0;
int y = 0;
for (int i = 0; i < 5; i++)
{
if (x == 2)
{
y = 17;
}
}
After the above code block, the variable 'y' has a value of '17'. This is because during the
execution of the loop, 'x' did equal '2' at one point. Thus, the "if" statement was executed and
'y' was set to '17'.
This concludes our overview of programming basics. In the upcoming sections and in the sample application,
we will incorporate everything we've learned thus far into real world examples. Consequently, if you're
not 100% up to speed on HTML, Java, or programming basics, don't worry, the examples covered in future sections
should help you immensely.
It's now time to begin writing JSP code, which is the focus of the next section.
|