Reputation: 329
I am trying to skip values using a for loop. Something like
for(int i = 32; i <= 255 - but skip 128 to 159; i++) {
char ascii = (char) i;
System.out.println(ascii);
}
Any suggestions? Thanks!
Upvotes: 2
Views: 13282
Reputation: 1
You can try this:
for (int i=32; counter<=255; i++){
if (counter>=128 && counter<=159) {
continue;
}
char ascii = (char) i;
System.out.println(ascii);
}
Upvotes: 0
Reputation: 11
Here is some code:
public static void main(String[] args) {
for(int i = 32; i <= 255; i++) {
if (i < 128 || i > 159) {
char ascii = (char) i;
System.out.println(ascii);
}
}
}
Upvotes: 1
Reputation: 2531
just for completeness, this is also possible
for (int i = 32; i <= 255; i = (i == 127 ? 160 : i + 1)) {
char ascii = (char) i;
System.out.println(ascii);
}
Upvotes: 3
Reputation: 43
Or add the test to the loop like a functional language:
for(int i = 32; i <= 255; i++) if (i < 128 || i > 159) {
char ascii = (char) i;
System.out.println(ascii);
}
Upvotes: 1
Reputation:
Use this at the beginning of your loop:
for(int i = 32; i < 256; i++) {
if(i == 128) i = 160;
//...
}
This is MUCH better than simply continuing. You don't want to iterate over 128 to 159; you'd be wasting time.
Upvotes: 13
Reputation: 8280
I would make two loops :
int MIN = 128;
int MAX = 159;
for(int i = 32; i < MIN ; i++) {
char ascii = (char) i;
System.out.println(ascii);
}
for(int i = MAX + 1; i < 255; i++){
char ascii = (char) i;
System.out.println(ascii);
}
Upvotes: 0
Reputation: 11913
for(int i = 32; i <= 255 - but skip 128 to 159; i++) {
char ascii = (char) i;
System.out.println(ascii);
if(i == 127) {
i = 160;
}
}
Upvotes: 1
Reputation: 16526
You could add an if statement inside your loop.-
for(int i = 32; i <= 255; i++) {
if (i < 128 || i > 159) {
char ascii = (char) i;
System.out.println(ascii);
}
}
Upvotes: 0
Reputation: 726559
You can skip the elements that you do not want, like this:
for(int i = 32; i <= 255; i++) {
if (i >= 128 && i <= 159) continue;
char ascii = (char) i;
System.out.println(ascii);
}
or split the loop in two, like this:
for(int i = 32; i <= 127; i++) {
char ascii = (char) i;
System.out.println(ascii);
}
for(int i = 160; i <= 256; i++) {
char ascii = (char) i;
System.out.println(ascii);
}
Upvotes: 3