|
||||||||||||||||
. . . . . |
Java For Kids tutorial 4
| Intro |
Begin |
if/else |
Loops |
Arrays |
Graphics |
Animation |
Mouse |
Game |
Real |
Methods |
Class |
Class 2 |
Applet |
MouseClick |
Thread |
Button |
Java LOOPSWHILE loopsTry this :
void main() {
// 1. declare and initialise variables
int counter = 1;
// 2. now do the while loop
while(counter < 5 )
{
printLine("counter is equal to " + counter);
// 3. add 1 to the value of counter
counter = counter + 1 ;
}
// 4. the while loop has finished,
//so program continues on to here
printLine("Goodbye");
}
What was printed ? Notes on Above code
Loops are repeating sets of statements and in the case of the while loop, here is the formula:
while(this statement is true)
{
// execute the statements or lines between these brackets
// add 1 to the counter (usually)
}
// and continue the program with the following lines
E.G.
while(x <5)
{
printLine(" x = " + x);
x = x + 1;
}
What will the above code output ?
Java Exercises:
Flash Tutorials in Video Format -
Watch them now at LearnFlash.com Java do .. while LOOPsdo while loops are similar to while loops except that the condition is tested after the code to be repeated or looped. The general form of a do .. while loop is as follows:
do
{
// execute code between these brackets
}
while(true or false(boolean) statement)
Note that the repeated code is always executed once, because the condition is tested after the loop ends. It may be better to use a plain old while loop in most cases, because it tests the condition before the loop starts. E.G
int y = 10 ;
printLine("Counting Down ..");
do
{
printLine(".. " + y);
y = y - 1;
}
while( y >1 )
printLine("Blast Off");
Try it !! What did You get ?
Java Exericises
Java for LOOPsJava for loops are a short hand way of;
Before we carry on we have have to learn about
the: Java INCREMENT operator ( ++ )x++; is the same as x = x + 1; X++ increments (or increases) the value of X by 1 . Here is an example that we could use in a while loop:
int x = 0 ; // declare and initialize counter x
while( x < 5)
{
printLine(" x = " + x );
// add 1 to the value of x
x++; // same as (x = x + 1)
}
Similarly , the: DECREMENT operator is ( -- )y-- decrements (or decreases) y by 1 y-- ; is the same as y = y - 1 ; Try this :
int y = 10 ;
while( y > 0)
{
printLine(" and " + y);
y-- ;
}
So, back to the for Loop.
for(int x = 0 ; x < 10 ; x++)
{
printLine("x = " + x);
}
The form of the for loop is:
for( INITIALIZE counter ; boolean CONDITION ; ITERATION )
{
// repeated code to be executed.
}
iteration means repetition, or repeating. In actual fact it is either usually increased by 1 (x++)
or decreased by 1 ( y--). Example B. Count down from 10 to 1
for(int x = 10 ; x>0 ; x--)
{
printLine(x);
}
|
|
||||||||||||||
|
| Home | Flash MX | Actionscript 2.0 | Flash 3D | Flash 8 | Flash Database | Flash Mobile | Flash CS3 | Java For Kids | Video Course | General Video | Photoshop | Web Design | Digital Photography | Forum | Games | free backgrounds | Resume | Flash Animations | Streaming Video | Students Work | Links | Contact me | sitemap | reviews | store | advertisers | . . |
||||||||||||||||