Of course there's a lot more to learn, but I think it would be more fun and more efficient to learn some of the tougher concepts by application.
Without further ado, let's jump into our next lesson!
Lesson #1-16: Analyzing the Looping Class
In the last lesson, I created a class called Looping as follows:
And of course, you can comment below with questions as always.
Now to analyze:
1. The blue text begins the Looping class. (I hope you know this by now ).
2. The light blue text then begins the main method (you should know what the main method is also).
3. The next three statements beginning "int..." initialize three integers: initialValue, finalValue, and counter.
4. The point of this program is to count from a lower number to a higher number, so the first condition it tests for, using an if statement is whether initialValue < finalValue.
5. If this condition is satisfied, we see a bunch of System.out.println("...")
6. The counter variable is increased by 1.
7. The while loop then initiates, and this increases initialValue by 1, and displays the new initialValue.
8. Counter variable is again increased by 1.
9. With each iteration (repetition) of the while loop, we check if the initialValue (which increases by 1 with each iteration) is equal to finalValue, because that is when we can stop counting.
10. When this if condition (colored gold) is satisfied, we stop counting, and display the counter variable, which has been increasing by 1 with each increase in value of the initialValue variable.
Make sure you run this code a few times with your own numbers to understand what exactly is happening at each step.
If you want to make your own programs, you should be able to analyze and at least have a general sense of what other programmers are trying to accomplish with their code.
Lesson #1-17: The For Loop
The while loop utilized a counter that we coded ourselves. Now we will implement the for loop which comes with a built-in counter, so to speak.
The form of a for loop is as follows (as stated in Java docs):
1. The initialization expression initializes the loop; it's executed once, as the loop begins.
2. When the termination expression evaluates to false, the loop terminates.
3. The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value."
Confused?
Let's look at an example:
The first part of the for (...) is the initialization of a variable.
We initiate an integer called variable, with the value of 1.
The second part "variable <11" is the condition. Think of this as: while (variable < 11)...
Everything inside the for loop will run as long as this second part is satisfied.
The third part "variable++" is what happens to the initialized variable after each iteration. We increase it by one.
Lesson #1-18: Applying the For Loop
In this lesson, we will recreate the Looping program using not the while loop, but the for loop!
Have a look at the while loop example again:
The only other segment of code we need to modify is the while loop section.
We will replace this with the for loop :
public static void main(String[] args) {
int initialValue = 0;
int finalValue = 10;
int counter = 0;
if (initialValue < finalValue) {
System.out.println("Input accepted!");
System.out.println("Initial Value: " + initialValue);
System.out.println("Final Value: " + finalValue);
System.out.println();
System.out.println("Initiating count.");
System.out.println();
System.out.println(initialValue);
counter++;
for (initialValue = 1; initialValue < finalValue + 1; initialValue++) {
System.out.println(initialValue);
counter++;
if (initialValue == finalValue){
System.out.println();
System.out.println("Counting complete.");
System.out.println("There are " + counter + " numbers (inclusive) between " + (initialValue - counter + 1) + " and " + finalValue + ".");
}
}
} else {
System.out.println("Final value is less than initial value!");
System.out.println("Please choose new values.");
}
}
}
Also, this code is not color coded. Try to keep track of what each pair of braces contain. An easy way to do this is to start from the highest level and go deeper into the code: (Class, main method, if statement, for loop, if statement). The indentations should help you discern which braces correspond.
If you need help, refer to the previous class "Looping." It is mostly similar.
Also if you copy and paste this into Eclipse, you can click next to a curly brace and Eclipse will draw a small rectangle on its counterpart to make it easier to tell.
Ctrl + Shift + F automatically indents your code for you! Use that well.
The output:
This concludes Day 9.
