0% found this document useful (0 votes)
19 views

For Loop

The document discusses for loops and how to iterate through arrays using for loops. It provides examples of initializing, using conditions, and breaking out of for loops. It also discusses accessing array elements and finding the length of an array. Finally, it provides an example of a program that authenticates a user by checking their input username and password against stored arrays.

Uploaded by

joem oliquino
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

For Loop

The document discusses for loops and how to iterate through arrays using for loops. It provides examples of initializing, using conditions, and breaking out of for loops. It also discusses accessing array elements and finding the length of an array. Finally, it provides an example of a program that authenticates a user by checking their input username and password against stored arrays.

Uploaded by

joem oliquino
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

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

for (Initialization; condition; operation){

// Do anything here
}
For Loop

for (int i = 0; i < 5; i++){

System.out.println(i);

}
ITERATING Arrays

 It means to read every element inside an array and do something with it.
Accessing Array

Value “Joem” “Maridol” “Ferdie” “Heidi” “Fiona”


Index 0 1 2 3 4
ITERATING Arrays

String[] names = (“Joem”; “Maridol”; “Ferdie”; “Heidi”; “Fiona”);

for(int = 0; i < 5; i++){

System.out.println(names[i]);

}
ITERATING Arrays

String[] names = (“Joem”; “Maridol”; “Ferdie”; “Heidi”; “Fiona”;”Argie);

for(int = 0; i < 6; i++){

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)

String[] names = {“Joem”; “Maridol”; “Ferdie”; “Heidi”; “Fiona”};


System.out.println(names.length);
Break Keyword

 Used to Break Out of a LOOP Statement earlier than it is expected to end.


Conditions in FOR Loop

 You can use Conditions inside for loop for different reasons like, searching inside
an array.
Conditions in FOR Loop

String [] names = {“Joem”; “Maridol”; “Ferdie”; “Heidi”; “Fiona”};

for (int=0; i<5; i++){


if (names[i].equalsIgnoreCase(“Maridol”)){
System.out.print(“We Found” + names[i]);
break;
}
}
package katrace420;
import java.util.Scanner;
public class TestJoem {

public static void main(String[] args) {


Scanner s = new Scanner(System.in);
String[] names = {"Joem","Maridol","Ferdie","Heide","Fiona"};
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]);
}
}
}
}
package katrace420;
import java.util.Scanner;
public class TestJoem {

public static void main(String[] args) {


Scanner s = new Scanner(System.in);
String[] names = {"Joem","Maridol","Ferdie","Heide","Fiona"};

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;

public class TestJoem {

public static void main(String[] args) {


Scanner s = new Scanner(System.in);
String[] usernames = {"joem", "maridol", "ferdie", "heide", "fiona"};

String[] passwords = {"joem123", "maridol123", "ferdie123", "heide123",


"fiona123"};
System.out.print("Username :");
String uname = s.nextLine();

System.out.print("Password :");
String pword = s.nextLine();
for (int i = 0; i < usernames.length;i++) {

if (uname.equals(usernames[i]) && pword.equals(passwords[i])) {


System.out.println("Welcome," + usernames[i]);
break;
}else {
System.out.print("Account not Found");
}
}

You might also like