As soon as Sunny stepped into the factory building, she was hit by a wave of sound. Various machines were whirring and pounding away, and workers in colorful suits of armor were rushing around, shouting things like, "Error on line C!" or "Someone check on the paint machines!"
Sunny couldn't help but be energized by the hustle and bustle of the factory. She turned to the unnamed person excitedly and asked, "So, where to first?"
The person stood taller in their neon orange armor and declared, "We'll start with the chainmail. Follow me!"
Sunny and Robert trailed after the guide, weaving around assembly lines and ducking past busy workers. Soon enough, they arrived at a row of machines. Metal wire was being fed in through one end, and circular links were released from the other end. Sunny tried to peek through the openings in the machines to see if she could figure out how they worked.
The person in orange armor chuckled and said, "No need to look inside the machines! Luckily, all of us gate-watchers carry around informational diagrams, just in case a curious Javalandian wants to learn more about our work." They produced a laminated paper seemingly out of thin air and handed it to Sunny.
Well, that's convenient, Sunny thought.
On the paper was a sketch of a machine. Wire came in from the left side and looked to be wrapped around a thicker bar inside the machine. Then the resulting coil was cut into links by a blade on the right side of the machine. Sunny nudged Robert excitedly and pointed at the diagram. The robot shrugged.
"As you can see here, as long as the wire keeps coming in, the machine has to wrap it into coils and then make links. We can use a while loop for that!" The person's armor clanked as they raised two fingers into the air. "So, there are two main types of loops we use here in Javaland. For loops are for when we need to do something a certain number of times. While loops are for when we want to do something until a condition is met."
The gate-watcher pointed to the diagram. "In this case, a while loop would work better because we want the machine to keep working until the factory closes. We don't know how many links the machine will be able to make exactly, so a for loop wouldn't work.
"As for the condition, it could be time. The machine will keep working until the end of the day." The armored person took out a marker and scribbled some lines of code on the laminated paper.
while (!endOfDay) {
}
"And while the condition in the parentheses is true, whatever's inside the curly braces will keep executing."
while (!endOfDay) {
wrap wire around bar;
cut off a link;
}
Sunny couldn't see the person's face through the helmet, but she imagined they'd be grinning proudly. "Thanks for the explanation," she started, "but what does the exclamation point mean? It doesn't seem like it's supposed to mean either enthusiasm or factorial…"
"Oh! Well, in coding, the exclamation point is called ‘not,' and it reverses the value of the boolean after it. So, !true is false, and !false is true." The gate-watcher pointed at the diagram. "Here, !endOfDay basically means ‘not end of the day,' so the loop runs while it isn't the end of the day. Does that make sense?"
Sunny nodded thoughtfully. "Yeah. So while it isn't the end of the day, the machine keeps wrapping the wire and cutting off links?"
"Exactly!" Sunny could hear a smile in the person's voice. "Now then, check out the chainmail assembling machines over here."
Sunny and Robert moved to another row of machines. Sunny oohed and ahhed as they took individual links and pieced them together into chainmail.
"In this case, we know exactly how many links we want in a row. So, we can use a for loop! Assuming the number of links in a row is 50, we can write a for loop like this."
for (int i = 0; i < 50; i++) {
add link to row;
}
"Just like ‘while,' ‘for' is a keyword. It signals that we'll be using a for loop. But the integer ‘i' could be named anything else, like ‘a' or even a word or phrase, like ‘javaIsAwesome'!
"Now, let's look at each of the parts of this loop. int i = 0 initializes the variable i and says that it'll start at 0. Then we have the condition, i < 50. This means that the loop will continue while i is less than 50. Finally, the i++ is the increment. i++ is a shorthand for i = i + 1, and it means that each time the code inside the loop runs, i will be increased by 1."
Sunny blinked for a few moments. "Ohhkay?"
"Hm. Maybe a simpler example would help."
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
0
1
2
3
4
"This for loop would print the integers 0 through 4 inclusive. Try stepping through the loop– tracing each of its iterations– to see why!"
Sunny frowned. "Okay, so I get the initialization part. At the beginning, i equals 0, so the integer 0 is printed. And then… there was something about… increasing by one?"
"Yep! Each time the part inside the loop runs, i will be incremented by one. So the second time it runs, 1 will be printed, then 2, and so on. After 4 is printed, i is increased by 1 again, but 5 is not less than 5, so the loop ends without printing 5."
"Right." Sunny took a moment to wrap her head around that. "Is it possible to increment i by more than one at a time?"
"Definitely! You could do i += 2, or i += 100, or even i -= 1, which is the same thing as i-- and would count down instead of up."
"Got it." Sunny whipped out her notebook and tried to scribble down everything she'd learned before she forgot. When she finished, she looked up at the gate-watcher again. "Is that all?"
The orange armor clanked again as the person let out a belly laugh. "Oh, those are just the absolute basics! There are so many other things you can do with loops. You could use logical operators like ‘not,' ‘and,' and ‘or' to customize while loops, or use nested for loops… And of course, don't forget to watch out for infinite loops!"
Sunny's head started spinning. "A-alright…"
"But we don't usually teach casual visitors all of that." The person tapped the diagram. "Even just knowing the basics is useful!"
"Okay. Thank you!"
"Now then, I could show you both around the rest of the factory–"
"Apologies, but I think we've spent enough time here already," Robert interrupted. "Your lesson on loops was much appreciated– I wouldn't have had the patience to teach Sunny myself– but we really should be getting to the Array Train."
Sunny pouted for a moment, then nodded reluctantly. "We should." She smiled at the gate-watcher. "Thank you for the diagram and the lesson, and I hope you can get your name soon!"
"Me too," the person agreed. "Well then, goodbye, and feel free to visit anytime!"
With that, they escorted Sunny and Robert out of the building and to the exit of the factories, then returned to their gate watching duty.
Points: 2564
Reviews: 3807
Donate