Using while and do..while loops, you can make the computer reapeat a set of instruction until a codition is met, without having to write it multiple times.

You’re writing a program and you need to repeat a certain piece of code multiple times, perhaps when you get a certain input from the user.

You can’t just throw hundreds of identical if statements and functions, that’s too hard and a waste of time.

Fortunately, in C there are 3 different types of loops: while, do…while and for.

Today we’re going to see the first two.

While

First of all, compare the syntax of while:

and its flowchart:

While flowchart

Like it was for if, condition is an integer number: if it’s equal to zero the condition is false, otherwise it’s true.

You can use the logical operators listed here to make comparison instead of using just a number or a variable.

When the computer encounter a while during the execution of a program, it checks first if the condition is true.

  • If so, the code inside the curly brackets is run
  • Otherwise, it ignores the while and it goes on to the rest of the program

When the computer finishes to run the code inside while, the whole process is repeated: it tests again the condition and sees if it must execute the code one more time and keep repeating or just quit the loop.

You can change this behaviour by using keywords such as break (that we saw last time) and other that we’re going to examine next time.

Do…while

Do…while is very similar to the first one, but with one difference: the condition is tested after the first execution.

First check its syntax and its flowchart, then you’ll get what I mean:

 

Do...while flowchart

Do is just a keyword that indicate the start of the loop.

The computer run the code inside do…while before testing the condition.

Then it decides if it has to do all over again or go on to the next part.

In short, the code inside do…while is executed at least one time.

Compare while and do…while

Do while example While example

See? The content inside do…while was executed one time even if the condition was false.

One last thing: as you can see after do…while I put a semicolon, but after while I didn’t.

The use of semicolons in C is quite irregular, so my recommendation is to learn it for each type of statement. Until now we’ve seen that we put a semicolon after called functions, operations, declarations and assignment of variables. We don’t put a semicolon after #include and statements that need brackets or, in general, that contains a piece of code.

In the case of do…while, we don’t put a semicolon after do, because there’s some code inside it, but we do after while, because it doesn’t contain anything.

Let’s analyse today’s program

As usual we’re going to analyse the new lines of code in this section. For information on the old ones, such as #include and printf, check the previous posts – in particular Make Your First C Program (+ Step-by-step explanation) and Learn C Programming: Variables | Step-by-Step Explanation.

while(n!=12) {

This is the start of our while loop. It presents a condition n!=12 and a content – between the curly brackets.

The condition is represented by a logical operation. It compares the variable n with the constant integer number 12. The operator is “not equal“.

Note: the double equal sign ( A==B ) is a logical operator that means: is A equal to B? The single equal sign ( A = B ) is the assignment operator and it stands for: A is equal to B. The operator I used in this example ( A!=B ) is the opposite of ==, therefore it stands for: is A not equal to B?

In conclusion, while(n!=12) { ... } means: until n isn’t equal to 12 do what’s inside the curly brackets.

Code inside while

Inside the while loop I put a printf that asks the user to enter the number 12 and a scanf that assigns the input to the variable n.

Then the loop starts all over again.

In short, until the user doesn’t enter the right value, the program keeps asking it.

This is a quite useful piece of code to include in bigger projects. If you don’t check what the user enters, you can get an unexpected result!

}

It’s just the end of the loop’s content. At this point the computer will check the condition again and decide if it has to repeat the code or go on to the rest of the program.

This behaviour can be changed with special keywords. I’ll explain them in detail next week, when I’m also going to show you the for loop, so keep an eye on this blog or subscribe to the newsletter for free to be always updated on new posts and having exclusive contents directly in your inbox.

Translation in human language

Computer, this program uses the input/output functions in the library stdio.h

The main code starts in main()

Print “Enter the number 12: ” on the command prompt

Create an integer variable called n

Assign the user’s answer to n

Until n isn’t equal to 12 do this:

Print “It isn’t 12. Retry: “

Assign the user’s input to n

Once you’ve left the loop, print “You did it!”

Conclusion

Today’s post has been quite short.

I was thinking about merging it with the next one, but then it would become too chaotic. Let’s keep things simple.

Write me a comment if you have any questions or advice: let’s make this blog better together!

Otherwise just share it, it takes just a click on the buttons below.

Anyway, the post is finished, have a good day and we will see the next week!

From Zephyro it’s all, Bye!

Categories: Learn C

Leave a Reply

%d bloggers like this: