Sometime during the year, I must make a webpage. As much fun as it sounds, I've yet to make it, so I'm forced to do it now. My topic will be...
for statements
Color and
bolding added in case topic is not exciting enough. I really should
get on task.
It computer programming the need for looping arose.
Programmers wanted an action to repeated until a certain condition was
satisfied. So they were given while
statements. But it wasn't enough. They soon found themselves
typing a similar pattern:
... int count = 0; int repeats = 7; while (count <= repeats)
...} |
Programmers soon tired of this repeated structure which
wasn't mean to repeat under a condition , it was meant to repeat a certain
number of times. So the for statement was born.
It's basic structure is:
for (variable1 = start; variable1 rationalOperator end; variableIncrement) { block statement} |
where variable1 is a local, numeric value (usually an int, but not necessarily),
start is your initial value, and end is your last value,
but not exactly. Because the loop may never reach exactly the end
value, it is stated as a conditional statements similar to those found
in while statements. In between
variable1 and end is rationalOperator, which can
be any rational operator, but is usually either <= or =>, based on variableIncrement,
which tells how to edit, or update, variable1. Common uses
of this are i++ for positively incrementing
values, and i-- for countdowns.
The actions of the for statement
are included in block statement.
Note: variable1 is local, meaning that unless it's been predefined
in the program, you must intialize it. After the loop runs, it will
lose its redefined value and if instatiated, be deleted. This is so
excess values will not be stored.
There are many uses of a for statement. For instance,
to calculate interest on a bank account, one must use for statements to
continuously calculate money, if do not want to use the insanely handy
P(1+r/100)t*r/n, we can use for statements to do this.
For example:
for (int i; i
<= n; i++) { double interest = balance * rate / 100;} |
Ain't she wonderful, folks? Now, we can calculate the amount of
money in our banking account after a set number of years (n), assuming the bank updates
annually, instead of quarterly, monthly, weekly, daily, hourly, minutely,
secondly, or continuously. But we can edit the code to do that! That'll
be your first exercise. Create a program using this as a shell for
part of the program. It will have parameters, hopefully. Probably
if-else statements. Look
them up. I don't have time to tell you about them.
For more on for statements, see any of the following:
Elliott
e
Rusty Harold's Brewing Java: A Tutorial
This is the most inopportune time to mention the book we are using, but
I guess here's better than anywhere. It's called Computing Concepts
with Java Essentials by Cay Horstmann.
Now that we know all about for statements, let's talk
about mistakes that stupid programmers such as yourselves make. The
first one is reading a webpage from a novice, cynical, self-righteous student
in a unually lazy programming class. What you messed up already?
Mistake #1: Inconsistancy of condition and update
For example:
for
(int myCounter = 0, myCounter >= 0, myCounter++) { //miscellanous programming stuff} |
See the problem? Me neither.
Oh, you do? Okay, me too. The program
will increment forever! Now, there are probably better examples of
how a programmer would mess up than this, but you get the idea, right? Here's
a vocabulary term: range. It's the set of numbers that one would
like to increment while one is talking in the indefinite person tense. Let's
say you wanted to find the square of all the numbers from 0 to 10. If
you don't know them by heart, go back to fourth grade, please.
Before we, start let's establish some things. First,
what variable do we want to use? Well, these values are all integers,
so it's a safe bet we can use an int. We want to start
with 0, so that's our initiation value. We want to stop at
10 (mind the italics), so we want the value to increment until it equals
(you're minding the italics, aren't you?) 10, so it's less than or equal
to 10 (>= 10), and
finally, we want every number from one to ten, so we can use the ++ shorthand.
for
(int base = 0, base <= 10, base++) { System.out.println(base * base);} |
Mistake #2 (of 2; this page is rather short): Using the Forbidden
Operator
For example
for
(double ickyDecimal = 3.12904; ickyDecimal != 31.4205, ickyDecimal = ickyDecimal
+ 1.04783) { ...} |
Now, just for you, I've created number that will work out...
maybe. The forbidden operator is the not-equal-to operator (!=). Because of scary limited
precision, using this may create a rounding error, so the variable, in my
case ickyDecimal, will never quite equal 31.4205, even though to the common
laymen (with a calculator, because the common laymen can't do math anymore),
if you continue to add 1.04783 to 3.12904 repeated, you will eventually
come up with 31.4205. Don't yell at the computer. It can't hear
you. Unless it has an audio input device, then it can hear, but it
won't care, because it is (1) an inanimate machine and (2) likely possessed
by a meticulously peevish demon, or in Karen Lockhart's case, Satan. Just
use the less-than (<)
or the less-than-or-equal-to operator (<=), and you will in most
cases be fine. I promise. Just don't yell at me if you're not.
I can't here you either.
Well, if you have any questions, I am an amateur programmer
who will be of little use to you, so please e-mail on an e-mail account
that I cannot access outside of school, especially during the summer, which
will destroyed sometime around June 2004, on a network that crashes often.
And this website? Maybe it will never disappear. It depends
on my teacher. Please visit his webpage with the link at the top of
the page. Don't worry, we're pretty confused by it, too, sometimes.
But if you think you may have gotten for statements down cold,
feel free to use these Exercises
from jGuru at Java Sun.
E-mail me: Christopher Verdery
The Writings of Lord Godeerc VanDrey
(Note: This is not a link to a for statements-related page.
You have been warned.)
©2003 Creedog, Inc. A Division of Godeerc VanDrey Enterprises, Inc. This page was last updated Monday, May 5, 2003. You'll be lucky if it is ever updated again.