---------------------------- int[] grades = getGradeList() boolean found = false int seeking = prompt("What value to find?")0 for (int i = 0; i < grades.size; i=i+1){ if (grades[i] == seeking) { found = true } } if (found) { print("found a " + seeking) } ---------------------------- // WRONG: int[] grades = getGradeList() boolean found = false int seeking = prompt("What value to find?") for (int i = 0; i < grades.size; i=i+1){ if (grades[i] == seeking) { found = true } else { // NO found = false // NO } } if (found) { print("found a " + seeking) } ---------------------------- int[] grades = getGradeList() int seeking = prompt("What value to find?") for (int i = 0; i < grades.size; i=i+1) { if (grades[i] == seeking) { print("found a " + seeking + " at " + i) } } ---------------------------- int[] grades = getGradeList() int seeking = prompt("What value to find?") int foundAt boolean found = false for (int i = 0; i < grades.size; i=i+1){ if (grades[i] == seeking) { foundAt = i found = true } } if (found) { print("found " + grades[foundAt] +" at "+ foundAt) } ---------------------------- int[] grades = getGradeList() int seeking = prompt("What value to find?") int foundAt = -1 // negative means not found for (int i = 0; i < grades.size; i=i+1){ if (grades[i] == seeking) { foundAt = i } } if (foundAt != -1) { print("found " + grades[foundAt] +" at "+ foundAt) } ---------------------------- int[] grades = getGradeList() int seeking = prompt("What value to find?") int foundAt = -1 // end loop early if we found it for (int i = 0; i < grades.size && foundAt == -1; i=i+1) { if (grades[i] == seeking) { foundAt = i } } if (foundAt != -1) { print("found " + grades[foundAt] +" at "+ foundAt) } ---------------------------- int[] userIDs = getUserIDList boolean valid = false do { int user = prompt("What is your ID?") for (int i = 0; i < userIDs.size; i = i + 1) { if (user == userIDs[i]) { valid = true } } } while (!valid) print("Welcome to the system, user #" + user) // often we will want the location afterward, // so could use index instead int[] userIDs = getUserIDList int foundAt = -1 do { int user = prompt("What is your ID?") for (int i = 0; i < userIDs.size; i = i + 1) { if (user == userIDs[i]) { foundAt = i } } } while (foundAt == -1) print("Welcome to the system, user #" + user)