You cannot do that you have to go by putting the if statements in the for loop.
Answer from Girish on Stack OverflowYou cannot do that you have to go by putting the if statements in the for loop.
You cannot add anything else to the () part of the "enhanced for" statement. JLS Β§ 14.14.2:
The enhanced for statement has the form:
for ( FormalParameter : Expression ) StatementThe type of the Expression must be
Iterableor an array type.
(The "FormalParameter" means a variable declaration.)
The break; statement is the only way:
for (String s : string_table) {
if (condition) break;
// ...
}
java - Implementing a conditional for loop - Stack Overflow
Java Foreach with a condition - Stack Overflow
Giving multiple conditions in for loop in Java - Stack Overflow
java - In which conditional statement(loop) condition is not necessary? - Stack Overflow
Videos
A clean approach would be to store the bounds in int variables and then select the corresponding variable based on the initial condition:
int bound;
if (a) {
bound = x;
} else {
bound = y;
}
for (int i = 0; i < bound; i++) {
...
}
You can use the ternary operator for that kind of assignment like
int bound = a ? x : y;
Or directly inside the for loop:
for (int i = 0; i < (a ? x : y); i++) {
...
}
Be aware that, with the first approach, the condition will only be evaluated once. If it can change inside the loop you will need to update it inside.
for(int i = 0; a ? (i<x) : (i<y); i++){}
or
for(int i = 0; i < (a ? x : y); i++){}
It must be asked why you'd want to...
No.
You could use a while loop instead.
Iterator iterator = list.iterator();
while(iterator.hasNext()) {
...
}
No, there is nothing like that. The "enhanced for loop" is a completely separate construct that does nothing except lopp through the iterator returned by its Iterable parameter.
What you can do is this:
for(Foo foo : foos)
{
//Do something
if(!condition){
break;
}
}
You can also use "or" operator,
for( int i = 0 ; i < 100 || someOtherCondition() ; i++ ) {
...
}
A basic for statement includes
- 0..n initialization statements (
ForInit) - 0..1 expression statements that evaluate to
booleanorBoolean(ForStatement) and - 0..n update statements (
ForUpdate)
If you need multiple conditions to build your ForStatement, then use the standard logic operators (&&, ||, |, ...) but - I suggest to use a private method if it gets to complicated:
for (int i = 0, j = 0; isMatrixElement(i,j,myArray); i++, j++) {
// ...
}
and
private boolean isMatrixElement(i,j,myArray) {
return (i < myArray.length) && (j < myArray[i].length); // stupid dummy code!
}
The correct answer is "1. for loop".
This is a syntactically legal for loop in Java: for (;;) {...}.
It behaves exactly the same as for (;true;) {...}.
In a do-while loop, a condition is required, e.g.: do {...} while(true);.
In a while loop, a condition is required, e.g.: while(true) {...}.
From the Java Language Specification*:
while (boolean-expression) Statementfor ( [ForInit] ; [boolean-Expression] ; [ForUpdate] ) Statementdo Statement while ( boolean-Expression )
So in other words, the for statement does not require a "conditional statement"
*) added "boolean-" to "expression" for clarity
I have this rock paper scissors game, which allows the user to enter "rock", "paper" or "scissors". They will then get a message saying weather they have beat the computer or not.
I want to modify this so that the user can play the game three times, while the app keeps count of who won each game. So I need a for loop to this, but I am not sure how to use an for loop with an if statement. Any help would be appreciated. This is what I have now at the minute.
public void compute(){
if(player1.equalsIgnoreCase("Rock")){
if(player2 == 1){
winner = "draw";
}
else if(player2 == 2){
winner = "Player 2";
}
else{
winner = "Player 1";
}
}
else if(player1.equalsIgnoreCase("Paper")){
if(player2 == 1){
winner = "Player 1";
}
else if(player2 == 2){
winner = "Draw";
}
else{
winner = "Player 2";
}
}
else if(player1.equalsIgnoreCase("Scissors")){
if(player2 == 1){
winner = "Player 2";
}
else if(player2 == 2){
winner = "Player 1";
}
else{
winner = "Draw";
}
}
else{
winner = "Sorry, you have enered an invalid option.";
}
}It would probably be acceptable to use loops (for, while,...), but you would need to check with the project author. I tend to treat loops and conditional statements separately, as they usually have different purposes...
- Conditional statements like
ifandswitchwill make a choice out of a list of options. They only run once. - Loops like
forandwhileare typically designed to run a piece of code multiple times.
Of course this is only a generalisation, and everyone probably has a different opinion, but I certainly treat them differently because they have different primary purposes.
For extra credit, Wikipedia seems to agree. The If Statement is a conditional operator, and the For Loop is an iteration statement.
for and while loops use (terminating) conditions, not conditional statements, so on that basis loops are OK.
Apart from loops, another option would be the ternary operator ? - it's not a statement, it's an operator, and you may be able to code some conditional flow using these, ie this code:
int x;
if (<some condition>)
x = 1;
else
x = 2;
may be coded using the ternary operator as:
int x = <some condition> ? 1 : 2;