Results 1 to 2 of 2

Thread: do...while loop in C programming

  1. #1
    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.

  2. #2
    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 ... 
      )

Similar Threads

  1. Watercooling: Single loop or Dual Loop
    By Akolekar in forum Hardware Peripherals
    Replies: 3
    Last Post: 21-10-2011, 10:52 PM
  2. Reboot Loop in Windows 7 Reboot loop and Safe mode doesn't work
    By mADiRAkSHii in forum Operating Systems
    Replies: 4
    Last Post: 25-01-2011, 07:23 PM
  3. Socket programming: Is any new Programming Language?
    By Kushan in forum Software Development
    Replies: 3
    Last Post: 14-11-2009, 11:13 AM
  4. Help in for loop in C programming
    By Wyvern in forum Software Development
    Replies: 3
    Last Post: 08-05-2009, 10:07 AM
  5. Replies: 3
    Last Post: 13-12-2008, 01:49 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,482,321.10451 seconds with 17 queries