Reputation: 3
How would I do this? Sorry my first time posting here.
int controller = 100;
while(controller <= 2)
{
System.out.println("Step 2");
controller++;
}
System.out.println("Done");
Upvotes: 0
Views: 2961
Reputation: 1
You can use a -2 decrement as already answered or a 'modulo 2' way.
for (int controller = 100; controller >= 2; controller --) {
if (controller % 2 == 0)
System.out.println(controller + " is even");
}
Upvotes: 0
Reputation: 201447
Your code is almost correct. You should reduce the index by 2 (not increment by 1), and you should check that the value is >= 2
(not <= 2
). Also, I believe you wanted to print controller
. Like
int controller = 100;
while(controller >= 2)
{
System.out.println(controller);
controller -= 2;
}
System.out.println("Done");
or like
for (int controller = 100; controller >= 2; controller -= 2) {
System.out.println(controller);
}
System.out.println("Done");
Upvotes: 2