Learning to Code — Part 4d: Conditionals and Loops — Break & Continue, and Logical & Conditional Operators

Scott Rosenbloom
9 min readJul 5, 2018

--

One more thing, which we’ve actually seen already, is the break and continue statements. Regarding the break statement, it simply means you can break out of a loop, and move on to the next statement after that loop. They also note that if you’ve got nested loops (loops within loops), the break statement takes you out of the loop you’re currently in, and into the one just before it.

I was trying to understand, in what situation would I want to break out of a loop, as opposed to exiting it when the condition that is being evaluated is true? A feature I really like about the SoloLearn app is that there’s a Comments board for every single lesson. When I clicked it for this lesson, the first comment I saw was this:

Let’s put answering that question off for the moment.

The other thing is the continue statement. It’s sort of a cousin of the break statement in that instead of breaking out of the loop all together, the continue keyword will just break out of the current iteration of the loop, and go onto the next one.

So, in the following, with the break keyword, it breaks out of the loop all together:

int num = 0;
while (num < 20) {
if (num == 5)
break;
Console.WriteLine(num);
n++;
}

Running this code prints 0, 1, 2, 3, 4 and then stops, because when the value of num equals 5, it breaks out….

HOLY CRAP! I just got it…

OK, (and I would like to have someone correct me on this if I’m wrong) notice that the if keyword, and then the Console.WriteLine(num) statement, are aligned with each other. While the alignment doesn’t cause the code to execute one way or another (although it is good practice), it occurred to me that Console.WriteLine(num) is NOT part of the if statement. This is confusing because, for the if statement, I would think it should be written like this:

if (num == 5) {
break;
}

I THINK that since there’s just a single statement within the if statement, then you don’t have to put in the curly braces.

Five Minutes Later…

I found a post that confirms what I just wrote but, once again, says that it is bad practice. That said…I NOW UNDERSTAND!!!

Anyway, the following code demonstrates the continue statement:

for (int = 0; 1 < 10; i++) {
if (I == 5)
continue;
Console.WriteLine(i);
}

When run, this code initially produces a similar result as compared to the first code block, except that, when the loop get’s to a point where the variable i is equal to 5, the continue statement will break out of the current iteration, and continue to the next one. So, it prints 0, 1, 2, 3 and 4 to the screen, skips 5 (again, because the condition within the if statement is now true), and then prints 6, 7, 8 and 9 to the screen. It does this because the condition within the for loop says, keep running through the loop until the value of i is less than 10. At the point when i equals 10, 10 is not less than 10, so the for loop is finished.

Next, the app goes through logical and conditional operators. The logical operators are used to join multiple expressions, and then return true or false.

The logical operators are:

  • &&, which means AND, and is written like this: y && y (in plain English, y and y)
  • ||, which means OR, and is written like this: x || y (in plain English, x or y)
  • !, which means NOT, and is written like this: !x (in plain English, not x)

As the name implies, this is exactly why you learned logic in high school. So, for the AND operator (which is &&), the following shows when the result is true or false:

  • Left operand is false, right operand is false, the result is false. In other words, if both operands are false, it returns false.
  • Left operand is false, right operand is true, the result is false. In other words, if at least one of the two operands is false, it returns false.
  • Left operand is true, right operand is false, the result is false. Just as with the previous point, if at least one of the two operands is false, it returns false.
  • Left operand is true, right operand is true, the result is true. In other words, if both operands are true, it returns true.

Here’s some code to show, for example, what happens when the two operands are true:

int age = 42;
double money = 540;
if (age > 18 && money > 100) {
Console.WriteLine(“Welcome”);
}

Translated, this code does the following:

  • Create a variable called age, set it’s data type to integer, and set it equal to 42.
  • Create a variable called money, set it’s variable to double, and set it equal to 540.
  • If the variable called age is greater than 18 (which it is, so that statement is true) AND the variable called money is greater than 100 (which it is, so that statement is true), then print “Welcome” to the screen.

Running this code will print “Welcome” to the screen because both of the conditions being evaluated (whether age is greater than 18 (or, is 42 greater than 18?), AND, whether money is greater than 100 (or, is 540 greater than 100?)) are true and, therefore, the code is executed.

