One of the most important control structures in Java is the for-loop. A for-loop is a block of code that is repeated with certain rules about how to start, increment, and end the process.
Suppose for example we want to sum all the integers from 3 to 79. The rules for starting, incrementing, and stopping are as follows,
This information can be translated to the for-loop shown below
for(int n = 3; n <= 79; n++) {
}
Now, to sum all the numbers from 3 to 79, we can let the for-loop do the work,
int sum = 0;
for(int n = 3; n <= 79; n++) {
sum += n;
}
System.out.println(sum);//prints 3157
The above example illustrates the following components of a for-loop
Warning: There is something really bad that can happen here. You must write your code so as to ensure that this control statement will eventually become false, thus causing the loop to terminate. Otherwise you will have an endless loop which will crash your program.
Note: the step expression occurs at the botton of the loop. Consider the pseudocode below,
int sum = 0;
for(int n = 3; n <= 79; ...) {
sum += n;
n++;//Just think of the n++ as the last line of code inside the braces
}
System.out.println(sum);//prints 3157
The break command: If the keyword break is executed inside a for-loop, the loop is immediately exited (regardless of the control statement). Execution continues with the statement immediately following the closing brace of the for-loop.
Declaring the start loop variable: The start loop variable can be declared in the parenthesis of the for-loop or outside the loop. Both situations are illustrated below,
int sum = 0;
//The start variable is declared in the parenthesis
for(int n = 3; n <= 79; n++) {
sum += n;
}
System.out.println(sum);//prints 3157
int sum = 0;
//The start variable is declared outside the loop
int n = 0;
for(n = 3; n <= 79; n++) {
sum += n;
}
System.out.println(sum);//prints 3157
If n is declared in the parenthesis of the loop, its scope is limited to the interior of the loop and is not recognized outside the loop as illustrated below.
int sum = 0;
//The start variable is declared in the parenthesis
for(int n = 3; n <= 79; n++) {
sum += n;
}
System.out.println(sum);//prints 3157
System.out.println(n);//Will not compile because n is declare in the loop
No curly brackets. If there is only one line of code or just one basic structure (an if-structure or another loop) inside a loop, then the curly brackets are unnecessary. The below example is correct, however it is still hightly recommended to include braces to avoid confusion.
int sum = 0;
for(int n = 3; n <= 79; n++)
sum += n;
System.out.println(sum);//prints 3157
Nested loops is the term used when one loop is placed inside another as in the following example,
for(int outer = 0; outer < 5; outer++) {
System.out.println(outer);//outer is printed 5 times
for(int inner = 0;inner < 8;inner++){
System.out.println(inner);//inner is printed 40 times
}//end inner loop
}//end outerloop
In the example above, the inner loop executes 8 times for each of the five iterations of the outer loop. Therefore, the code inside the inner loop will execute 40 times.