4.2. Cycle through names#
4.2.1. Add parrots#
First, add an array of names for each parrot:
public static void main () {
string[] parrot_names = {"Jurg", "Raffaele", "Dani"};
print ("Enter your phrase: ");
string input = stdin.read_line ();
if (input != null) {
print ("Your phrase: %s\n", input);
}
}
Now let’s use the for
loop to make each parrot repeat the entered phrase.
public static void main () {
string[] parrot_names = {"Jurg", "Raffaele", "Dani"};
print ("Enter your phrase: ");
string input = stdin.read_line ();
if (input == null) {
return;
}
for (int i = 0; i < parrot_names.length; i++) {
print (@"$(parrot_names[i]) says: $input\n");
}
}
Note
Template strings were used inside the for loop
Template strings allow you to print variable values in-line in a string. We’ll cover these in more detail in a later section.
After compilng this program, for the input “It is what it is”, the following output will be displayed:
Jurg says: It is what it is
Raffaele says: It is what it is
Dani says: It is what it is
4.2.2. Refactoring#
The program will always loop through every parrot name. Because of this, we can
replace the for
loop block with a simpler foreach
loop block:
public static void main () {
string[] parrot_names = {"Jurg", "Raffaele", "Dani"};
print ("Enter your phrase: ");
string input = stdin.read_line ();
if (input == null) {
return;
}
foreach (string name in parrot_names) {
print (@"$name says: $input\n");
}
}
Now if you compile and run this program again, you should it should work exactly the same.
This process of improving the program’s code is called refactoring.
4.2.3. Summary#
Congratualations! You’ve now finished this chapter.
To recap, you’ve learned:
Thing 1
Thing 2
Thing 3
– INSERT CHAPTER END MESSAGE HERE –
Now, on to the next chapter!