It’s also worth mentioning that you can join more than two conditions, like this:

if(age > 16 && age < 80 && grade > 50)

…and this translates into, if the value of age is greater than 16 AND less than 80 (which is the same as saying, if the value of age is between 16 and 80) AND the value of grade is greater than 50, then…do something!

For the OR operator (which is ||), the following shows when the result is true or false:

  • Left operand is false, right operand is false, the result is false. In other words, if both operands are false, it returns false.
  • Left operand is false, right operand is true, the result is true. In other words, if at least one of the two operands is true, it returns true.
  • Left operand is true, right operand is false, the result is true. Just as with the previous point, if at least one of the two operands is true, it returns true.
  • Left operand is true, right operand is true, the result is true. In other words, if both operands are true, it returns true.

Here’s some code to show, for example, what happens when one operands is false and the other is true:

int age = 18;
int score = 85;
if (age > 20 || score > 50 {
Console.WriteLine(“Welcome”);
}

After the creation of the variables, setting of their data types as integers, and assigning them each values, this code says, if the value of age is greater than 20 OR the value of score is greater than 50, print “Welcome” to the screen. In other words, if at least one of the two things is true, then…do something!

As before, keep in mind that you can also use multiple, and different, logical operators at the same time, like this example using OR and AND:

if (age > 16 && age < 80 || age > 0 && age < 16) {
//do something!
}

This translates into, if age is greater than 16 AND age is less than 80, OR age is greater than 0, AND age is less than 16, then…do something! The only way that the overall output would be false, would be if the value of the variable age, was a negative number, 16, or greater than 80. So, one OR the other of these two scenarios has to be true, to execute whatever code is written next.

That’s a lot to say!!

As mentioned, the last logical operator is NOT, written as !. The following shows when the result is true or false:

  • Right operand is true, the result is false.
  • Right operand is false, the result is true.

While I already understood this concept, these two bullet points didn’t immediately make sense. The following code, however, cleared it up:

int age = 8;if ( !(age >= 16) ) {
Console.WriteLine(“Your age is less than 16”)
}

The key here is the if statement, which translates (still confusingly) into, if NOT the value of the variable age is greater than or equal to 16, then…do something! Translating that into the way people actually speak, it says, if the value of the variable age is NOT greater than or equal to 16, then…do something!

The other operator I mentioned is the conditional operator, of which there’s just one, and it’s represented by the question mark (?) symbol. In reality, this conditional operator is just a more efficient way of writing the if statement. So, instead of writing the following code:

int age = 42;
string msg;
if (age >=18)
msg = “Welcome”;
else
msg = “Sorry”;
Console.WriteLine(msg);

…you could write the if statement portion like this:

int age = 42;
string msg;
msg = (age >= 17) ? “Welcome” : “Sorry”;
Console.WriteLine(msg);

Either block of code translates into the following:

  • Create a variable called age, set it’s data type to be an integer and it’s value to be 42.
  • Create a variable called msg, and set it’s data type to be a string (notice that no value is assigned…yet)
  • If the value of the variable age is greater than or equal to 17, set the variable msg to be equal to “Welcome”, otherwise, set the variable msg to be equal to “Sorry”.
  • Print the current value of the variable msg to the screen.

So, msg = (age >= 17) ? “Welcome” : “Sorry”; simply tests the condition of whether or not the value of the variable age is greater than or equal to 17 and, if it is, sets the previously created, but not originally assigned a value, string variable called msg the value “Welcome”. If it isn’t, the string variable msg is assigned the value of “Sorry”.

This brings us to the end of the Conditionals and Loops section. The last part of it, however, is to create a simple, basic calculator, that repeatedly asks the user to enter two values and display their sums, until the user enters the value, “exit”.

I’m going to leave this for the next article, but I will say that, one way or another, I’m ready to launch Visual Studio and create something with what I’ve (re-)learned. Not doing this was definitely my problem the first time around, and most likely, when I did finally launch it much later in the training…I froze!

Anyway, as usual, please feel free to correct, or add to, anything that I’ve written.

Here’s a link to the previous article, Learning to Code — Part 4c: Conditionals and Loops — The For & Do-While Loops.

--

--