For Loop
For Loop
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 While Loop For
Loops are commonly used to iterate through Collection like arrays
For Loop
// Do anything here
}
For Loop
System.out.println(i);
}
ITERATING Arrays
It means to read every element inside an array and do something with it.
Accessing Array
System.out.println(names[i]);
}
ITERATING Arrays
System.out.println(names[i]);
}
Array Length
You can find out the size of an array by calling their length variable (built in
variable in an array)
You can use Conditions inside for loop for different reasons like, searching inside
an array.
Conditions in FOR Loop
System.out.print("Name to search:");
String search = s.nextLine();
for(int i = 0; i < names.length; i++) {
if(names[i].equalsIgnoreCase(search)) {
System.out.println("We Found " + names[i]);
break;
}else {
System.out.println(names[i]);
}
}
}
}
Authenticating Simulation
Create a program that has 2 sets of arrays w/ Value 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}”
package katrace420;
import java.util.Scanner;
System.out.print("Password :");
String pword = s.nextLine();
for (int i = 0; i < usernames.length;i++) {