Sitemap

Control Statement Part-6

3 min readDec 23, 2023

--

In this chapter we are going to discuss while-do statement.

Previous Chapter
First Chapter

Please read the previous chapter first.

Before discussing while-do statement we need to understand pseudocode. Pseudocode is like a code but does not depends on any particular langulage like Java, C++, C etc. Psuedocode is used to describe an algorithm. An algorithm is bunch of statements you performs to accomplish a certain task. Pseudocode is going to be very helpful in describing an algorithm.

While-Do Statement:

Lets first look at format of while-do statement.

while (expression) block_statement;

The statements inside the block_statement NOT including the curly braces is also call the body of the while-do statement. Same goes for if-then statement and if-else-then statement. The body contains one or more statements.

Following is the pseudocode of the while-do statement, following are the steps:

  1. evaluate the expression.
  2. If the evaluated value is true then body of the do-while executes. And the execution go to Step 1.
  3. otherwise, if the evaluated value is false then the next statement if any (after the while-do statement) executes.

There is another thing called iteration that we need to understand. Iteration defines the number of times the body executed. Body contains one or more statements enclosed by open and close curly braces ‘{‘ and ‘}. Note that block includes the body and the curly braces.

Here is the example of the body:

{
int a = 0;
a++;
int b = a;
}

Here the 3 statements together called body and they are enclose by open and close curly braces.

Get rakibul rushel’s stories in your inbox

Join Medium for free to get updates from this writer.

Now lets write a program which will print 1 to 3 using while-do statement. The program is the following:

Fig 1.1

Iteration 1:

  1. The variable counter contains value 1. As a result the expression
    “counter <= 3” is evaluated to true.
  2. As the evaluated value is true the two statements inside the body executed. The statement at line 6 print the current value of counter which is 1 and the statement at line 7 increment the variable counter by 1. So, the variable counter now contains 2.

Iteration 2:

  1. The variable counter contains value 2. As a result the expression
    “counter <= 3” is evaluated to true.
  2. As the evaluated value is true the 2 is printed in the console and then the variable is incremented by 1. So, the variable counter now contains 3.

Iteration 3:

  1. The variable counter contains value 3. As a result the expression
    “counter <= 3” is evaluated to true.
  2. As the evaluated value is true 3 is printed in the console and then the variable is incremented by 1. So, the variable counter now contains 4.

As the variable counter contains 4, the expression “counter <= 3” is evaluated to false. In this case the body of the while-do statement won’t execute. So the execution moves to the next statement at line 10 which print a blank line and then the next statement is executed which print
“Good Bye!” in the console.

Here is the output of the program.

Press enter or click to view image in full size
Fig 1.2

The control statement which allows to execute a group of statements multiple times is called loop also.

Try deleting the statement at line 7 and see what happens. Your program will never terminate. It will continue running while printing 1 one by one. As the variable counter is not incrementing any more the value of it remains 1 always. So, the expression “counter <= 3” will be evaluated to true always. So, there will be an infinite number of iterations. This is sometimes called an infinite loop. Try it!.

In Linux, you can press Ctrl + C to terminate the program forcefully.

This is it for this chapter.

Next Chapter

--

--