Classic Loops
Normal code runs in a linear fashion, from 1 line to the next to the next, never skipping a line never repeating a line.
This is all well and good but sometimes we like code to repeat, we built computers after all to repeat tasks millions of times, which we humans are just too slow and lazy to do.
Loops are a primary course of program crashes and hangs, often the programmer makes a mistake or doesn't think of a way out of his/her loop, the computer gets to it runs the loops all day and does nothing else, leaving the user sarring angrily at the computer, so I should probably code a way out before covering the loop itself.
Like an 'if' statement a loop runs the code while something is true, the difference being it KEEPS running the code while it is true and keeps checking 'if' it is still true, this is the only difference.
Their are two important commands to change a loops direction or stop it in its track:
BREAK
I believe you have met in the 'switch' tutorial. Break, breaks the loop and exits it, ignoring the run while true and exiting immediatly so very useful indeed, if something goes wrong call 'break'.
CONTINUE
Loops can contain alot of code, you may not wish to reach the bottom of the loop before, looping again to save processor power and time you can call continue at any time in a loop to go back to the top of the loop!
Loop example: (why not try it out?)
Method 1:
Code:
public static void main(String[] args) {
int i=0;
System.out.println("Entering my first loop");
while (i < 50){
i++;
System.out.println("Incrementer i="+i);
}
System.out.println("my first loop exited");
}
Method 2:
Code:
public static void main(String[] args) {
int i=0;
System.out.println("Entering my first loop");
while (true){
i++;
System.out.println("Incrementer i="+i);
if(i >= 50) break;
}
System.out.println("my first loop exited");
}
Congratulations you just ran something 50 times with 8 lines of code!
2 ways were shown, Method 1 ran the loop while i was less than 50, "while (i < 50)" while method 2 ran the loops indefinatly, and instead exited when i was more than 50 "while (true)"
The challenge
Using the code from the previous tutorial also shown below can you take out the repeating code and use a loop instead?
USE:
Code:
public static void main(String[] args) {
// Array declaration, like the sample above 5 different names 0,1,2,3,4 (NOT 5)
String name[] = new String[5];
// setting values
name[0]="martin";
name[1]="joe";
name[2]="moe";
name[3]="";
name[4]="";
String tmpname="john";
int i=0, set_index=-1;
/* TODO: REMOVE THIS REPEATING PART */
// 0
if(name[i].compareTo("")==0){
set_index=i;
}
// 1
i++;
if(name[i].compareTo("")==0){
set_index=i;
}
// 2
i++;
if(name[i].compareTo("")==0){
set_index=i;
}
// 3
i++;
if(name[i].compareTo("")==0){
set_index=i;
}
// 4
i++;
if(name[i].compareTo("")==0){
set_index=i;
}
/* TODO: ENDS */
if(set_index==-1){
System.out.println("memory full");
} else{
name[set_index]=tmpname
}
}
One method of solving the problem:
[click for answer]
Code:
public static void main(String[] args) {
// Array declaration, like the sample above 5 different names 0,1,2,3,4 (NOT 5)
String name[] = new String[5];
// setting values
name[0]="martin";
name[1]="joe";
name[2]="moe";
name[3]="";
name[4]="";
String tmpname="john";
int i=0, set_index=-1;
/* TODO: REMOVE THIS REPEATING PART */
while(true){
if(name[i].compareTo("")==0){
set_index=i;
break; // we have done our job exit the loop
}
i++;
if(i > 4) break; // we failed our job exit the loop
}
/* TODO: ENDS */
if(set_index==-1){
System.out.println("memory full");
} else{
name[set_index]=tmpname
}
}
Copyright (c) 2008, Martin Sykes.
Learning Center is a branch of Martrinex Systems, Martrinex.net
Do not copy any materials from this site without permission from the auther.
Add Comment
Comments