Do While Loop in C

Before proceeding to do while loop in C, recap the previous lesson. We seen that in our previous lesson the body of while loop doesn’t execute even a single time. We have read, if while loop condition false then program will not execute then it will be exit. Hence we can say while loop execute zero times.

Do While Loop in C plus plus:

Just imagine, if we need to type a string at least one time. Is while loop correct way to use for this program? Answer is no, because we discussed earlier lesson while loop just execute, If condition will true, means while loop not able to do this particular task. So, For this particular type of question C introduce do while loop. What actually it does?

Do while loop in C language does execute at least one time. The syntax of do while structure is as under:

  do 

  {

      Statements;

  }

  While (Condition);

Above statement shows that, Condition will be test after execute the statement. Therefore, loop of the body is executed at least one time. Then the condition of do while loop is tested,If condition true it will execute repeatedly otherwise false case it will exit in the loop.Statements are enclosed in curly brackets. In do while loop structure While Condition will be written after the statement block.

FLOW CHART of Do While Loop

Do While Loop in C

Now you can see the infrastructure of do while loop. Do while loop is a specific loop which is execute at least one time. Lets take a simple program example to understand the loop.

#include<iostream> // Header files 
using namespace std; 
main()
{
  int digits;
  cout<<"Please type first digit: ";
  cin>>digits;
  do
  {
  	digits++;
  	cout<<digits<<'n';
  }
  while(digits<100);
}

Above mentioned example is the same example of previous lesson while loop with little bit difference, Now we used do while loop structure in the above example. Above Example explicitly describing that, statement execute at least one time. Keep remember put double quotes after the close condition of do while loop. To understand deeply do while loop, we write a simple program of guessing game as under,

#include<iostream> 
using namespace std;
main()
{
  int guess=0;
  char c;
  do
  {
  	cout<<"Please enter character b/w a-z: ";
  	cin>>c;
  // Check the guess character
  if(c=='j')
  {
  	cout<<"Congratulation your guess is Correct !!";
  }
  else{
  	guess=guess+1;
  }
}
  while(guess<=3 && c!='j');
}

In the above guessing game program, we assign two variables then run do while loop, In looping procedure we mention if structure (c==j). And finally end of the loop we write while condition and put double quotes. All do while statement enclosed in curly braces. Here is output screenshot of above guessing game program.

Do While Loop in C

Keep remember C language case-sensitive so care about lower case or uppercase alphabets, if you use small ‘j’ so don’t use capital ‘J’ because differ exist both of them. In the Next lesson, we will read about for loop Structure with new enhancement. Hope you understand this structure, if you found any error during practice, then feel free comment below. 😉

0 0 votes
Article Rating
Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

[…] Learn While loop in C Language in next lessons we will take other loops do while loop or for loop structure for more enhancement and advance tricks. Hope you understand the while loop […]