Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I have a loop in my java program and I want to run some test code every say 10th iteration in the loop. How do I do this ???
Use something like if (i % 10 == 0) where i is your loop counter. % is the remainder operator.
If i starts from 0 then the condition is true on the first iteration. If you want to start i from 0 but don't want the condition to be true until the 10th trial, then use if (i % 10 == 9).
You can use the % operator with 10 and your counter. See below for example.
for(int i=0;i<=100;i++){
if(i>0&&i%10==0){
System.out.println(i);//replace this with your code.
}
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I need to write a for loop in java that displays all of the numbers ranging from 13 - 93 that end in the number 3. It must include 13 and 93.
Loop from 13 to 93. Increment by 10. Like,
for (int i = 13; i <= 93; i += 10) {
// Print the value
}
If you wanted, you could write a function that achieves this task but with parameters
that allow for more flexible control. Where it takes in arguments representing a
from value and a to value, and something like a endsWith value. It's just a suggestion though.
Any decimal based number mod 10 will get you the last digit.
for (int i = start ; i <= end ; i++) {
if (i % 10 == 3) {
System.out.println(i);
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Here is the code that I tried in VS Code:
int i = 1;
while (i < 10)
if((i++) % 2 == 0)
System.out.println(i);
And the output was:
3
5
7
9
At the first iteration of the loop i == 1.
i++ increments i to 2, but returns 1.
Therefore if((i++) % 2 == 0) evaluates to false (since 1 is not even), and nothing is printed.
In the next iteration, i++ increments i to 3, but returns 2.
Therefore if((i++) % 2 == 0) evaluates to true (since 2 is even), and 3 is printed.
1 would not be printed even if i was printed on the first iteration of the loop, since i++ increments i to 2 before the println statement.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Make a method/function with one parameter/argument of int type. if user pass 5 value then it will return 7 and if user put 7 it will return 5 . but important thing is that I was not allowed to use any conditional operation like == , <, >, <=, >=, if else, ternary operator ,switch statement , while, for loop and any built in method.
public int fiveOrSeven(int input){
return 35 / input;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I found a few weird constructs in Java that the compiler allows, but for which I'm not sure what could be the practical use.
1) if statement:
if((score=score+10) > 110); //No if body
while eg: switch(++i); is not
2) for loop:
for(;;); //No loop body
Are there practical, valid circumstances to use the preceding code?
This:
if((score=score+10) > 110);
is equivalent to:
score += 10;
but has no practical use otherwise.
This:
for(;;);
loops forever doing nothing - not particularly useful, unless you wanted to create a permanently busy thread perhaps for testing purposes.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So, I'm trying to calculate a list of number to eventually sort, so I only want the final result of this for loop.
for (int anno=startyear; TimePeriod>=anno;anno++) {
System.out.println(anno);
}
Where anno = 1995 and I am counting to the current day, I end up getting a result that slowly counts up, where it first counts at 1995, then it counts 1995 and then 1996, and so on.
How do I only get the end result for use in my program? The result that would simply be 1995-2014. Not the repeats.
edit: Forgot to mention I need every number in between 1995-2014 as well
You shouldn't need a loop for this, assuming your TimePeriod variable equals 2014 then just do the following to print out the desired result:
System.out.println(startyear+"-"+TimePeriod);
That will print out:
1995-2014
You already know the final value: it's TimePeriod. If that's all you need, just use that and get rid of the loop:
System.out.printf("%d-%d", startyear, TimePeriod);