Programming Reference/Librarys
Question & Answer
Q&A is closed
for ( init; condition; increment ){ statements; }
a for loop do statements while the condition is true/untrue, the increment counts the loop, it is possible to use more than 1 condition in the for loop
package codereferececomjava; public class CodeRefereceComJava { public static void main(String[] args) { int myOtherCount=5; for (int i = 0; i <= myOtherCount; i++) { System.out.println("i is: "+ i + " <= " + myOtherCount ); } } }
i is: 0 <= 5 i is: 1 <= 5 i is: 2 <= 5 i is: 3 <= 5 i is: 4 <= 5 i is: 5 <= 5
with 2 conditions
package codereferececomjava; import java.util.Scanner; public class CodeRefereceComJava { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int passwd=12345; int typedIn = 0; // 2 conditions in for loop for (int i = 0; i < 3 && passwd != typedIn; i++) { System.out.print("please enter 12345 between 3 attempts: " ); typedIn = keyboard.nextInt(); } if (passwd == typedIn) { System.out.println("Success"); } else { System.out.println("Failure"); } } }
please enter 12345 between 3 attempts: 42 please enter 12345 between 3 attempts: 12345 Success