C++ Tutorial #7 – While Loops & Do While Loops

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Today, I will be writing two tutorials, these will be very short. First I will begin with “While Loops”, and last I will cover “Do While Loops”. If you remember my last c++ tutorial on “For Loops”, then this will be really easy to learn. Let’s Begin!

 

WHILE LOOPS

 

Here is the syntax for the While Loop:

 

while(condition)
{
statement;
}

 

While Loops are a little bit different than For Loops. Instead of repeating the statement until the condition is true, While Loops  repeat the statement while the condition is true, but it will stop when the condition is false.

 

Let’s begin coding:

 

 

If you wrote this correctly, your program should look like this:

 

10
9
8
7
6
5
4
3
2
1
BLAST-OFF!!!

 

It will not print “0” because that condition is true. You have probably figures out by now, but I’ll still explain how the program works.

 

 

We created int x and set its value to 10. Unlike For Loops, in While Loops we have to initialize a variable for the condition before you start writing your loop.

 

 

We created our for loop with (x > 0) for our condition. Since 10 is greater than 0, the condition is true. Because the condition is true, the program will run the statement inside the loop. In our statement, we tell our program to print the value of x; write a new line; and decrease the value of x by one. Since the condition is still true (9 is greater than 0), the program will continue to run the statement inside the loop, until the condition is false. Once the condition is false, we break out of the loop.

 

 

Then we tell our program to print “BLAST-OFF!!!”.

 

 

DO WHILE LOOPS

 

Here is the syntax for Do While Loops:

 

 

The behavior of Do While Loops are like While Loops, except the condition is checked after the statement is executed instead of before. That makes it so that the statement can run at least once. Like While Loops, Do While Loops repeat the statement while the condition is true, but it will stop when the condition is false. In this next example, I’m going to add a little twist to the code. We will be using strings, and I’m going to throw-in an If Statement.

 

Let’s start coding again:

 

 

Your program should look something like this:

 

Enter Password: ABC123
You entered: ABC123

Enter Password: Password
You entered: Password

Good job, you finally learned how to enter Password!

 

If you noticed, the program prompts the user to enter “Password”. It’s not asking the user what the password is. You just simply type “Password” and hit Enter.

 

Let me explain the code:

 

 

We created a string called “str”. We didn’t assign it a variable because we will be doing that later in our Do While Loop.

 

 

We created our Do While Loop with (str != “Password”) for our condition. Since ABC123, is not equal to Password, that means the condition is true and the loop will run again. If you enter Password the condition will be false and the program will break out of the loop.

 

 

Take a look at the If Statement I wrote inside the Do While Loop (Yes, you can do that). This checks if the user assigns the variable “str” equal to password in lowercase, and changes it to Password with a capital ‘P’. That makes it so the condition is false, and the program will break out of the loop. Always remember that C++ is case sensitive. I also want to point out that you can write an If Statement in one line if you have only one statement, and you don’t have to use curly braces, but make sure you place a semicolon at the end, so your program know that that’s the end of the line.

 

 

That’s pretty much all there is to While Loops, and Do While Loops. Make sure you keep practicing, and experimenting. Try throwing in an if statement or something else to transform the way your program works. Hopefully, this example gave you some ideas. In the next tutorial, we will go back to for loops, and I will be teaching you about Range-based For Loops. It behaves like a For Loop, but it uses a different syntax.

Facebooktwittergoogle_plusredditpinterestlinkedinmail

About Justin

Computer programmer and owner of kauergames.com.
Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.