Joseph Haugh
University of New Mexico
Recall, the syntax of the while loop:
while (<<booleanCondition>>) {
<<<bodyStatements>>>
}As well as the syntax of the for loop:
for (<<initStatement>>; <<booleanCondition>>; <<updateStatement>>) {
<<bodyStatements>>
}What statements are allowed in <<bodyStatements>>?
Any valid statements!
This includes loops!
Nested loops can be intimidating at first
However, they follow the same rules as any other loop
For example, let’s step through the following code:
void main() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
IO.println("(" + i + ", " + j + ")");
}
}
}Copy and paste this code into Intellij and set a breakpoint on the first loop
What does it print?
Code
void main() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
IO.println("(" + i + ", " + j + ")");
}
}
}
Output
(0, 0)
(0, 1)
(0, 2)
(1, 0)
(1, 1)
(1, 2)
(2, 0)
(2, 1)
(2, 2)
Code
void main() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
IO.println("(" + i + ", " + j + ")");
}
}
}
Pseudocode
i in range [0, 3)
j in range [0, 3)
(i, j)Output
(0, 0)
(0, 1)
(0, 2)
(1, 0)
(1, 1)
(1, 2)
(2, 0)
(2, 1)
(2, 2)
The nested loop can see changes to variables declared in the outer loop
For example, we could start the nested loops j variable at i’s value
What does this code print?
void main() {
for (int i = 0; i < 3; i++) {
for (int j = i; j < 3; j++) {
IO.println("(" + i + ", " + j + ")");
}
}
}Code
void main() {
for (int i = 0; i < 3; i++) {
for (int j = i; j < 3; j++) {
IO.println("(" + i + ", " + j + ")");
}
}
}
Pseudocode
i in range [0, 3)
j in range [i, 3)
(i, j)Output
(0, 0)
(0, 1)
(0, 2)
(1, 1)
(1, 2)
(2, 2)
plusOutplusOut is given a String, s, and another String, word+word parameterplusOut("12xy34", "xy") -> "++xy++"
plusOut("12xy34", "1") -> "1+++++"
plusOut("12xy34xyabcxy", "xy") -> "++xy++xy+++xy"
+word parameterString plusOut(String s, String word) {
// Create a new string to return
// All characters in s replaced with +
// Except for appearances of word
}
String plusOut(String s, String word) {
// Create a new string to return
String result = "";
// All characters in s replaced with +
// Except for appearances of word
return result;
}
s are affected we know we need to loop over s (5-7)String plusOut(String s, String word) {
// Create a new string to return
String result = "";
// All characters in s replaced with +
for (int i = 0; i < s.length(); i++) {
// Except for appearances of word
}
return result;
}
i?n characters where n is the length of wordn characters as we get them, so we need another String (6)n characters?String plusOut(String s, String word) {
// Create a new string to return
String result = "";
// All characters in s replaced with +
for (int i = 0; i < s.length(); i++) {
String nextN = "";
for (int j = i; j < word.length(); j++) {
nextN += s.charAt(j);
}
// Except for appearances of word
}
return result;
}
result (3)s (5-11)
n characters in nextN(6)n iterations, where n is the length of word(7-9)
nextN(8)String plusOut(String s, String word) {
// Create a new string to return
String result = "";
// All characters in s replaced with +
for (int i = 0; i < s.length(); i++) {
String nextN = "";
for (int j = i; j < word.length(); j++) {
nextN += s.charAt(j);
}
// Except for appearances of word
}
return result;
}
Now we need to determine if the nextN is equal to word (11)
If it is we need to append word to the result (12)
Otherwise, we need to append a + (14)
Does this work?
Try this:
plusOut("1xy2", "xy");String plusOut(String s, String word) {
// Create a new string to return
String result = "";
// All characters in s replaced with +
for (int i = 0; i < s.length(); i++) {
String nextN = "";
for (int j = i; j < word.length(); j++) {
nextN += s.charAt(j);
}
// Except for appearances of word
if (nextN.equals(word)) {
result += nextN;
} else {
result += '+';
}
}
return result;
}
plusOut("1xy2", "xy");
+!word.length() + i (7)String plusOut(String s, String word) {
// Create a new string to return
String result = "";
// All characters in s replaced with +
for (int i = 0; i < s.length(); i++) {
String nextN = "";
for (int j = i; j < word.length() + i; j++) {
nextN += s.charAt(j);
}
// Except for appearances of word
if (nextN.equals(word)) {
result += nextN;
} else {
result += '+';
}
}
return result;
}
plusOut("1xy2", "xy");
s including the lastString plusOut(String s, String word) {
// Create a new string to return
String result = "";
// All characters in s replaced with +
for (int i = 0; i < s.length(); i++) {
String nextN = "";
if (word.length() + i <= s.length()) {
for (int j = i; j < word.length() + i; j++) {
nextN += s.charAt(j);
}
}
// Except for appearances of word
if (nextN.equals(word)) {
result += nextN;
} else {
result += '+';
}
}
return result;
}
plusOut("1xy2", "xy");
i by the number of iterations we took in nested loop when we find wordString plusOut(String s, String word) {
// Create a new string to return
String result = "";
// All characters in s replaced with +
for (int i = 0; i < s.length(); i++) {
String nextN = "";
if (word.length() + i <= s.length()) {
for (int j = i; j < word.length() + i; j++) {
nextN += s.charAt(j);
}
}
// Except for appearances of word
if (nextN.equals(word)) {
result += nextN;
} else {
result += '+';
}
}
return result;
}
plusOut("1xy2", "xy");
String plusOut(String s, String word) {
// Create a new string to return
String result = "";
// All characters in s replaced with +
int i = 0;
while (i < s.length()) {
String nextN = "";
if (word.length() + i <= s.length()) {
for (int j = i; j < word.length() + i; j++) {
nextN += s.charAt(j);
}
}
// Except for appearances of word
if (nextN.equals(word)) {
result += nextN;
i += word.length();
} else {
result += '+';
i++;
}
}
return result;
}