Right, before I start: this is a hw question.
We have to print out: -20, -15, -10, -5, 5, 10, 15, 20
for(i = -20; i<25; i += 5)
{
System.out.println(i);
}
I’ve gotten it to output all the numbers, however, we’re not allowed to output 0.
This question has me stumped . Any help please?
So in programming there are conditional statements. In java the keyword for this is
if. These statements only execute if the condition they check is true.In your case you have a condition, “I want to print out all numbers EXCEPT for zero”. You can take this and turn it into an if-statement. To do that you convert your statment to something like “as long as the number is not zero, print it out”. So once you’ve reached this you can write:
if (i != 0) System.out.println(i);So everytime you go through the loop the condition will check if
iis any number different from zero. If it is the print statement will execute. When it is zero it will do nothing as the statment we placed does not evaluate to true.