help me understand "continue"
My understanding of "continue" is that it will cause a jump to the end of the current loop.
Can someone explain what's happening in this code? I would think that once a character passes the first test (between 'a' and 'z'), it would skip out and re-loop with i++, but I've tested the method and it works a-ok.
publicstaticboolean isAlphaNumeric(String s){
char[] chars = s.toCharArray();
for (int i=0; i<chars.length; i++){
char c = chars[i];
if ((c >='a') && (c <='z'))continue;// lowercase
if ((c >='A') && (c <='Z'))continue;// uppercase
if ((c >='0') && (c <='9'))continue;// numeric
returnfalse;
}
returntrue;
}
[1690 byte] By [
torfua] at [2007-11-27 8:17:47]

You are right, continue skips to the next iteration.
In your code sample, what happens is that for every character read, it has to be within one of the three allowed ranges so that it can go on with the next iteration, otherwise the string is considered invalid. When using continue, the next steps occuring will be "incrementation" "verification of the condition" and then either it goes back inside the iteration block, or it ends there.
Continue is often used in a loop for exactly this purpose, to just plain 'ignore' this iteration in a loop.I have met Java purists that refuse to use continue and break in normal code, saying that it's bad design, but I have no issues with it at all.
Just for posterity's sake, you know that your function up there could be replaced with one line? Regexes are awesome.
return s.matches("\\p{Alnum}*");
Example follows:
public class IsAlnum {
public static boolean isAlphaNumeric(String s) {
return s.matches("\\p{Alnum}*");
}
public static void main(String[] args) {
for(String s: args) {
System.out.println( " The String " + s + " is " +
(isAlphaNumeric(s) ? "definitely" : "not") + " alphanumeric.");
}
}
}
kev@mymachile ~/java$ java IsAlnum Howdy y\'all Java hackers!
The String Howdy is definitely alphanumeric.
The String y'all is not alphanumeric.
The String Java is definitely alphanumeric.
The String hackers! is not alphanumeric.