9 min readβ’january 7, 2023
Kashvi Panjolia
Kashvi Panjolia
int i = 0;
while (i < 5) {
System.out.print(i);
i++;
}
i
was created and the value 0 was assigned to it. In line two, we created our while loop and put the condition in parentheses. This line tells the computer to execute the while loop while the value of i
is less than 5. The body of the loop is enclosed in the curly brackets {}. Line 3 prints the value of the variable i
, but not on a new line. Line 4 is a critical element of the while loop: the modifier. This line modifies the condition of the while loop -- in this case, the value of the variable i
-- so that in the future, the condition will be false. The value of the variable i
is incremented so the new value is 1. i
is now 1 and 1 is less than 5, the body of the loop will execute again. MCQ questions will often ask you what the output of the while loop is, and it can be hard to keep track of all this information in your head, so you can use a tracing table to help you trace the code. This is a tracing table for the loop above:i | Output |
0 | 0 |
1 | 1 |
2 | 2 |
3 | 3 |
4 | 4 |
i
, then check if it meets the condition of the while loop. Since 5 is not less than 5, the while loop will stop at the fifth iteration. This tracing table is very simple, but as your loops get more complex, the tracing table becomes essential to your success with multiple-choice questions.for (initialization; condition; iteration) {
// code to be executed
}
initialization
statement is executed only once before the first iteration of the loop, and is usually used to initialize a loop counter variable. In the while loop above, we initialized the variable i
before the loop, but with a for loop, we can do it on the same line. The condition
is a boolean expression that is evaluated at the beginning of each iteration of the loop. If the condition is true
, the code inside the loop (the body of the loop) is executed. If the condition is false
, the loop is terminated and the code after the loop block will execute. The iteration
statement is executed at the end of each iteration, and is usually used to update the loop counter variable. In the while loop, we added an i++;
statement at the end of the loop, but we can easily add it to the first line with a for loop. A for loop needs all three of these parts to run properly.for (int i = 0; i < 5; i++) {
System.out.print(i);
}
.length()
and .substring(int index)
, and now it's time to use them in loops.String str = "computer science";
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.substring(i, i+1).equals("e")) {
count++;
}
}
System.out.println(count);
count
variable because this variable dictates how many times the loop should run, while the count
variable keeps track of the number of "e"s. .substring(int startIndex, int endIndex)
method to separate out the current letter in the string. If i
is 0, then i+1
will be 1, and the substring(0, 1)
method will return the first letter in the string "computer science." The first letter is then compared to the string "e" using the equals(String string)
method, which returns a boolean. Since "c" is not equal to "e," the method returns false
, we don't enter the body of the if statement, and the loop goes back to line 3 to run again. count
variable by 1 to track that we have found an "e" in "computer science." After the loop has run for each letter in str
, the print statement at the end prints out the new value of count
, which is 3 because there are three "e"s in "computer science."int rows = 5;
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
*
**
***
****
*****
rows
and initializes it to the value 5. This variable determines the number of rows in the pyramid. Line 2 declares an integer variable i
and initializes it to 1. It also starts a for
loop that will continue to execute as long as i
is less than or equal to rows
(5). This outer for
loop will iterate rows
number of times. Line 3 declares an integer variable j
and initializes it to 1. It also starts a nested for
loop that will continue to execute as long as j
is less than or equal to i
. The inner for
loop will iterate i
number of times for each iteration of the outer loop. This means the inner loop will iterate i
number of times, five times, since the outer loop will iterate five times.j
is equal to 1 and i
is equal to 1, so the inner loop only runs once and only one asterisk is printed. Line 5 closes the inner for loop with a curly bracket. Line 6 creates a new row using the println()
function, and runs only for the outer loop. After line 7, the outer loop is run again, this time with i
being 2, and the whole process starts over, and this time the inner loop runs twice, so two asterisks are printed. It is highly recommended to use a tracing table for a nested for loop because there is a lot of information to keep track of. Β© 2023 Fiveable Inc. All rights reserved.