FOR Loop JAVA
FOR Loop JAVA
• It is used when you want a Code Block to run repeatedly WHILE the
condition is Met.
• It is more compact format but more complicated version of a While
Loops are commonly used to iterate through Collection like Arrays
FOR Loop
For (initialization; condition; operation){
}
For Loop
for (int i=5; i<5; i++){
System.out.print(i);
}
Iterating Arrays
String[] names =(“joem”, “Emil”, “Haisen”, “Nelson”);
System.out.print(names[i]);
Array Lenght
• You can find the size of an array by calling their length variable (built
in variable in array)
System.out.println(names.Length);
CONDITION in FOR Loop
• You can use Conditions inside for loop for different reasons like,
searching inside an array
String[] names =(“joem”, “Emil”, “Haisen”, “Nelson”);
for (int i=0; i < 5; i++){
if (names[i].equals(“joem”)){
System.out.print(“We found” + names[i]);
break;
}
Authenticating Simulation
• Create a program that has 2 sets of arrays w/ values already one for
the username and one for the password, the username and password
should be paired by index. Let the user input a username and
password then check if the account exists on the 2 sets of arrays.
• If the username and password doesn’t match then display “Account
not found”
• If the username and password matches then display
“Welcome(username)”