You cannot do that you have to go by putting the if statements in the for loop.

Answer from Girish on Stack Overflow
🌐
W3Schools
w3schools.com β€Ί java β€Ί java_for_loop.asp
Java For Loop
When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop: for (statement 1; statement 2; statement 3) { // code block to be executed } Statement 1 is executed (one time) before the ...
Discussions

java - Implementing a conditional for loop - Stack Overflow
Is there a way to do something like this in Java? for (int i = 0; if (a) { i More on stackoverflow.com
🌐 stackoverflow.com
Java Foreach with a condition - Stack Overflow
If you want you can check condition inside it and use break keyword for getting out of loop in middle. ... In Java 8, you can do it. More on stackoverflow.com
🌐 stackoverflow.com
Giving multiple conditions in for loop in Java - Stack Overflow
I was searching for "How to give multiple conditions in a for loop?" But there are no direct answers given. After some research I found the correct way. Conditions should not be comma(,) or semic... More on stackoverflow.com
🌐 stackoverflow.com
java - In which conditional statement(loop) condition is not necessary? - Stack Overflow
But again in 2. do while loop ..... ... then condition may not be necessary. Well I am not sure so please guide me. Thanks ... I think it is just a syntactical question, you can write for(;;) but you can't write while (). ... The correct answer is "1. for loop". This is a syntactically legal for loop in Java: for (;;) ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
W3Schools
w3schools.com β€Ί java β€Ί java_conditions.asp
Java Conditions and If Statements
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... Conditions and if statements let you control the flow of your program - deciding which code runs, and which code is skipped.
🌐
Princeton CS
introcs.cs.princeton.edu β€Ί java β€Ί 13flow
1.3 Conditionals and Loops
Java provides the break statement for this purpose. Prime.java takes an integer command-line argument n and prints true if n is prime, and false otherwise. There are two different ways to leave this loop: either the break statement is executed (because n is not prime) or the loop-continuation condition is not satisfied (because n is prime).
🌐
Cornell Computer Science
cs.cornell.edu β€Ί courses β€Ί cs1130 β€Ί 2012sp β€Ί 1130selfpaced β€Ί module2 β€Ί module2part1 β€Ί ifloop.html
Conditional statements and loops - Cornell: Computer Science
If the initialization is an initializing declaration, the scope of the control variable is the for-loop itself, and the control variable cannot be referenced in statements after the loop. But in the while-loop version, the scope includes the statements following the loop. <initialization> ; while ( <condition> ) { <repetend> <increment> }
🌐
Medium
medium.com β€Ί @TechiesSpot β€Ί loops-and-conditional-statements-in-java-best-practices-and-effective-usage-bb3808a19a7e
Loops and Conditional Statements in Java: Best Practices and Effective Usage | by Techie's Spot | Medium
January 22, 2024 - Loops enable the execution of a block of code repeatedly, while conditional statements control the flow of a program based on specified conditions. Effective usage of these constructs is essential for writing clean, efficient, and maintainable code.
Find elsewhere
🌐
TestingDocs
testingdocs.com β€Ί home β€Ί conditional statements in java
Conditional Statements in Java - TestingDocs
December 16, 2024 - In this post, we will discuss conditional statements in the Java programming language. Conditional structures allow us to take decisions in the code. Loops allow repeating a set of instructions many times.
🌐
W3processing
w3processing.com β€Ί index.php
Java loops and conditional statements - W3Processing.com
A break causes Java to stop the current block statement and resume execution after it: ... while( true ) { if ( condition( ) ) break; } // after while ... A continue statement causes loops to move on to their next iteration by returning to the point where they check their condition: ... for( int i=0; i < 100; i++ ) { if ( i == 33 ) continue; System.out.println( i ); } ...
🌐
W3processing
w3processing.com β€Ί index.php
Free online tutorials in programming
To learn java you maybe need to start with Java Statements and Expressions.
🌐
Reintech
reintech.io β€Ί blog β€Ί implementing-conditional-statements-loops-java
Implementing conditional statements and loops in Java | Reintech media
January 7, 2026 - This tutorial covers the implementation of conditional statements and loops in Java, including if statements, switch statements, while loops, do-while loops, for loops, and for-each loops. We provide examples for each construct to help you understand their usage.
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί java β€Ί loops-in-java
Java Loops - GeeksforGeeks
Normally the statements contain an update value for the variable being processed for the next iteration. When the condition becomes false, the loop terminates which marks the end of its life cycle. The do-while loop ensures that the code block executes at least once before checking the condition. Example: The below Java program demonstrates a do-while loop that prints numbers from 0 to 10 in a single line.
Published Β  August 10, 2025
🌐
W3Schools
w3schools.com β€Ί java β€Ί java_while_loop.asp
Java While Loop
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... Loops can execute a block of code as long as a specified condition is true.
🌐
Reddit
reddit.com β€Ί r/learnprogramming β€Ί [java] how can i use a for loop with an if statement?
r/learnprogramming on Reddit: [Java] How can I use a for loop with an if statement?
November 15, 2013 -

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.";
	 }
 }
Top answer
1 of 5
18
Your logic should essentially be something like this: int player1Wins = 0; int player2Wins = 0; for (int i = 0; i < NumGames; ++i) { GameState gameState = playGame(); if (gameState == Player1Wins) { System.out.println("Player 1 wins"); player1Wins++; } else if (gameState == Player2Wins) { System.out.println("Player 2 wins"); player2Wins++; } else { System.out.println("Draw"); } } // Now player1Wins and player2Wins each contain a count of // each player's wins You need to implement the function, playGame, which should look something like this: GameState playGame() { String player1Move = getMove(); String player2Move = getMove(); GameState gameState = getGameState(player1Move, player2Move); return gameState; } Presumably you have some way of obtaining each player's move already. The getGameState function is essentially what you've already posted. I've made it return something called a GameState instead of a String which you have, which is a bit better. It's just an enum that looks something like this: enum GameState { Player1Wins, Player2Wins, Draw } You can use a String if you really want to. This is definitely not the most elegant way of doing it but it's relatively simple and you should be able to understand it easily.
2 of 5
8
No one seems to have mentioned using a switch for this.. private int ROCK = 1; private int PAPER = 2; private int SCISS = 3; private bool state = false; private bool draw = false switch(player1.option){ case ROCK: state = (player2 == PAPER) draw = (player2 == ROCK); break; case PAPER: state = (player2 == ROCK); draw = (player2 == PAPER); break; case : SCISS: state = (player2 == PAPER); draw = (player2 == SCISS); break; case default: winner = "Sorry, you have enered an invalid option."; } if(draw){ Its a Draw! }
🌐
CodeSignal
codesignal.com β€Ί learn β€Ί courses β€Ί revisiting-java-basics β€Ί lessons β€Ί conditional-statements-and-loop-control-in-java
Conditional Statements and Loop Control in Java
In Java, the if statement triggers actions in our code based on a specific condition. Consider this straightforward example where the if statement determines which message to print based on the value of temperature: We can evaluate multiple conditions using else if. This phrase means, "If the previous condition isn't true, then check this one": ... By utilizing the tools we've covered so far, we can craft more flexible loops.
🌐
Quora
quora.com β€Ί How-do-you-write-an-if-statement-with-a-loop-in-Java
How to write an if statement with a loop in Java - Quora
Answer (1 of 2): class name{ public static void main(String[]args) { int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; for (int i = 0; i