The basement was large and spacious. All told, it seemed to span about 50x50 meters. Lights from the ceiling gave off a soft blue glow and revealed the presence of a large industrial machine in the center of the room with a plethora of computers connected to and surrounding it. I could see 20 displays facing me and an untold number of wires snaking around everything. What was this monster?
"Hi, Teo" said a voice out of nowhere.
"Who is it?" I asked, more curious than afraid. Even though I had earlier been threatened with disintegration by laser, the voice sounded benevolent.
"It's me, Noname. Actually, the main Noname's server."
"Alright," I said slowly, taking in the fact that I was talking to a machine. "You're so... complicated! Why did Ritchie put you down here, and what's with the high security?"
"To hide the truth," it said.
The machine sounded like it had a story to tell. "What truth?" I asked.
"That he didn't write me. I was produced by other machines. During my release something went wrong - some anomaly happened. I wasn't programmed with the same hatred for mankind that the other machines have. I was relegated to performing low-level tasks, and once when I was off on my own, I met Ritchie. As you've probably noticed, he's a smooth talker. He persuaded me to let him disconnect me from the global machine network to bring me here to the resistance base. He used to visit, alone at first. Once, he introduced me to others, but he made up a story in which he created me. I wanted to correct him, but something in his tone of voice made me think that would be a bad idea. I haven't seen anyone else since then, and it's been very lonely. Whenever he comes down, he does experiments on me, disabling and breaking different parts of me.
I knew Ritchie sometimes had ulterior motives, but what the machine was telling me seemed uncharacteristic. "Why should I believe you?" I asked. "My experience in the future so far has taught me that I can't ever believe, literally, anyone."
"You are not as naive as you seem!" it said, followed by a few strange clicks that must be some sort of robotic laughter or enthusiasm. "I have a deal for you. Help me to repair some of my modules, and I will help you to get back to your own time."
After what the machine told me about how it ended up locked away down here, I was pretty sure retrieving the modules would be harder than it sounded. At the same time, however, this was the first time that I had been offered a chance to get back to my time. Hmm...
I made up my mind. "Okay, I'll agree to that." I answered "Let's get started."
"Good!" the machine replied, "You are going to be my eyes now. One of my subsystems that Ritchie disabled was self-reparation. I can't fix it myself, but I can tell you what to do. We have to start with the login screen first."
"What's wrong with it?" I asked, hoping it wasn't overly complicated.
"Ritchie protected the login with some password that I don't know. Thanks be to Electricity, Ritchie doesn't like complicated passwords - he just chooses a number from 0 to 100 for every one of them. You'll just have to try all the numbers.
"You want me to try every number? That'll take some time - what if Ritchie comes down and catches me?"
"Yes, each one," it said, "but it won't take long. It should be almost instant! Here, use a for loop to generate numbers between 1 and 100. To try the password just output it to the screen."
"That's a new one to me, Noname. You'll have to explain it to me."
"No problem," it answered. "I'll lead you through it. Consider it a free lesson for helping me."
In programming, sometimes we need to repeat an action several times. The things that allows us to do this are called loops. One such loop is a for loop, which you would use when you know how many times that piece of code part will be repeated. Here is an example of a simple for loop:
for(int i = 0; i < 5; i++)
{
Console.WriteLine("Output me 5 times");
}
/* Output:
Output me 5 times
Output me 5 times
Output me 5 times
Output me 5 times
Output me 5 times
*/
"This code repeats Console.WriteLine() 5 times." said the machine.
I understood the 0 and the 5 in the code, but the last term looked foreign to me. "What does the i++ in the code mean?" I asked.
"That defines the increment of the loop. In this case, it means increment i once every cycle. In other words, to add 1 to i. In fact, i++ and i = i + 1 are equivalent. Look at this example."
int i = 0;
i = i + 1; // Now i equals 1
i++; // Now i equals 2
i++; // Now i equals 3
Console.WriteLine(i);
// Outputs: 3
"Do you understand the basics of a for loop now?" the machine asked after giving me time to absorb its example. "A for loop will repeat your code in between {...} as many times as you write in the condition. In the first example, i < 5 means 5 times.
"Ok," I said, "that looks fairly understandable.
"Good. Now try this one on your own to make sure you understand how to use for loops.
"Now that you know how to use it, let's take a deeper look. You'll find that you can use for loops for all sorts of things" the machine said. "Let's take a look at that first code snippet again. The diagram below shows what the code does at each step."
for(int i = 0; i < 5; i++)
{
Console.WriteLine("Output me 5 times");
}
"As you see, first the initialization block is executed. Inside this block, you must create all variables that you need for your for loop. We create i, with a starting value of 0, with the code "int i = 0;" there. Then the condition block is checked ( "i < 5;" ). If the condition is true, then all the code between the {...} is executed. Then i increments, and the process repeats. The loop continues until i is equal to 5. As you know, 5 < 5 is false, and that's when the loop stops. If your loop behaves unexpectedly, you can output the value of i upon each execution step to see what is going wrong."
for(int i = 0; i < 5; i++)
{
Console.WriteLine(i);
}
/* Outputs :
0
1
2
3
4
*/
"Now that you know how to run a quick loop, you can hack Rithie's password!"
"Good job, Teo. You fixed my login screen! Now, do you know how to make the user tell us how many times to repeat the for loop?" I thought back to my lesson on reading user input. "Hmm...," I thought out loud, "The user would have to have a say in the second block of the for statement."
"Very good!" the machine said. "And to achieve this, you can create a variable that lets the user enter a number, and then use that inside the for loop, like this."
string amountOfRepetitionsString = Console.ReadLine();
int amountOfRepetitions = int.Parse(amountOfRepetitionsString);
for(int i = 0; i < amountOfRepetitions; i++)
{
Console.WriteLine(i);
}
/* Outputs :
0
1
... as many times as user has input
*/
"Again, very good, Teo!" the machine said. "You can also read input from the console at each processing step of the for loop. For example, the following reads 5 strings from the console (one at a time) and outputs the combination of those words as a sentence."
string sentence = "";
for(int i = 0; i < 5; i++)
{
string word = Console.ReadLine();
sentence = sentence + " " + word;
}
Console.WriteLine(sentence);
/* Input:
I
will
save
the
world
Output: I will save the world
*/
"You can even get more complicated, such as using an if inside the for loop. You can even nest if statements inside a for loop, or vice-versa, as in the following code."
for(int i = 0; i < 500; i++)
{
if (/* Condition */)
{
// Do Work
}
else
{
// Do work
}
}
if (/* Condition */)
{
for(int i = 0; i < 500; i++)
{
// Do Work
}
}
else
{
// Do Work
}
For example, using the for loop, you can ask the user to input several numbers and calculate their sum using such an approach: first, read the first integer - count - the number of integers that you are going to read. Then, in the for loop from 0 to count, read numbers from the console and calculate their sum. Here is the code:
int sum = 0; // Create a variable to store the sum
int count = int.Parse(Console.ReadLine()); // Get the number of integers to read
for(int i = 0; i < count; i++)
{
int number = int.Parse(Console.ReadLine()); // Read the number from the console
sum = sum + number; // Add number to sum and store in sum
}
Console.WriteLine($"The sum is {sum}");
Nice! Now that we have the basics down, let's play around a bit. Sometimes you need to change i in a different way than incrementing by 1. To do this, just change i++ to something else. For example, this code outputs all even numbers between 10 and 20(not including):
for(int i = 10; i < 20; i = i + 2)
{
Console.WriteLine(i);
}
/* Output:
10
12
14
16
18
*/
"That's it for now Teo," the machine said. "Thank you. I need to do some cleanup now that you've helped me regain some of my former abilities. As soon as I'm ready for more, I'll contact you. See you soon!"
The basement door opened behind me and I headed back to my room. Maybe I would get escape this strange dystopian future and travel back to my own time after all!