C Program to Find Total of Even Integers

C – do while loop in C programming with Syntax Example

The while and for loops test the termination condition at the top. By contrast, the third loop in C, the do while loop, tests at the bottom after making each pass through the loop body; the body is always executed at least once.

The syntax of the do is below,

do
statement
while (expression);

once the statement is executed, then expression is evaluated. If it is true, statement is evaluated again, and so on. When the expression becomes false, the loop terminates.

Except for the sense of the test, do-while is equivalent to the Pascal repeat-until statement. Experience shows that do-while is much less used than while and for. Nonetheless, from time to time it is valuable, as in the following function i toa, which converts number to character string (the inverse of atoi).

The do loop has the form:

do
{
statements;
}
while (condition)

Notice that the condition is at the end of this loop. This means that Do while loop will always be executed at least once, before the test is made to determine whether it should continue. This is the only difference between while and do while.

The while loop is ideally suited for such cases example, Let us look at flowchart shown below would help you to understand the operation of the while loop.C do while loop

The job is slightly more complicated than might be thought at first, because the easy methods of generating the digits generate them in the wrong order. We have chosen to generate the string backwards, then reverse it.


C do while loop Example

1/* itoa: convert n to characters in s */

void itoa(int n, char s[])
{
int i, sign;
if «sign = n) < 0) 1* record sign *1
n = -n; 1* make n positive *1
i = 0;
do { 1* generate digits in reverse order *1
s[i++] = n % 10 + '0'; 1* get next digit *1
} while «n 1= 10) > 0); 1* delete it *1
if (sign < 0)
s[i++] = '-';
s[i] = '
1/* itoa: convert n to characters in s */
void itoa(int n, char s[])
{
int i, sign;
if «sign = n) < 0) 1* record sign *1
n = -n; 1* make n positive *1
i = 0;
do { 1* generate digits in reverse order *1
s[i++] = n % 10 + '0'; 1* get next digit *1
} while «n 1= 10) > 0); 1* delete it *1
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}
'; reverse(s); }

The do-while is necessary, or at least convenient, since at least one character must be installed in the array s, even if n is zero. We also used braces around the single statement that makes up the body of the do-while, even though they are unnecessary, so the hasty reader will not mistake the while part for the beginning of whi1e loop.

Online Training Tutorials

  • Model View Controller (MVC)Model View Controller Design Pattern (MVC)) in SAP CRMBusiness Server Pages (BSPs) can be created using different programming paradigms and design patterns. The CRM WebClient UI BSP is based on the Model View Controller (MVC) paradigm. MVC is […]
  • servlets tutorialServletRequest InterfaceServletRequest: When a servlet is asked to handle a request (ServletRequest), it often requires specific information about the request in order to process it effectively. The servlet will […]
  • dunning areasWhat is Dunning Procedure in SAP?(Dunning Configuration is not designed. This may be configured basing on the client’s credit policy whenever required and basing on the following documentation.) Define Dunning Areas In […]
  • General Ledger master dataGeneral Ledger Master Data in SAP FICOGeneral Ledger Master Data in SAP FICO: In SAP, the basic element of the master data structure in the General Ledger is the chart of accounts, which is assigned to a company code and […]
  • Cash DiscountTerms of Payment and Cash Discount: OverviewWhat is the mean by Cash Discount? Cash Discount is a reduction in the price of an item for sale, allowed in those cases when payment is made within a stipulated period. In a business […]
  • OOP Interview Questions and AnswersOOP Interview Questions and AnswersThere are still people, for instance, that call Artificial Intelligence to any program that is written in Prolog or Lisp. In the same way, there are those who maintain that any program […]
  • Variant PrincipleWhat is Variant Principle in SAP?The variant principle there is 3 variants. They are 1.fiscal year variant 2.posting period variant3.feild status variant. The first customization is fiscal year variant. The variants […]
  • C Program to Find Total of Even IntegersC – If else Statement with ExamplesThe if else statement is used to express decisions and the syntax as per below, if (expression) statement 1 else statement 2 where the else part is optional. The expression is […]