---------------------------- int grades[] = getGradeList() boolean found = false int seeking = prompt "What value to find?" for (int i = 0; i < grades.size; i++){ if (grades[i] == seeking) then found = true endif endfor if (found) then print "found a " + seeking endif ---------------------------- // WRONG: int grades[] = getGradeList() boolean found = false int seeking = prompt "What value to find?" for (int i = 0; i < grades.size; i++){ if (grades[i] == seeking) then found = true else // NO found = false // NO endif endfor if (found) then print "found a " + seeking endif ---------------------------- int grades[] = getGradeList() int seeking = prompt "What value to find?" for (int i = 0; i < grades.size; i++) if (grades[i] == seeking) then print "found a " + seeking + " at " + i endif endfor ---------------------------- int grades[] = getGradeList() int seeking = prompt "What value to find?" int foundAt boolean found = false for (int i = 0; i < grades.size; i++) if (grades[i] == seeking) then foundAt = i found = true endif endfor if (found) then print "found " + grades[foundAt] +" at "+ foundAt endif ---------------------------- 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++) if (grades[i] == seeking) then foundAt = i endif endfor if (foundAt != -1) then print "found " + grades[foundAt] +" at "+ foundAt endif ---------------------------- 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++) if (grades[i] == seeking) then foundAt = i endif endfor if (foundAt != -1) then print "found " + grades[foundAt] +" at "+ foundAt endif ---------------------------- 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 endif endfor 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 endif endfor while (foundAt == -1) print "Welcome to the system, user #" + user