Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



do...while loop in C programming

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 07-05-2009
Member
 
Join Date: May 2008
Posts: 21
do...while loop in C programming
  

It often needs to repeat a piece of program as a condition is true (such as the precision of a calculation is insufficient). C offers the following two structures for this:

The do-while statement lets you repeat a statement or compound statement until a specified expression becomes false.

The expression in a do-while statement is evaluated after the body of the loop is executed. Therefore, the body of the loop is always executed at least once.

The expression must have arithmetic or pointer type. Execution proceeds as follows:
  1. The statement body is executed.
  2. Next, expression is evaluated. If expression is false, the do-while statement terminates and control passes to the next statement in the program. If expression is true (nonzero), the process is repeated, beginning with step 1.

The do-while statement can also terminate when a break, goto, or return statement is executed within the statement body.

Code:
while (condition) (/ * 1 structure: a condition for entering the code * / 
    repeating instructions; 
  ) 

  do (/ * structure 2: a condition for the release of the code * / 
    repeating instructions; 
  ) While (condition);
The block between the bracket {} is repeated in both cases as long as the conditions is true. In the case of the first structure, the condition is evaluated first, so it is possible (if the condition is false from the outset) that the instructions are not executed at all. The second structure is known, however, that the instructions will be executed at least once, since the condition is evaluated at the end of each repetition.

Reply With Quote
  #2  
Old 07-05-2009
Member
 
Join Date: Oct 2008
Posts: 54
Re: do...while loop in C programming

Sometimes we would like to evaluate the condition or at the beginning or the end of the loop, but somewhere in the middle. The trick then is to create an infinite loop (condition always true) and use the break which immediately brought out of the loop:

Code:
while (1) (/ * Structure 3: condition in the loop * / 
    Instructions ... 
    if (condition) break; 
    Instructions ... 
  )
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "do...while loop in C programming"
Thread Thread Starter Forum Replies Last Post
Watercooling: Single loop or Dual Loop Akolekar Hardware Peripherals 3 21-10-2011 10:52 PM
Reboot Loop in Windows 7 Reboot loop and Safe mode doesn't work mADiRAkSHii Operating Systems 4 25-01-2011 06:23 PM
Socket programming: Is any new Programming Language? Kushan Software Development 3 14-11-2009 10:13 AM
Help in for loop in C programming Wyvern Software Development 3 08-05-2009 10:07 AM
JetBrains Introduces a New Programming Paradigm With its Meta Programming System JoeFrat Software Development 3 13-12-2008 12:49 PM


All times are GMT +5.5. The time now is 05:11 PM.