C++ do while loop statement

C++ do while loop statement

The do-while loop statement is also used for looping as for loop. The syntax of do-while syntax

do
{
statement
statement
statement
……..
……..
}
while(expression);

note in the do-while loop semi colon is placed after the parenthesis containing the expression

do-whileloop in c++

in the do-while loop the semi colon is place after the parenthesis containing the expression. in the example of do while loop first the body of the loop is execute and after expression is evaluated. if the expression is true or non zero then the body of the loop is executed and this process is executes until the expression is not false. When in the loop expression is false zero the loop terminates.

Generally the while loop is use more frequently than the do-while loop. But there are many situations the do-while loop is better to check the condition at the bottom of loop or check the condition after the body execute. For example in the next program understand the concept of do-while loop

/* program to count digit in a number*/
#include <iostream>
int main()
{
int num,count=0,rem;
std::cout<<"Enter a number";
std::cin>>num;
do
{
num=num/10;
count++;
}
while(num>0);
std::cout << "the number of digits are =" << count;
return 0;
}

Enter a number56
the number of digits are =2

  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise