Reputation: 687
I wanted to run a for loop for 1,7,14,19.I know this is a basic question but I could not get the idea.I tried with
for(int i=1;;i++){
if(i==1||i==7||i==14||i==19){
System.out.println(i);
} else if(i==20){
break;
} else{
}
}
But this keeps on printing.Also same with below code
for(int i=1;(i==1||i==7||i==14||i==19);i++){
System.out.println(i);
}
Any help is appreciated.
Upvotes: 0
Views: 436
Reputation: 161
Use Array and for each
int ary[]= { 1, 7, 14, 19} ;
for(int i : ary){
System.out.println(i);
}
Upvotes: 1
Reputation: 201439
In Java 8+, you could use an IntStream
. Like,
IntStream.of(1, 7, 14, 19).forEachOrdered(System.out::println);
Upvotes: 5
Reputation: 533492
I use an array
for (int i : new int[] { 1, 7, 14, 19 }) {
// something with i
Upvotes: 11