Learning to Code — Part 4b: Conditionals and Loops — The Switch Statement & While Loop

Scott Rosenbloom
6 min readJul 3, 2018

--

OK, here we go, back into the depths (really, at this point, it’s still the shallow end, but you get the point). I mistitled the previous post which really should have been called Part 4a: Conditionals and Loops — The if-else Statement (and I have since corrected that).

Anyway, that should also explain the (lengthy) title of this post, which will start off talking about the switch statement which lets you test whether or not a variable has one of a list of multiple values. Each one of the items in that list is called a case. In other words, in the case of the variable equaling such and such a value from a list of values, do this thing.

Sounds simple enough, but I think the way it was presented within the SoloLearn app confused me initially. The following is what was shown:

int num = 3;
switch (num) {
case 1:
Console.WriteLine(“one”);
break;
case 2:
Console.WriteLine(“two”);
break;
case 3:
Console.WriteLine(“three”);
break;
}

The result of running this code is having the word “three” written to the screen. What confused me here is the fact that the 1, 2 and 3 written after the word case WERE THE ACTUAL VALUES THAT WERE BEING CHECKED! I read them as the first case, the second case and the third case.

This assumption was based on the fact that there’s nothing surrounding the number (like parentheses or quotation marks). As it turns out (and after reading a bit on the C# Tutorial site), for integer values, you don’t have to put anything around it. For string values, on the other hand, you’d surround them in quotation marks, as in the following code:

string name = “Scott”;
switch(name) {
case “Mike”:
Console.WriteLine(“Hi Mike”);
break;
case “Scott”:
Console.WriteLine(“Hi Scott”);
break;
case “Jim”:
Console.WriteLine(“Hi Jim”);
break;
}

The result of running this code is having “Hi Scott” written to the screen. You’ll also notice the break statement. This statement terminates the switch statement. Without a break statement, the code would just continue to evaluate each case value.

It’s also worth mentioning (as listed in that same explanation on the C# Tutorial site) that if the switch and case statements were part of a function, you could use the return statement instead of the break statement (more on the return statement in a future post).

You can also add a way to compensate for a scenario where none of the cases match. This is called the default case, where you use the keyword default instead of case. If we were to add one more line to the last code block above, so that there was a default case, right before the last curly brace, you’d add the following:

     default:
Console.WriteLine(“The default case”);
break;

The next three sections taught me about the while loop, which will loop repeatedly through a block of code as long as a given condition is true. In other words, wake up each morning, see if it’s raining and, if it is, take an umbrella. When the day comes when it finally stops raining, don’t take an umbrella.

An example of the code would be the following:

int num = 1;
while(num < 6) {
Console.WriteLine(num);
num++;
}

Running this code would cause the numbers 1 through 5 to be written to the screen, each one on it’s own line, and here’s why:

First, a variable called num is created, has it’s data value set to be an integer, and it’s value set to 1.Next, the code asks, “While the value of the variable is less than 6...
...print the value of num, which, at this point, is 1, to the screen.
Then, increment the current value of num, which is 1, by 1 (which brings the new value of num to 2).
Remember, num++ really means num = num + 1, or, in this case, num = 1 + 1, which means num now equals 2.
Next, we jump back up to the top of the while loop and see if the statement num < 6 is still true. At this point, since num is equal to 2, it is still less than 6. Therefore...
...print the value of num, which, at this point is 2, to the screen.
Then, increment the current value of num, which is 2, by 1 (which brings the new value of num to 3).

After this, the while loop runs again with the value of num as 3, because 3 is less than 6. It’ll go through it again after that with the value of num as 4, and then 5, printing each number to the screen, each on it’s own line, and then incrementing the value of num by 1.

Finally, when the value of num has been incremented to 6, and we jump back to the top of the while loop, it evaluates whether the value of num is less than 6, and in this case finds it to be false, because 6 is not less than 6. As a result, it breaks out of the code and either the program ends or, if there’s additional code, it proceeds forward.

In Part 4a, we talked about Relational Operators. There’s another kind of operator called compound arithmetic operators. Where num++ translates into num = num + 1, num+=2, translates into num = num + 2. If we used this in the code above (instead of num++):

  • the value 1, would print to the screen,
  • increment by 2 to a value of 3,
  • print the value 3 to the screen,
  • increment by another 2 to a value of 5,
  • print the value 5 to the screen,
  • increment by another 2 to the value of 7,
  • but this time, when it evaluates whether the value of num is less than 6, in this case, it will find it be false, because 7 is not less than 6.

To close out the while loop sections, the SoloLearn app introduces another efficiency, which shortens the code significantly to be the following:

int num = 0
while(++num < 6)
Console.WriteLine(num);

The tutorial states that running this code will have the same result: printing 1 through 5, each on their own line, to the screen. This confused me a bit as well because my eyes immediately darted to the new condition to evaluate within the first line of the while loop (being ++num < 6). If everything else was the same, wouldn’t that mean that it would print 2 through 5, each on their own line, to the screen? Well, yes, except that, that entry is not the only thing that changed in the code above:

  • In the first version of the code above, after the variable num is created, and it’s data type is set to be an integer, it is set to have a value of 1.
  • In this modified code, however, the variable num is set to have a value of 0. That means that when it is incremented by 1, before it’s evaluated as to whether or not it’s less than 6, the variable num now has a value of 1.

Ahh ha! So, not a problem with the tutorial, and not a problem with the code. It was a problem with me, and my curious (or lazy, depending on how you look at it) mind making my eyes go to the most egregious change from what I had seen.

This brings us to the end of the while loop. In the next post, we’ll talk about the for loop, which executes a set of statements a specific number of times.

As always, please feel free to correct, or add to, anything that I’ve written.

Also, here’s a link to the previous post (now correctly titled), Learning to Code — Part 4a: Conditionals and Loops — The if-else Statement.

--

--