From the course: Learning C#

While loops - C# Tutorial

From the course: Learning C#

While loops

- In a previous video, we learned about four loops which are usually used to execute a set of program statements for a set number of times. In this video we'll take a look at While loops, which are often used to execute statements for as long as a particular condition exists. So let's take a look at a simple example. So here in my While loops folder, I'll open up the code. So here in my code I have a string variable that I initialize to an empty string and I'm going to create a While loop using the while keyword. So I'm going to do that here and I'm going to write while, and then some parentheses and then the curly braces for my code. So inside the parentheses I need to define the logical expression that's going to be evaluated each time through the loop. So I'm going to have this loop execute while the input Str variable is not equal to the word exit. So I'm going to write while input Str is not equal to exit then I want to do something. So for the loop content what I'm going to do is read a string from the user and print it out to the console. So I'll write input Str equals and I'll call the Read Line function and then I'll just print it back out You entered and then that's going to be input Str So this loop will run until the user enters the word exit. So let's go ahead and give this a try. I'm going to save this and then I'll open up my integrated terminal and let's try this dot net run. All right. So let's go ahead and enter some text, I'll put in my name and then I'll put in, hello, I'll put in stop, and then I'll put an exit, and you can see that when I type in the word exit, the loop stops because now input Str is equal to exit, this expression right here evaluates to false, and then the loop stops. So there is a close relative of the While loop it's called the do-while loop and the main differences in how it's written. So let's close the terminal here and now let's create a same loop. We are going to do the same thing using the do-while loop. So do-while loop looks a little bit different. So I'm going to uncomment these lines here, and I will comment out our previous example using control slash, so to create the same loop using do-while I'm going to write the word do, and then the braces, and then outside the last curly brace I'm going to write while, and then input Str is not equal to exit. Okay. And then I'll take the same code from before, copy that, put it in here, make sure that's indented, here we go. So now at first glance, these two loops look pretty similar, and so if we run this second example, let me scroll up a little bit and bring the terminal back up. So I'll run it again. So now we we have our do-while loop. and so once again, I'll write in, you know, Joe and then hello, and then stop and then exit. So the output looks essentially the same; however, the key difference is that the do-while loop is always guaranteed to run at least one time. Why? Because the condition check is done at the bottom of the loop, whereas in the while case the condition is checked right at the top. So if I initially set the value of input Str to exit and then I ran the first loop again nothing would happen because the input Str not equal to exit, that condition is already evaluating to false. So the do-while loop would execute at least one time because that input Str not equal to exit condition gets checked until after the first iteration. So you can use a while loop if you want the loop to not execute one time if the condition is not met and you can use the do-while loop if you always want the loop to execute at least once before the condition is checked.

Contents